我在UIScrollView
的一个问题上挣扎了很长时间。基本上,它是一个简单的可缩放的UIScrollView
,它显示一个UIImageView
。
当图像被放大到最大,我们释放我的捏手势,动画是奇怪的,并没有顺利地放大到最小的缩放比例。
在苹果的例子中,它实际上是可以复制的:PhotoScroller将图片放大到最大,你就会看到这个问题。
我追踪到它是对layoutSubviews的额外调用,这个调用是在iOS 8中进行的(iOS 7工作得很好)。
有没有人遇到过这个问题,如果是的话,找到了解决办法?
发布于 2014-09-21 12:35:14
我打电话给self就能解决这个问题。layoutSubviews在我的scrollViewDidZoom方法中。有点像黑客,但它似乎解决了我的问题。这可能有助于:
- (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;
}
发布于 2014-09-28 18:26:58
@Jonah的修复为我解决了一个类似的问题,但重要的是不要直接调用layoutSubviews。
您可以使用以下代码实现类似和更安全的效果:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
[self setNeedsLayout]; // triggers a layout update during the next update cycle
[self layoutIfNeeded]; // lays out the subviews immediately
}
有关更多信息,请参见苹果的UIView文档:参考文件/occ/instm/UIView/layoutSubview
https://stackoverflow.com/questions/25852883
复制相似问题