iOS scrollView的setContentOffset同步问题

我用一个子视图初始化一个UIScrollView。 button后,我想要:

  • 添加一个新的子视图
  • 用animation滚动到新的子视图
  • animation完成后删除旧的子视图。

为此,我做了以下几点:

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

问题是,在animation开始之后,“tActualSide”立即被移除,并且也将从animation中移除。

我想同步它,tActualSide只会在animation结束时被删除。

我怎样才能做到这一点?

 [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]; } 

听取委托callback:

 - (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