ios - 如何在 UIScrollView 中缩放 UIImageView?

标签 ios objective-c uiscrollview uiimageview

我在 UIScrollView 中有一个 UIImageView,我可以垂直滚动,但不能水平滚动,因为我设置了内容大小。这正是我想要的。

但是,我似乎无法放大和缩小。我设置了最小和最大缩放比例,但它不起作用。

谁能告诉我为什么?

谢谢。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //self.scrollView.scrollEnabled = YES;

    CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    [self.scrollView setContentSize:scrollableSize];

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
    tempImageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);

    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.minimumZoomScale = 1.0  ;
    self.scrollView.maximumZoomScale = tempImageView.image.size.width / self.scrollView.frame.size.width;
    self.scrollView.zoomScale = 1.0;
    self.scrollView.delegate = self;

    [self.scrollView addSubview:tempImageView];
}

最佳答案

您必须实现以下缩放委托(delegate)方法:

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

并确保将 self.imageView 添加到 self.scrollView 中。 它应该看起来像这样:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    [self.scrollView setContentSize:scrollableSize];

    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
    self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);

    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.minimumZoomScale = 1.0  ;
    self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
    self.scrollView.delegate = self;

    [self.scrollView addSubview:self.imageView];
}

关于ios - 如何在 UIScrollView 中缩放 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28131492/

相关文章:

ios - 如何使用 XCode 在 IOS 中进行单元测试?

ios - 在 EKEvent 中设置选中时间

iphone - 在运行时创建新的对象模板 iPhone

objective-c - 如何在MenuBar的末尾添加新的Menu?

iphone - UIScrollView 每次访问 View 都会更改滚动位置

ios - 为什么 UIScrollView 在移动到另一个文本字段时会重新调整大小 - iOS iPhone

ios - 将 NSString 转换为整数

iphone - 如何在列表中获取一年中的所有月份

ios - ScrollView : unexpectedly found nil while unwrapping an Optional value

ios - NSURLSession 下载任务-进度条问题