我希望在屏幕上设置一个 50x50 的 UIImage 动画,就像按一下按钮时的 ECG 信号一样。
我正在使用一个测试项目并且在没有按钮的情况下运行:
@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;
}];
}
感谢您的帮助。
最佳答案
这是您的解决方案:
- (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;
}];
}];
}
这应该可行,并且您的动画应该一个接一个地出现。
关于ios - 按钮启动的UIImage动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683178/