由按钮启动的UIImage动画

时间:2014-03-27 09:25:30

标签: ios objective-c animation uibutton uiimage

我想在屏幕上为50x50 UIImage制作动画,就像按下一个按钮时的心电图一样。

我正在使用一个测试项目,没有按钮就能正常工作:

@interface IPhoneViewController ()

@end

@implementation IPhoneViewController

- (void)viewDidLoad

{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}


 -(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 170, oldPosition.y);
    self.dot.layer.position = newPosition;


    }];

 }



@end

当我在此代码上方添加行- (IBAction)onButtonPressed:(id)sender; {时,会在行-(void)viewDidAppear:(BOOL)animated {处创建一个错误,指出“使用未声明的标识符'viewDidAppear'”。

首先,如何使用按钮启动动画;其次,我想像ECG机器一样在上下运动中改变动画的方向,但如果我在第一个之后添加另一组方向,只执行最后的指令。

 -(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

[UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 170, oldPosition.y);
    self.dot.layer.position = newPosition;


}];
 [UIView animateWithDuration:1.0F animations:^{
     CGPoint oldPosition = self.dot.layer.position;
     CGPoint newPosition = CGPointMake(oldPosition.x + 10, oldPosition.y - 100);
     self.dot.layer.position = newPosition;


 }];

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

以下是您的解决方案:

- (IBAction)onButtonPressed:(id)sender{
[UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 170, oldPosition.y);
    self.dot.layer.position = newPosition;


}completion:^(BOOL finished){
[UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 10, oldPosition.y - 100);
    self.dot.layer.position = newPosition;


}];
}];


}

这应该有用,你的动画应该是一个接一个。