UIScrollView缩小问题(IOS 8 GM)

时间:2014-09-15 16:45:32

标签: ios objective-c uiscrollview zoom ios8

我已经在UIScrollView问题上苦苦挣扎了很长时间了。 基本上,它是一个简单的可缩放UIScrollView,显示UIImageView

当图像最大缩小时,我会释放我的捏合手势,动画很奇怪,并且不能平滑地放大到最小缩放比例。

它实际上可以在Apple的示例中重现:PhotoScroller 缩小到最大图像,您将看到问题。

我追踪它是对iOS 8中制作的layoutSubviews的额外调用(iOS 7完美运行)。

有没有人遇到过这个问题,如果是的话,找到了解决方案?

2 个答案:

答案 0 :(得分:4)

@Jonah的解决方案为我解决了类似的问题,但重要的是不要直接调用layoutSubviews。

使用以下代码可以获得类似且更安全的效果:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    [self setNeedsLayout]; // triggers a layout update during the next update cycle
    [self layoutIfNeeded]; // lays out the subviews immediately
}

有关详细信息,请参阅Apple的UIView文档: https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/layoutSubviews

答案 1 :(得分:3)

我能够通过调用[self。我的scrollViewDidZoom方法中的layoutSubviews]。这有点像黑客,但似乎已经解决了我的问题。 这可能会有所帮助:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    [self layoutSubviews];
}

将layoutSubviews覆盖为中心内容

- (void)layoutSubviews
{
[super layoutSubviews];

// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = super.bounds.size;
CGRect frameToCenter = imageView.frame;

// center horizontally
if (frameToCenter.size.width < boundsSize.width){
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
}
else {
    frameToCenter.origin.x = 0;
}
// center vertically
if (frameToCenter.size.height < boundsSize.height){
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
}
else {
    frameToCenter.origin.y = 0;
}
imageView.frame = frameToCenter;
}