目标c-如何在Xcode中用动画替换按钮



我的代码中有一个UIButton,它在屏幕上稳定地移动。目前,当用户按下按钮时,alpha将变为0,然后它就消失了。我想做的是在按下按钮/按钮消失后运行一个单独的动画。看起来很容易,但问题是我需要在按下按钮的确切位置运行动画。我对如何实现这一点一无所知。任何帮助都将不胜感激!我将在下面发布一些相关代码。

-(void)movingbuttons{
    movingButton2.center = CGPointMake(x, y);
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveObject)];
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
-(void)moveObject{
    movingButton2.center = CGPointMake(movingButton2.center.x , movingButton2.center.y +1);
}

-(IBAction)button2:(id)sender {
    [UIView beginAnimations:nil context:NULL];
    [movingButton2 setAlpha:0];
    [UIView commitAnimations];
}

用下面的代码替换按钮2操作,并用您想要的动画实现someOtherMethodWithAnimation方法:)

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
 movingButton2.alpha = 0;
[UIView commitAnimations];
[self performSelector:@selector(someOtherMethodWithAnimation) withObject:nil afterDelay:1.0];

将动画替换为:

[UIView animateWithDuration:0.25
             animations:^{
                 self.movingbutton2.alpha = 0.0;
                             // modify other animatable view properties here, ex:
                             self.someOtherView.alpha = 1.0;
             }
             completion:nil];

这只是一个挑剔的地方,但视图控制器的.xib文件中的按钮是否正确地连接到了IBOutlets和IBActions?

更新

您并不局限于修改方法中的那个按钮。您可以在动画块中添加所需的任何代码(请参见上面的udated axample)。

UIView可设置动画的属性,其中有一个动画部分。

另一种方法可能是(我只是以alpha为例):

[UIView animateWithDuration:1.0
                 animations:^{
                     self.movingButton2.alpha = 0.0;
                 }
                 completion:^{
                     [UIView animatateWithDuration:0.25
                                        animations:^{
                                            self.someOtherView.alpha = 1.0;
                                        }
                                       completion:nil];
  }];

这将更有可能保证动画将一个接一个地发生。

最新更新