代码改变世界

iOS UI进阶-3.0 核心动画

  jiangys  阅读(323)  评论(0编辑  收藏  举报

Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<QuartzCore/QuartzCore.h>

Core Animation的使用,请参考最下面的博客。

由于Core Animation动画,改变的只是一个影子,实际的位置和尺寸都不会有变化。因而,在实际开发中,还是建议直接使用UIView动画。

UIView动画

复制代码
- (void)testViewSimpleAnim
{
    [UIView beginAnimations:nil context:nil];
    // 动画执行完毕后, 会自动调用self的animateStop方法
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animateStop)];
    self.myview.center = CGPointMake(200, 300);
    [UIView commitAnimations];
}

-(void)animateStop
{
    NSLog(@"%@",@"--animateStop--");
}
复制代码

 或者:

    [UIView animateWithDuration:1.0 animations:^{
        self.myview.center = CGPointMake(200, 300);
    } completion:^(BOOL finished) {
        
    }];

效果是从红色框移动:

执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在

[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间

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

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

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

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

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

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

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

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

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

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

Block动画

方式一:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

参数解析:

  • uduration:动画的持续时间
  • udelay:动画延迟delay秒后开始
  • uoptions:动画的节奏控制
  • uanimations:将改变视图属性的代码放在这个block中
  • ucompletion:动画结束后,会自动调用这个block

方式二:

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

参数解析:

  • uduration:动画的持续时间
  • uview:需要进行转场动画的视图
  • uoptions:转场动画的类型
  • uanimations:将改变视图属性的代码放在这个block中
  • ucompletion:动画结束后,会自动调用这个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];

参数解析:

  • uduration:动画的持续时间
  • uoptions:转场动画的类型
  • uanimations:将改变视图属性的代码放在这个block中
  • ucompletion:动画结束后,会自动调用这个block

图片翻页

复制代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.index++;
    if (self.index == 3) {
        self.index = 0;
    }
    
    NSString *filename = [NSString stringWithFormat:@"%d.jpg", self.index + 1];
    self.iconView.image = [UIImage imageNamed:filename];
    
    [UIView transitionWithView:self.iconView duration:1.0 options:UIViewAnimationOptionTransitionCurlUp animations:nil completion:nil];
}
复制代码

效果:

 

参考博客:

Core Animation1-简介:http://www.cnblogs.com/mjios/archive/2013/04/15/3021039.html

Core Animation2-CABasicAnimation:http://www.cnblogs.com/mjios/archive/2013/04/15/3021343.html

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示