【问题标题】:Incorrect Resizing of UIScrollView subviews while resizing the scrollView's frame调整滚动视图框架的大小时,UIScrollView 子视图的大小调整不正确
【发布时间】:2011-11-04 16:46:22
【问题描述】:

我有一个 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 年了,所以不是很有经验,但也没有那么笨。

【问题讨论】:

    标签: ios objective-c uiview uiscrollview uiviewanimation


    【解决方案1】:

    一年后我在看这个问题,比我聪明一年,我认为答案是覆盖我的 UIView 子类的 layoutSubviews。

    或将 UIView 的 autoresizeSubviews 属性设置为 YES,并设置正确的子视图的自动调整大小掩码。

    【讨论】:

      【解决方案2】:

      你有没有尝试不覆盖 setFrame 方法?

      如果它不起作用,请尝试在动画块中设置所有子视图的最后一帧。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-12
        • 2011-07-17
        • 1970-01-01
        • 2011-03-18
        相关资源
        最近更新 更多