首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIScrollView变焦问题(IOS 8 GM)

UIScrollView变焦问题(IOS 8 GM)
EN

Stack Overflow用户
提问于 2014-09-15 16:45:31
回答 2查看 1.2K关注 0票数 4

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

当图像被放大到最大,我们释放我的捏手势,动画是奇怪的,并没有顺利地放大到最小的缩放比例。

在苹果的例子中,它实际上是可以复制的:PhotoScroller将图片放大到最大,你就会看到这个问题。

我追踪到它是对layoutSubviews的额外调用,这个调用是在iOS 8中进行的(iOS 7工作得很好)。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-21 20:35:14

我打电话给self就能解决这个问题。layoutSubviews在我的scrollViewDidZoom方法中。有点像黑客,但它似乎解决了我的问题。这可能有助于:

代码语言:javascript
运行
复制
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    [self layoutSubviews];
}

将layoutSubviews重写为中心内容

代码语言:javascript
运行
复制
- (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;
}
票数 3
EN

Stack Overflow用户

发布于 2014-09-29 02:26:58

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

您可以使用以下代码实现类似和更安全的效果:

代码语言:javascript
运行
复制
- (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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25852883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档