如何递归执行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

目录
相关文章
SwiftUI—如何制作循环动画并指定动画的循环次数
SwiftUI—如何制作循环动画并指定动画的循环次数
625 0
SwiftUI—如何制作循环动画并指定动画的循环次数
|
前端开发 容器
View的测量、布局和绘制过程中父View(当前View)和子View的先后顺序
View的测量、布局和绘制过程中,到底是先测量(布局、绘制)父View,还是先测量子View,这篇文章会从源码角度给出答案。
|
iOS开发
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
277 0
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?
ViewGroup在调用onMeasure时,会先测量父View,还是会先测量子View?
调用View#requestLayout后,哪些View会被影响?
调用View#requestLayout后,哪些View会被影响?
|
API
原来可以这么操作—修改子View绘制顺序
周一的早上,由于项目的开发做的差不多了,正在等待测试结果的我就开始发呆,思考端午节的去处~
322 0
原来可以这么操作—修改子View绘制顺序
element 的dialog嵌套问题,第二次弹出的会被遮住,怎么解决?
element 的dialog嵌套问题,第二次弹出的会被遮住解决办法
1238 0
element 的dialog嵌套问题,第二次弹出的会被遮住,怎么解决?