滚动时自动重置UIView帧

时间:2013-03-29 13:07:38

标签: ios objective-c uiview uiscrollview

UIScrollView包含5 UIView个。当用户触摸UIView中的任何一个时,我基本上想要展开和折叠它们。

动画globalPressReleasesView展开动画的代码:

            [UIView beginAnimations:@"Expand" context:nil];
            [UIView setAnimationDuration:1.0];
            [UIView setAnimationDelegate:self];

                                [globalPressReleasesView setFrame:CGRectMake(globalPressReleasesView.frame.origin.x, globalPressReleasesView.frame.origin.y, globalPressReleasesView.frame.size.width, globalPressReleasesView.frame.size.height + [globalPressReleasesArray count]*105)];

                                [financialPressReleasesView setFrame:CGRectMake(financialPressReleasesView.frame.origin.x, financialPressReleasesView.frame.origin.y + [globalPressReleasesArray count]*105, financialPressReleasesView.frame.size.width, financialPressReleasesView.frame.size.height)];


                                [newOnCscView setFrame:CGRectMake(newOnCscView.frame.origin.x, newOnCscView.frame.origin.y + [globalPressReleasesArray count]*105, newOnCscView.frame.size.width, newOnCscView.frame.size.height)];

                                [latestEventsView setFrame:CGRectMake(latestEventsView.frame.origin.x, latestEventsView.frame.origin.y + [globalPressReleasesArray count]*105, latestEventsView.frame.size.width, latestEventsView.frame.size.height)];

                                [latestCaseStudiesView setFrame:CGRectMake(latestCaseStudiesView.frame.origin.x, latestCaseStudiesView.frame.origin.y + [globalPressReleasesArray count]*105, latestCaseStudiesView.frame.size.width, latestCaseStudiesView.frame.size.height)];

                                [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height + [globalPressReleasesArray count]*105)];

            [UIView commitAnimations];

其他xib属性:

每个UIView剪裁 scrollview.autoresize = YES

问题:

'globalPressReleasesView'完美展开,但当我滚动scrollview时。 globalPressReleasesView帧将自身重置为xib中定义的原始帧值。

有人可以猜出问题是什么吗?

1 个答案:

答案 0 :(得分:3)

首先,让我们简化代码:

CGFloat heightDifference = [globalPressReleasesArray count] * 105.0f;

CGRect globalPressReleasesViewFrame = globalPressReleasesView.frame;
globalPressReleasesViewFrame.size.height += heightDifference;
globalPressReleasesView.frame = globalPressReleasesViewFrame;

NSArray* movedViews = @[financialPressReleasesView, newOnCscView, latestEventsView, latestCaseStudiesView];

for (UIView* movedView in movedViews) {
   CGRect movedViewFrame = movedView.frame;
   movedViewFrame.origin.y += heightDifference;
   movedView.frame = movedViewFrame
}

CGSize contentSize = scrollView.frame.size;
contentSize.height += heightDifference;
[scrollView setContentSize:contentSize];

现在很清楚代码的作用。

代码似乎绝对正确。我建议你检查一下你设置帧的值(NSLog或断点)。

请注意,只有两种方法可以更改视图框。 1.您在代码中更改它 2.它已经自动调整,因为你已经改变了它的祖先的框架(或当用户切换纵向/横向模式时)。