代码:
#import "AnimatedViewController.h"
@interface AnimatedViewController ()
{
UIView * _colorView;
}
@end
@implementation AnimatedViewController
- (void)viewDidLoad {
[super viewDidLoad];
_colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
_colorView.backgroundColor = [UIColor redColor];
[self.view addSubview:_colorView];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[UIView animateWithDuration:3
delay:1
options:UIViewAnimationOptionRepeat
animations:^{
_colorView.frame = CGRectMake(200, 400, 100, 100);
_colorView.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished) {
NSLog(@"动画加载完毕");
}];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[UIView animateWithDuration:3 animations:^{
_colorView.frame = CGRectMake(150, 500, 100, 100);
_colorView.backgroundColor = [UIColor yellowColor];
}];
}
效果: