我有一个UIScrollView子类,我使用UIView动画以编程方式滚动.
我希望用户能够点击或缩放滚动视图的内容的UIImageView ,而动画正在发生.
使用类似于此的配方时,这工作正常:
- (void) scrollSmoothlyatPixelsPerSecond:(float)thePixelsPerSecond {
// distance in pixels / speed in pixels per second
float animationDuration = _scrollView.contentSize.width / thePixelsPerSecond;
[UIView beginAnimations:@"scrollAnimation" context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView setAnimationDuration:animationDuration];
_scrollView.contentOffset = CGPointMake(_scrollView.contentSize.width, 0);
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
现在,因为iOS 4.0,UIView beginAnimations:是不鼓励的.所以我尝试使用块和UIView animateWithDuration更新我的代码:滚动的工作方式与上面相同.
关键和令人抓狂的区别在于,在动画期间,UIScrollView和其他视图不再响应事件处理方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)
也不是:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
Run Code Online (Sandbox Code Playgroud)
尝试缩放时调用.
为清晰起见编辑:没有UIView存在响应触摸事件.这不仅限于UIScrollView.UIScrollView的对等体UIToolbar不响应触摸事件,也不作为UIScrollView对等体的子视图的其他按钮.在动画进行过程中,似乎整个父UIView都被冻结了用户交互.同样,在动画完成之后,所有上述UIView都再次响应.
这些都可以在UIView beginAnimations中调用:无论动画状态如何,都可以进行调用.
我的animateWithDuration:代码略有不同 - 但差异并不重要.动画完成后,再次调用上述触摸事件......
这是我的动画代码:
- (void) scrollSmoothlyToSyncPoint:(SyncPoint *) …Run Code Online (Sandbox Code Playgroud)