但随着UIView的beginAnimations做工精细:动画中的UIScrollView触摸事件不是射击animateWithDuration:
-
26-09-2019 - |
题
我有一个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];
}
现在,由于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;
也不:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
在尝试放大时调用。
修改为清楚起见:未有的UIView本响应触摸事件。这不仅限于在UIScrollView的。该UIScrollView的同行UIToolbar不到触摸响应事件,也不是一个对等方的UIScrollView的子视图其他按钮。看来,整个父的UIView被冻结出的用户交互的的,而动画正在进行。再次,在动画完成后,所有上述UIViews的是再次响应。
这些人都被调用在UIView的beginAnimations:制剂无论动画状态的
我animateWithDuration:代码略有不同 - 但差异并不材料。一旦动画完成,上述的触摸事件被再次调用...
这里是我的动画代码:
- (void) scrollSmoothlyToSyncPoint:(SyncPoint *) theSyncPoint andContinue:(BOOL)theContinueFlag{
float animationDuration = theSyncPoint.time - [player currentTime];
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
[_scrollView setContentOffset:theSyncPoint.contentOffset];
}
completion:^(BOOL finished){
if (theContinueFlag) {
SyncPoint *aSyncPoint = [self nextSyncPoint];
if (aSyncPoint) {
[self scrollSmoothlyToSyncPoint:aSyncPoint andContinue:YES];
}
}
}];
}
的唯一事件处理程序,上述动画块中的火灾是:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
因此,问题:我应该 - 忽视苹果的气馁的标签,并继续使用beginAnimation配方?我应该 - 重新实现缩放和我的其他基于触摸的事件使用则hitTest?有没有在动画块,可以帮助我面对这个问题之间实现差异的一些知识?有什么明显的是我失踪?
我是一个新的开发者给苹果,所以我不知道如何认真地采取他们的气馁的标签。但是,如果这个API将被弃用,然后消失,我宁愿在移动持久的方向。
非常感谢您的关注。
解决方案
您只需要包括调用像这样的动画时UIViewAnimationOptionAllowUserInteraction选项:
[UIView animateWithDuration:animationDuration
delay:0
options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
<snip>];
这是一个狡猾的一个,我知道了! ;)
回复:“望而却步”的标签 - 一般情况下,当苹果告诉你不要做的问候东西设计应用程序,在他们的平台上运行,它通常是为自己好。所以,你是正确的期待采用动画块,而不是笨重的老办法。
其他提示
quickthyme
的答案是正确的。以下是在夫特摘录
夫特4
UIView.animate(
withDuration: 0.3, // specify animation duration here
delay: 0, // specify delay if needed
options: [
UIView.AnimationOptions.allowUserInteraction,
UIView.AnimationOptions.curveLinear
],
animations: {
// animations code
}
)