UIScrollView触摸事件期间没有使用animateWithDuration触发动画:但使用UIView beginAnimations可以正常工作:

Jac*_*fus 15 uiscrollview uiview uiviewanimation objective-c-blocks

我有一个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 *) 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];
                             }
                         }
    }];
}
Run Code Online (Sandbox Code Playgroud)

在上面的动画块中触发的唯一事件处理程序是:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)

所以,问题:我应该 - 忽略Apple 不鼓励的标签并继续使用beginAnimation配方吗?我应该 - 使用hitTest重新实现缩放和其他基于触摸的事件吗?是否有一些关于动画块之间实现差异的知识可以帮助我解决这个问题?有什么明显的东西让我失踪吗?

我是Apple的新开发人员,因此我不知道如何认真对待他们沮丧的标签.但如果这个API将被弃用然后消失,我宁愿朝着持久的方向前进.

非常感谢你的关注.

qui*_*yme 44

您只需要在调用动画时包含UIViewAnimationOptionAllowUserInteraction选项,如下所示:

[UIView animateWithDuration:animationDuration 
                          delay:0 
                        options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
                     <snip>];
Run Code Online (Sandbox Code Playgroud)

我知道,这是一个偷偷摸摸的!;)

回复:"气馁"标签 - 一般来说,当Apple告诉你不要在设计在他们的平台上运行的应用程序时做些什么,这通常是为了你自己的利益.所以你想要采用动画块而不是笨重的旧方法是正确的.