UIView的动画

//

//  ViewController.m

//  UI-NO.6

//

//  Created by Bruce on 15/7/20.

//  Copyright (c) 2015年 Bruce. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

{

    UIImageView *animationView;

    NSMutableArray *imageList;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor orangeColor];

    

    [self loadData];

    

    UIImage *image = [UIImage imageNamed:@"niao2-1(被拖移).tiff"];

    

    animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];

//    设置UIImageView播放的动态图数组

    animationView.animationImages = imageList;

//    播放一组动画 需要的时间

    animationView.animationDuration = 1;

    animationView.animationRepeatCount = -1;

    [self.view addSubview:animationView];

//    让动画开始

//    [animationView startAnimating];

//    让动画结束

//    [animationView stopAnimating];

//    判断动画是否正在播放

//    animationView.isAnimating

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(10, 20, 50, 35);

    [button setTitle:@"播放" forState:UIControlStateNormal];

    [button setTitle:@"暂停" forState:UIControlStateSelected];

    [button setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

- (void)play:(UIButton *)sender

{

    

    if (sender.selected != YES) {

//        播放动画  重置按钮的title

        

        sender.selected = YES;

        [animationView startAnimating];

        [self moveAnimation];

        

    }else{

        

        sender.selected = NO;

        [animationView stopAnimating];

    }

    

}

- (void)loadData

{

//    当调试的时候  打印数组的值 是nil或者null 意味着 咱们没有初始化数组对象  或者 有被赋值为nil或者null(数组对象就会被销毁) 的情况

    

    imageList = [NSMutableArray array];

    

    for (int i=1; i<=6; i++) {

        

        NSString *imageName = [NSString stringWithFormat:@"niao2-%d(被拖移).tiff",i];

        [imageList addObject:[UIImage imageNamed:imageName]];

        

    }

    

}

- (void)moveAnimation

{

    UIImage *image = [UIImage imageNamed:@"niao2-1(被拖移).tiff"];

//    Duration 动画持续的时间

//    [UIView animateWithDuration:5 animations:^{

//        animationView.frame = CGRectMake(200, 400, image.size.width*2, image.size.height*2);

//    }];

    

    

    [UIView animateWithDuration:5 animations:^{

        animationView.frame = CGRectMake(200, 400, image.size.width*2, image.size.height*2);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:5 animations:^{

            animationView.frame = CGRectMake(0, 0, image.size.width, image.size.height);

        }];

    }];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/wukun16/p/4883959.html