您好,欢迎来到易妖游戏网。
搜索
您的当前位置:首页十 iOS 之UIVIew动画 和 核心动画的区别

十 iOS 之UIVIew动画 和 核心动画的区别

来源:易妖游戏网

UIVIew动画 和 核心动画的区别

先看一个动画效果

  • 这是一个很简单的动画,我让这个红色的view从(153,139)的位置平移到(200,400)的位置
用核心动画
  • ViewController.m
#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动画
  • ViewController.m
#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的的动画----------------*/

    [UIView animateWithDuration:0.25 animations:^{

        _redView.layer.position = CGPointMake(200, 400);

    } completion:^(BOOL finished) {
        NSLog(@"变化后的位置:%@",NSStringFromCGPoint(_redView.layer.position));
    }];


}




@end
  • 看打印
结论
  • 核心动画一切都是假象,并不会真实的改变图层的属性值,如果以后做动画的时候,不需要与用户交互,通常用核心动画(转场)。

  • UIView动画必须通过修改属性的真实值,才有动画效果。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- vipyiyao.com 版权所有 湘ICP备2023022495号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务