用户在动画UIButton上的互动

See*_*rex 4 cocoa-touch objective-c uibutton uiviewanimation ios

我试图在针对iPhone的Xcode 4.2中制作一个小应用程序.我想要的是UIButton在屏幕上设置动画,当你按下它时,你将它的alpha设置为0.我从UIView类中找到了一个能够处理用户交互的方法,并且来到这个代码:

    [UIView animateWithDuration:3.0 
                      delay:0
                    options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent
                 animations: ^{ CGRect myRect = enemy1.frame;
                     myRect.origin.y = 300;
                     myButton.frame = myRect;
                 }
                 completion:nil];
Run Code Online (Sandbox Code Playgroud)

现在,这可以正确地完成动画,但事实是它立即设置origin.y为300,这意味着我不能按下按钮"当它向下滑动".我只能在那origin.y300个位置按下它,即使它还没有动画到那里,我也可以按下它.

Kur*_*und 6

在视图为动画时,触摸事件在iOS中无法正常工作.您可以在位于源位置时触摸视图,或者在位于目标位置时触摸视图,但触摸事件将无法正确触发视图在动画制作时所处的位置.

要解决此问题,您必须在视图动画时禁用用户交互,忘记在动画时触摸事物的想法,或者您可以使用启用了用户交互的另一个固定视图覆盖动画视图,从而拦截触摸事件,然后尝试确定触摸事件是否在动画视图区域中.你可以这样做:

CGPoint animatingViewPosition = [(CALayer*)[animatingView.layer presentationLayer] position];
Run Code Online (Sandbox Code Playgroud)


iDi*_*lip 1

您可以使用,

button.frame = CGRectMake(100, 100, 60, 40);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4.0];
button.frame = CGRectMake(100, 300, 60, 40);    
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)