iOS scrollView setContentOffset同步问题

时间:2012-07-30 14:29:51

标签: ios uiscrollview

我使用子视图初始化UIScrollView。按钮操作后,我想:

  • 添加新的子视图
  • 使用动画滚动到新的子视图
  • 动画结束时删除旧的子视图。

为此我做了以下事情:

[mCubeView setContentOffset:tOffset animated:YES];    
[tActualSide removeFromSuperview];

问题是,在动画开始后,“tActualSide”会立即被删除,并且也会从动画中删除。

我想同步它,只有在动画结束时才会删除tActualSide。

我该怎么做?

2 个答案:

答案 0 :(得分:5)

侦听委托回调:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

当你收到那条消息时

[tActualSide removeFromSuperview];

引用Apple文档(请注意“setContentOffset:animated:”参考):

scrollViewDidEndScrollingAnimation:
Tells the delegate when a scrolling animation in the scroll view concludes.

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
Parameters
scrollView
The scroll-view object that is performing the scrolling animation.
Discussion
The scroll view calls this method at the end of its implementations of the UIScrollView and setContentOffset:animated: and scrollRectToVisible:animated: methods, but only if animations are requested.

Availability
Available in iOS 2.0 and later.
Declared In
UIScrollView.h

答案 1 :(得分:3)

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[mCubeView setContentOffset:tOffset];
[UIView commitAnimations];
[self performSelector:@selector(remove) withObject:nil afterDelay:0.5f];


- (void)remove
{
    [tActualSide removeFromSuperview];
}