zoukankan      html  css  js  c++  java
  • 如何递归执行view的动画

     如何递归执行view的动画

    效果:

     

    山寨的源头:

    图片素材:

    源码:

    //
    //  ViewController.m
    //  RepeatAnimationView
    //
    //  Created by YouXianMing on 15/1/30.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) UIImageView *imageView;
    
    @property (nonatomic) CGRect   startRect;
    @property (nonatomic) CGRect   centerRect;
    @property (nonatomic) CGRect   endRect;
    
    @property (nonatomic) CGFloat  distanceFromStartToCenter;
    @property (nonatomic) CGFloat  distanceFromCenterToEnd;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.distanceFromStartToCenter = 40.f;
        self.distanceFromCenterToEnd   = 30.f;
        
        // 背景色
        self.view.backgroundColor = [UIColor blackColor];
    
        // 红色图片
        self.imageView        = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red"]];
        self.imageView.center = self.view.center;
        self.imageView.alpha  = 0;
        [self.view addSubview:self.imageView];
        
        
        // 设置rect
        self.startRect    = self.imageView.frame;
        
        CGRect tmpRect    = self.startRect;
        tmpRect.origin.y -= self.distanceFromStartToCenter;
        self.centerRect   = tmpRect;
        
        tmpRect           = self.centerRect;
        tmpRect.origin.y -= self.distanceFromCenterToEnd;
        self.endRect      = tmpRect;
        
        // 递归调用
        [self doAnimation];
    }
    
    - (void)doAnimation {
        [UIView animateWithDuration:1.f
                              delay:0.2f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             
                             self.imageView.alpha = 1.f;
                             self.imageView.frame = self.centerRect;
                             
                         } completion:^(BOOL finished) {
                             
                             [UIView animateWithDuration:0.5f
                                                   delay:0.1f
                                                 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^{
                                                  
                                                  self.imageView.alpha = 0.f;
                                                  self.imageView.frame = self.endRect;
                                                  
                                              } completion:^(BOOL finished) {
                                                  
                                                  self.imageView.frame = self.startRect;
                                                  [self doAnimation];
                                              }];
                         }];
    }
    
    @end
    //
    //  ViewController.m
    //  RepeatAnimationView
    //
    //  Created by YouXianMing on 15/1/30.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) UIImageView *imageView;
    @property (nonatomic, strong) UIImageView *cyanView;
    
    @property (nonatomic) CGRect   startRect;
    @property (nonatomic) CGRect   centerRect;
    @property (nonatomic) CGRect   endRect;
    
    @property (nonatomic) CGFloat  distanceFromStartToCenter;
    @property (nonatomic) CGFloat  distanceFromCenterToEnd;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.distanceFromStartToCenter = 40.f;
        self.distanceFromCenterToEnd   = 30.f;
        
        // 背景色
        self.view.backgroundColor = [UIColor blackColor];
    
        // 红色图片
        self.imageView        = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red"]];
        self.imageView.center = self.view.center;
        self.imageView.alpha  = 0;
        [self.view addSubview:self.imageView];
        
        self.cyanView       = [[UIImageView alloc] initWithFrame:self.imageView.bounds];
        self.cyanView.image = [UIImage imageNamed:@"cyan"];
        [self.imageView addSubview:self.cyanView];
        
        
        // 设置rect
        self.startRect    = self.imageView.frame;
        
        CGRect tmpRect    = self.startRect;
        tmpRect.origin.y -= self.distanceFromStartToCenter;
        self.centerRect   = tmpRect;
        
        tmpRect           = self.centerRect;
        tmpRect.origin.y -= self.distanceFromCenterToEnd;
        self.endRect      = tmpRect;
        
        // 递归调用
        [self doAnimation];
    }
    
    - (void)doAnimation {
        [UIView animateWithDuration:1.f
                              delay:0.2f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             
                             self.imageView.alpha = 1.f;
                             self.imageView.frame = self.centerRect;
                             self.cyanView.alpha  = 0.5;
                             
                         } completion:^(BOOL finished) {
                             
                             [UIView animateWithDuration:0.5f
                                                   delay:0.1f
                                                 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^{
                                                  
                                                  self.imageView.alpha = 0.f;
                                                  self.imageView.frame = self.endRect;
                                                  self.cyanView.alpha  = 0.f;
                                                  
                                              } completion:^(BOOL finished) {
                                                  
                                                  self.imageView.frame = self.startRect;
                                                  self.cyanView.alpha  = 1.f;
                                                  [self doAnimation];
                                              }];
                         }];
    }
    
    @end

  • 相关阅读:
    bzoj-2748 2748: [HAOI2012]音量调节(dp)
    bzoj-2338 2338: [HNOI2011]数矩形(计算几何)
    bzoj-3444 3444: 最后的晚餐(组合数学)
    codeforces 709E E. Centroids(树形dp)
    codeforces 709D D. Recover the String(构造)
    codeforces 709C C. Letters Cyclic Shift(贪心)
    codeforces 709B B. Checkpoints(水题)
    codeforces 709A A. Juicer(水题)
    Repeat Number
    hdu 1003 Max Sum (动态规划)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4260949.html
Copyright © 2011-2022 走看看