UIVIew动画 和 核心动画的区别
先看一个动画效果
- 这是一个很简单的动画,我让这个红色的view从(153,139)的位置平移到(200,400)的位置
用核心动画
#import "ViewController.h"
@interface ViewController ()<CAAnimationDelegate>
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"最开始的位置:%@",NSStringFromCGPoint(_redView.layer.position));
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CABasicAnimation * anim = [CABasicAnimation animation];
anim.keyPath = @"position";
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 400)];
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.delegate = self;
[_redView.layer addAnimation:anim forKey:nil];
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"变化后的位置:%@",NSStringFromCGPoint(_redView.layer.position));
}
@end
用UIView动画
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"最开始的位置:%@",NSStringFromCGPoint(_redView.layer.position));
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:0.25 animations:^{
_redView.layer.position = CGPointMake(200, 400);
} completion:^(BOOL finished) {
NSLog(@"变化后的位置:%@",NSStringFromCGPoint(_redView.layer.position));
}];
}
@end
结论