iOS弹簧动画与视图过渡动画

作者: 阳光下慵懒的驴 | 来源:发表于2016-05-25 17:27 被阅读5752次

    这两种动画实现起来都很简单,苹果已经为我们封装好了,并且提供了多种好看实用的效果

    弹簧动画

    弹簧动画
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 创建一个气泡
        UIButton * bubble = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        [self.view addSubview:bubble];
        bubble.layer.cornerRadius = 50;
        bubble.backgroundColor = [UIColor cyanColor];
    
        [bubble addTarget:self action:@selector(bubbleAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    - (void)bubbleAction:(UIButton *)button {
        // 先缩小
        button.transform = CGAffineTransformMakeScale(0.7, 0.7);
        
        // 弹簧动画,参数分别为:时长,延时,弹性(越小弹性越大),初始速度
        [UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:0 animations:^{
            // 放大
            button.transform = CGAffineTransformMakeScale(1, 1);
        } completion:nil];
    }
    

    完事

    视图过渡动画

    视图过渡动画有两种,一种是单个视图的过渡动画,另一种是两个UIView之间的过渡

    过渡类型:

    • UIViewAnimationOptionTransitionFlipFromTop由上向下翻转动画
    • UIViewAnimationOptionTransitionFlipFromBottom由下向上翻转动画
    • UIViewAnimationOptionTransitionCrossDissolve渐现动画
    • UIViewAnimationOptionTransitionCurlDown由上到下翻页动画
    • UIViewAnimationOptionTransitionCurlUp由下到上翻页动画
    • UIViewAnimationOptionTransitionFlipFromRight由左向右翻转
    • UIViewAnimationOptionTransitionFlipFromLeft由右向左翻转

    速度

    • UIViewAnimationOptionCurveEaseInOut 缓入缓出,开始和结束时减速
    • UIViewAnimationOptionCurveEaseIn 缓入,开始时候减速
    • UIViewAnimationOptionCurveEaseOut 缓出,结束时减速
    • UIViewAnimationOptionCurveLinear 线性,匀速运动

    过渡视图和速度在动画的options参数中使用。但是不要使用多种过渡类型,真是要难看死了我的天哪。

    options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseInOut
    

    Swift写法(感谢熊格智障的评论):

    options: [.TransitionCrossDissolve,.CurveEaseInOut]
    

    1. 单个视图的过渡

    单个视图的动画
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 创建一个按钮
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
        [self.view addSubview:button];
        button.backgroundColor = [UIColor orangeColor];
        button.layer.cornerRadius = 3;
        
        [button setTitle:@"👀" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    - (void)buttonAction:(UIButton *)button {
        // transition动画:指定一个view,单独为它设置transition的动画
        // option设置动画类型,这里使用翻页动画以及淡出淡入
        [UIView transitionWithView:button duration:0.7 options:UIViewAnimationOptionTransitionCurlDown | UIViewAnimationOptionCurveEaseOut animations:^{
            [button setTitle:@"😝" forState:UIControlStateNormal];
        } completion:nil];
    }
    

    再来一个实用的imageView图片渐变效果:

    图片渐变
    @interface ViewController ()
    @property (nonatomic, retain) UIImageView * imageView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
        [self.view addSubview:_imageView];
        _imageView.image = [UIImage imageNamed:@"1.jpg"];
        
        // 1s后换图片
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:NO];
    }
    
    - (void)changeImage {
        // 使用渐变效果就够了
        [UIView transitionWithView:_imageView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            _imageView.image = [UIImage imageNamed:@"2.jpg"];
        } completion:nil];
    }
    

    2. 两个视图的过渡动画

    两个视图的过渡动画

    方法:UIViewtransitionFromView: toView: duration: options:方法
    官方解释:toView added to fromView.superview, fromView removed from its superview
    实际效果:动画的作用范围为fromView的父视图的frame。动画结束后fromView会被删除,toView会被添加到fromView的父视图

    @interface ViewController ()
    @property (nonatomic, retain) UIView * view1;
    @property (nonatomic, retain) UIView * view2;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 创建占用半个屏幕的view,作为过渡view的父视图,测试动画的作用范围
        UIView * bigView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 300)];
        [self.view addSubview:bigView];
        bigView.backgroundColor = [UIColor orangeColor];
    
        self.view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
        [bigView addSubview:_view1];
        _view1.backgroundColor = [UIColor redColor];
        
        self.view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
        // 不要加到视图上!看效果
    //    [self.view addSubview:_view2];
        _view2.backgroundColor = [UIColor blackColor];
        
        // 1s后执行
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(animate) userInfo:nil repeats:NO];
    }
    
    - (void)animate {
        [UIView transitionFromView:_view1 toView:_view2 duration:1 options:UIViewAnimationOptionTransitionCurlDown completion:^(BOOL finished) {
        }];
    }
    @end
    

    相关文章

      网友评论

      • 897948f5dfd3:简单粗暴 好
      • 南方旅行:深度好文 :+1:
      • Zeke权:swift用数组
        [UIViewAnimationOptions.TransitionCrossDissolve, UIViewAnimationOptions.CurveEaseInOut]
        阳光下慵懒的驴:@熊格智障 太感谢了!
      • code_间特门:恩,谢谢。尽量多更点,我会保持关注 :wink:
        阳光下慵懒的驴:@code_间特门 非常感谢:grin:。我会经常更新的
      • code_间特门:UIViewAnimationOptionTransitionCurlDown | UIViewAnimationOptionCurveEaseOut
        能问一下楼主这 "|" 是什么运算符, 在这句代码中表达了什么意思
        :flushed:
        阳光下慵懒的驴:@code_间特门 @code_间特门 在这个代码中的意思就是动画类型为向下翻页,并且速度为淡出
        阳光下慵懒的驴:@code_间特门 这个是C语言中的按位或,按二进制位进行或运算。在OC的枚举中使用它可以同时使用多个值

      本文标题:iOS弹簧动画与视图过渡动画

      本文链接:https://www.haomeiwen.com/subject/fuetdttx.html