核心动画(UIView封装动画)-转

一.UIView动画(首尾) 
1.简单说明 
UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持。 
执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码在[UIView beginAnimation:nil context:nil]和[UIView commitAnimations]之间。 

常见方法解析: 
(1)设置动画代理对象,当动画开始或者结束时会发消息给代理对象 
+(void)setAnimationDelegate:(id)delegate 

(2)当动画即将开始时,执行delegate对象的selector,并且把beginAnimation:context:中传入的参数传进selector 
+(void)setAnimationWillStartSelector:(SEL)selector 

(3)当动画结束时,执行delegate对象的selector,并且把beginAnimation:context:中传入的参数传进selector 
+(void)setAnimationDidStopSelector:(SEL)selector 

(4)动画的持续时间,秒为单位 
+(void)setAnimationDuration:(NSTimeInterval)duration 

(5)动画延迟delay秒后再开始 
+(void)setAnimationDelay:(NSTimeInterval)delay 

(6)动画的开始时间,默认为now 
+(void)setAnimationStartDate:(NSDate*)startDate 

(7)动画的节奏控制 
+(void)setAnimationCurve:(UIViewAnimationCurve)curve 

(8)动画的重复次数 
+(void)setAnimationRepeatCount:(float)repeatCount 

(9)如果设置为YES,代表动画每次重复执行的效果会跟上一次相反 
+(void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses 

(10)设置视图view的过度效果,transition指定过渡类型,cache设置YES代表使用视图缓存,性能较好。 
+(void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView*)view cache:(BOOL)cache

2.代码示例

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) UIView *customView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _customView = [[UIView alloc] initWithFrame:CGRectMake(10,
                                                           100,
                                                           100,
                                                           100)];
    _customView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_customView];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //打印动画块的位置
    NSLog(@"动画执行之前的位置:%@",NSStringFromCGPoint(self.customView.center));

    //首尾式动画
    [UIView beginAnimations:nil context:nil];
    //执行动画
    //设置动画执行时间
    [UIView setAnimationDuration:2.0];
    //设置代理
    [UIView setAnimationDelegate:self];
    //设置动画执行完毕调用的事件
    [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    _customView.center = CGPointMake(200, 300);
    [UIView commitAnimations];
}

- (void)didStopAnimation{
    NSLog(@"动画执行完毕");
    NSLog(@"动画执行之后的位置:%@",NSStringFromCGPoint(self.customView.center));
}

@end

3.UIView封装的动画与CALayer动画的对比 

使用UIView和CALayer都能实现动画效果,但是在真实的开发中,一般还是主要是用UIView封装的动画,而很少使用CALayer的动画。 
CALayer核心动画与UIView动画的区别: 
UIView封装的动画执行完毕之后不会反弹。即如果是通过CALayer核心动画改变layer的位置状态,表面上看虽然已经改变了,但是实际上它的位置是没有改变的。

 

二.block动画 
1.简单说明  + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion 

参数解析 
duration:动画的持续时间 
delay:动画延迟delay秒后开始 
options:动画的节奏控制 
animations:将改变视图属性的代码放在这个block中 
completion:动画结束后,会自动调用这个block

转场动画  + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion 

参数解析 
duration:动画的持续时间 
view:需要进行转场动画的视图 
options:转场动画的类型 
animations:将改变视图属性的代码放在这个block中 
completion:动画结束后,会自动调用这个block

+(void)transitionFromView:(UIView )fromView toView:(UIView )toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion 
方法调用完毕后,相当于执行了下面两句代码 
//添加toView到父视图 
[fromView.superview addSubview:toView]; 
//把fromView从父视图中移除  
[fromView.superview removeFromSuperview]; 

参数解析 
duration:动画的持续时间 
options:转场动画的类型 
animations:将改变视图属性的代码放在这个block中 
completion:动画结束后,会自动调用这个block

2.代码示例

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong) UIView *customView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _customView = [[UIView alloc] initWithFrame:CGRectMake(10,
                                                           100,
                                                           100,
                                                           100)];
    _customView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_customView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //block代码块动画
    [UIView transitionWithView:_customView
                      duration:3.0
                       options:UIViewAnimationOptionLayoutSubviews
                    animations:^{
                        //执行的动画
                        NSLog(@"动画开始执行前的位置:%@",NSStringFromCGPoint(self.customView.center));
                        _customView.center = CGPointMake(200, 300);
                    } completion:^(BOOL finished) {
                        //动画执行完毕后的首位操作
                        NSLog(@"动画执行完毕");
                        NSLog(@"动画执行完毕后的位置:%@",NSStringFromCGPoint( self.customView.center));
                    }];
}
@end

提示:self.customView.layer.position和self.customView.center等价,因为position的默认值为(0.5,0.5)。

转载于:https://www.cnblogs.com/jiuyi/p/10108509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值