博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS UIView动画(封装动画)
阅读量:4588 次
发布时间:2019-06-09

本文共 3326 字,大约阅读时间需要 11 分钟。

 

UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView 将为这些改变提供动画支持
执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视 图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil][UIView commitAnimations]之间
 
常见方法解析:
+ (void)setAnimationDelegate:(id)delegate
设置动画代理对象,当动画开始或者结束时会发消息给代理对象
 
+ (void)setAnimationWillStartSelector:(SEL)selector
当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中 传入的参数传进selector
 
+ (void)setAnimationDidStopSelector:(SEL)selector
当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入 的参数传进selector
 
+ (void)setAnimationDuration:(NSTimeInterval)duration 动画的持续时间,秒为单位
+ (void)setAnimationDelay:(NSTimeInterval)delay 动画延迟delay秒后再开始

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

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve 动画的节奏控制,具体看下面的备注

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

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

 

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

 

 

#import "NJViewController.h"@interface NJViewController ()@property (weak, nonatomic) IBOutlet UIView *cutomView;@end@implementation NJViewController- (void)viewDidLoad{    [super viewDidLoad];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{       [UIView transitionWithView:self.view duration:1.0 options:0 animations:^{        NSLog(@"animations");        // 要执行的动画        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];    } completion:^(BOOL finished) {        NSLog(@"completion");        // 执行完毕之后执行的动画    }];}- (void)test2{    [UIView animateWithDuration:2.0 animations:^{        NSLog(@"动画执行之前: %@",NSStringFromCGPoint(self.cutomView.center));        // 需要执行动画的代码        self.cutomView.center = CGPointMake(300, 300);            } completion:^(BOOL finished) {        // 动画执行完毕之后执行的代码        NSLog(@"动画执行之后: %@",NSStringFromCGPoint(self.cutomView.center));            }];}- (void)test1{    // 1.创建核心动画    // 注意点:如果通过核心动画改变layer的位置状态, 表面上看上去已经改变了, 但是实质上是没有改变的    CABasicAnimation *anima = [CABasicAnimation animation];    anima.keyPath = @"position";    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];        anima.removedOnCompletion = NO;    anima.fillMode = kCAFillModeForwards;        anima.delegate = self;        // 2.添加核心动画    [self.cutomView.layer addAnimation:anima forKey:nil];}- (void)animationDidStart:(CAAnimation *)anim{    NSLog(@"核心动画执行之前 %@", NSStringFromCGPoint(self.cutomView.layer.position));}- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    NSLog(@"核心动画执行完毕 %@", NSStringFromCGPoint(self.cutomView.layer.position));}- (void)test{    // 1.UIVIEW封装的动画, 动画执行完毕之后不会反弹    NSLog(@"动画执行之前: %@",NSStringFromCGPoint(self.cutomView.center));    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:2.0];    [UIView setAnimationDelegate:self];    [UIView setAnimationDidStopSelector:@selector(didStopAnimatino)];    self.cutomView.center = CGPointMake(300, 300);    [UIView commitAnimations];    }- (void)didStopAnimatino{    NSLog(@"动画执行完毕 %@", NSStringFromCGPoint(self.cutomView.center));}@end
View Code

 

转载于:https://www.cnblogs.com/liuwj/p/6599594.html

你可能感兴趣的文章
吴恩达机器学习笔记(十) —— 推荐系统
查看>>
Linux下Ant安装与配置
查看>>
实验二 用机器指令和汇编指令编程
查看>>
大数据系列之kafka监控kafkaoffsetmonitor安装
查看>>
常用正则表达式
查看>>
Java基础知识
查看>>
Identity Server4学习系列三
查看>>
我的一些学习资源
查看>>
第二届i春秋挖洞大赛的一些感想
查看>>
YAML 语言教程
查看>>
ios开发之C语言基础
查看>>
Cocos Console命令总结
查看>>
网页回到顶部 GoTop 按钮自动隐藏
查看>>
循环语句
查看>>
数据结构与算法(7) -- 二叉查找树
查看>>
【校招面试 之 C/C++】第7题 C++构造函数不能是虚函数的原因
查看>>
Mysql事务及锁
查看>>
TNS-01251: Cannot set trace/log directory under ADR
查看>>
最后一面《HR面》------十大经典提问
查看>>
一篇文章读懂开源web引擎Crosswalk-《转载》
查看>>