简体   繁体   English

物镜c捏放大后如何删除顶部和底部空间?

[英]How to remove top and bottom space after pinch zoom in objective-c?

My problem with after pinch zoom, when I pinch zoom and after drag image then white space on top and bottom. 我捏缩放后的问题,当我捏缩放并拖动图像后,顶部和底部的空白。

My Code as per apple documentation. 根据Apple文档的我的代码。

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html

Sample code 样例代码

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    scrollView.minimumZoomScale=0.5;
    scrollView.maximumZoomScale=6.0;
    scrollView.contentSize=CGSizeMake(1280, 960);
    scrollView.delegate = self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}

在此处输入图片说明 在此处输入图片说明

Thank you. 谢谢。

At first set your minimum zoom scale to 1.0: 首先,将最小缩放比例设置为1.0:

    scrollView.minimumZoomScale = 1.0;

Next, set the contentSize for scrollView: 接下来,为scrollView设置contentSize:

    scrollView.contentSize = [UIScreen mainScreen].bounds.size;

And if you don't want to see the extra space when bouncing, add the next line: 如果不想在弹跳时看到多余的空间,请添加下一行:

    scrollView.bounces = NO;

Completely, the method should looks something like this: 完全,该方法应如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scrollView.minimumZoomScale = 1.0;
    self.scrollView.maximumZoomScale = 6.0;

    self.scrollView.bounces     = NO;
    self.scrollView.contentSize = [UIScreen mainScreen].bounds.size;
    self.scrollView.delegate    = self;
}

Hope it will help. 希望它会有所帮助。

This is how I do it. 这就是我的方法。 If you image fits perfectly into the view, there will be no white space when you zoom and pan. 如果图像完全适合视图,则缩放和平移时将没有空白。

If your image does not fit or scale properly, there will be white space on the sides OR the top. 如果图像不合适或无法正确缩放,则侧面或顶部会有空白。

First make sure your image is the correct size. 首先,请确保您的图像尺寸正确。 Next you need to add minimumZoomScale and maximumZoomScale to the scrollView. 接下来,您需要添加minimumZoomScalemaximumZoomScale的滚动视图。 The rest will work after that. 其余的将在那之后工作。 You can use auto-layout or frames. 您可以使用自动布局或框架。 Up to you. 由你决定。

This example uses auto-layout: 此示例使用自动布局:

#import "ViewController.h"

@interface AutoLayoutScrollView : UIScrollView
@property (nonatomic, weak) UIView *contentView;
@end

@implementation AutoLayoutScrollView
- (instancetype)init {
    if (self = [super init]) {
        UIView *view = [[UIView alloc] init];
        self.contentView = view;

        [self addSubview:view];
        [self.contentView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
        [self.contentView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
        [self.contentView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
        [self.contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
        [self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
    }

    return self;
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];

    UIView *parent = self.superview;

    if (parent) {
        [self.leftAnchor constraintEqualToAnchor:parent.leftAnchor].active = YES;
        [self.rightAnchor constraintEqualToAnchor:parent.rightAnchor].active = YES;
        [self.topAnchor constraintEqualToAnchor:parent.topAnchor].active = YES;
        [self.bottomAnchor constraintEqualToAnchor:parent.bottomAnchor].active = YES;
        [self setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
}
@end


@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) AutoLayoutScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.scrollView = [[AutoLayoutScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView.contentView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES;
    [self.scrollView.contentView.heightAnchor constraintEqualToAnchor:self.view.heightAnchor].active = YES;

    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image"]];
    [self.imageView setContentMode:UIViewContentModeScaleAspectFit];

    [self.scrollView.contentView addSubview:self.imageView];
    [self.imageView.leftAnchor constraintEqualToAnchor:self.scrollView.contentView.leftAnchor].active = YES;
    [self.imageView.rightAnchor constraintEqualToAnchor:self.scrollView.contentView.rightAnchor].active = YES;
    [self.imageView.topAnchor constraintEqualToAnchor:self.scrollView.contentView.topAnchor].active = YES;
    [self.imageView.bottomAnchor constraintEqualToAnchor:self.scrollView.contentView.bottomAnchor].active = YES;
    [self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.scrollView setMinimumZoomScale:1.0];
    [self.scrollView setMaximumZoomScale:3.0];
    [self.scrollView setZoomScale:1.0f animated:YES];
    [self.scrollView setDelegate:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}
@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在scrollview中使文本从顶部开始,而不是从底部开始(Objective-C) - Getting text in scrollview to start at the top, not the bottom (Objective-C) 在Objective-C中在屏幕的顶部和底部添加模糊遮罩 - Add blur mask on top and bottom of the screen in Objective-C 使用Objective-C,如何在不影响顶层的情况下为底层的帧位置设置动画? - With Objective-C, how can I animate the frame position of a bottom layer without affecting top layers? 如何用objective-c中的破折号代替空格? - how to replace space with dash in objective-c? 删除选项卡栏上的多余空间-Objective-C - Remove extra space on tab bar - Objective-C 如何放大和居中显示用户位置(Objective-C) - How to zoom in and center the user location (Objective-C) 如何使用Objective C iOS从iPhone X中的表视图顶部删除多余空间 - How to remove extra space from the top of the Table View in iPhone X using Objective C iOS 如何在我的文本字段中添加顶部填充(Objective-c) - How to add Top Padding in my textField (Objective-c) 如何从 iOS 中 UITextview 的顶部和底部去除多余的空间? - How to remove extra space from the top and bottom of the UITextview in iOS? Objective-c将子视图添加到视图的底部 - Objective-c adding Subviews to the bottom of view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM