调整滚动视图框架大小时 UIScrollView 子视图的大小调整不正确
我遇到的情况是,我有一个 UIView 子类,其中包含 UIScrollView 作为成员。
我用动画调整 UIView 的大小并用它调整滚动视图的大小。 UIScrollView 是一个无限的图像库(即,当分页发生时交换内容的 3 个视图),
[UIView animateWithDuration: ...
当我对代码进行动画处理时,在动画块中我
myView.frame = someNewFrame;
认为动画会在动画处理时插入中间帧大小。即在动画的每一帧期间进行调用:
[myView setFrame: someIntermediateFrame];
所以我覆盖了 myView 的 setFrame 方法:
-(void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self resizeSubviews];
}
- (void)resizeSubviews
{
CGRect frame = self.bounds;
scrollView.frame = frame;
scrollView.contentSize = CGSizeMake(frame.size.width * 3.0, frame.size.height); // 3 pages
[scrollView setContentOffset:CGPointMake(frame.size.width, 0.0) animated:NO]; // set contentOffset to middlePage
[scrollView setNeedsDisplay]; // just in case. don't know if this is necessary. Currently not helpful.
frame.origin.x = 0.0;
frame.origin.y = 0.0;
pageZeroRect = frame;
self.pageZero.frame = frame;
frame.origin.x += frame.size.width;
pageOneRect = frame;
self.pageOne.frame = frame;
frame.origin.x += frame.size.width;
pageTwoRect = frame;
self.pageTwo.frame = frame;
}
除了包含视图之外,不会发生任何动画。滚动视图(和子视图)只是跳转到其最终值。
有什么想法出了什么问题吗?这根本没有意义。一切看起来都不错,但幕后肯定还有其他一些我不知道的事情发生。我已经从事 iOS 编程 1.5 年了,所以我不是很有经验,但也不是那么愚蠢。
I have a situation where I have a UIView subclass with a UIScrollView as a member.
I animate the resizing of the UIView and resize the scrollview with it. The UIScrollView is an infinite image gallery (i.e. 3 Views that swap content in and out as paging occurs)
[UIView animateWithDuration: ...
when I animate the code, in the animations block I have
myView.frame = someNewFrame;
I thought that the animation will interpolate the intermediate frame sizes as it animates. i.e. make the call during every frame of the animation:
[myView setFrame: someIntermediateFrame];
So I overrode myView's setFrame method:
-(void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self resizeSubviews];
}
- (void)resizeSubviews
{
CGRect frame = self.bounds;
scrollView.frame = frame;
scrollView.contentSize = CGSizeMake(frame.size.width * 3.0, frame.size.height); // 3 pages
[scrollView setContentOffset:CGPointMake(frame.size.width, 0.0) animated:NO]; // set contentOffset to middlePage
[scrollView setNeedsDisplay]; // just in case. don't know if this is necessary. Currently not helpful.
frame.origin.x = 0.0;
frame.origin.y = 0.0;
pageZeroRect = frame;
self.pageZero.frame = frame;
frame.origin.x += frame.size.width;
pageOneRect = frame;
self.pageOne.frame = frame;
frame.origin.x += frame.size.width;
pageTwoRect = frame;
self.pageTwo.frame = frame;
}
No animation occurs other than the containing view. The scrollview (and subviews) just jumps to its final value.
Any ideas what's going wrong? It just doesn't make sense. Everything looks ok, but there must be some other stuff going on under the hood that I don't know about. I've been programming iOS for 1.5 years now and so am not super experienced, but not THAT dumb either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一年后,我聪明了一年,我正在考虑这个问题,并且认为答案是覆盖我的 UIView 子类的layoutSubviews。
或者将 UIView 的 autoresizeSubviews 属性设置为 YES,并设置子视图的正确自动调整大小掩码。
I'm looking at this question a year later, a year smarter, and would think the answer would be to override layoutSubviews of my UIView subclass.
or set the UIView's autoresizeSubviews property to YES, and set the correct autoresizing masks of the subviews.
您是否尝试过不覆盖 setFrame 方法?
如果它不起作用,请尝试在动画块中设置所有子视图的最终帧。
Have you try not to overwrite the setFrame method?
if it's not working then, try to set the final frame of all your subviews in your animation block.