【问题标题】:Possible to "UIScrollView.zoomToRect" and then do UIView animations?可以“UIScrollView.zoomToRect”然后做 UIView 动画吗?
【发布时间】:2009-09-25 19:54:26
【问题描述】:
我有一个UIScrollView。我想用它做一些动画,但我想添加一个简洁的小动画,基本上将UIScrollView 缩放回它开始的位置,然后我可以做一些UIView 动画。这可能吗?
我尝试让按钮触发一个带有scrollView.zoomToRect 的方法,并且该方法在执行UIView 动画的后台线程中调用另一个方法。问题是无论我做什么,UIScrollView 都不会缩小到正常,如果我有一个动画。我只想让它缩小,然后是一些动画,但我不能。
在动画块中插入scrollView.zoomToRect方法没有帮助。
有人有想法吗?
【问题讨论】:
标签:
iphone
animation
uiscrollview
【解决方案1】:
我不确定这是否可以作为我自己问题的答案,但万一其他人想知道,或者其他人有更好的解决方案。我使用了以下代码:
(当我点击翻转按钮时调用)
- (void) flipCurrentViewZoomOut {
// If either view is zoomed in
if (view1.scrollView.zoomScale != 1 || view2.scrollView.zoomScale != 1 ) {
if (view1IsVisible == YES) {
[view1.scrollView zoomToRect:[UIScreen mainScreen].bounds animated:YES];
} else {
[bview2.scrollView zoomToRect:[UIScreen mainScreen].bounds animated:YES];
}
[UIView beginAnimations:nil context:nil];
// The duration is enough time for the zoom-out to happen before the second animation methods gets called (flipCurrentView).
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
// When done, then do the actual flipping of the views (exchange subviews, etc.)
[UIView setAnimationDidStopSelector:@selector(flipCurrentView)];
// In order for the zoomToRect to run at all, I need to do some animation, so I basically just move the view 0.01 which is not noticable (and it's animating a flip right after anyway)
if (view1IsVisible == YES) {
view1.frame = CGRectMake(-0.01, 0, 320, 480);
} else {
view2.frame = CGRectMake(-0.01, 0, 320, 480);
}
[UIView commitAnimations];
} else {
// If either view hasn't been zoomed, the flip animation is called immediately
[self flipCurrentView];
}
}
需要注意的重要一点是,在我的flipCurrentView 方法(翻转视图的第二个动画方法)中,我将view1 和view2 的帧重置为[UIScreen mainScreen].bounds(在这种情况下,这就是边界我需要)。我必须这样做,否则我在上面粘贴的动画代码不会第二次运行,因为 origin.x 将是 -0.01 并且它不能从 -0.01 动画到 -0.01,所以它只会跳过那个动画块。
如果我做错了什么,请告诉我,如果有更好的方法可以做到这一点。总是乐于学习:)