简单动画的制作,分别用UIImageView的属性animationgImage和定时器NSTimer制作

ViewController的 .h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

 

@property (strong,nonatomic) UIImageView *myImageView0;

@property (strong,nonatomic) UIButton *myButton;

@property (strong,nonatomic) NSArray *myArray;

@property (strong,nonatomic) NSTimer *myTimer;

@end

 

//ViewController的 .m文件

#import "ViewController.h"

 @interface ViewController ()

 

@end

 

@implementation ViewController

//定义两个全局变量

int numble=0

int num=0

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

self.view.background.Color=[UIColor lightGrayColor];

//创建一个播放动画的视图

    self.myImageView0=[[UIImageView alloc]initWithFrame:CGRectMake(65, 50, 280, 280)];

        [self.view addSubview:self.myImageView0];

    

    //创建一个按钮,控制动画的播放和停止

    self.myButton=[[UIButton alloc]initWithFrame:CGRectMake(160, 350, 80, 40)];

//改变按钮视图的颜色

    self.myButton.backgroundColor=[UIColor redColor];

//改变按钮中字体的颜色

    [self.myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//给按钮Button添加标题

    [self.myButton setTitle:@"停止" forState:UIControlStateNormal];

//创建一个储存图画或动画序列帧的数组

//将所有的图画存入数组中

    self.myArray=[NSArray arrayWithObjects:[UIImage imageNamed:@"0.png"],[UIImage imageNamed:@"1.png"],[UIImage imageNamed:@"2.png"],[UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"4.png"],[UIImage imageNamed:@"5.png"],[UIImage imageNamed:@"6.png"],[UIImage imageNamed:@"7.png"],[UIImage imageNamed:@"8.png"],[UIImage imageNamed:@"9.png"],[UIImage imageNamed:@"10.png"],[UIImage imageNamed:@"11.png"],[UIImage imageNamed:@"12.png"],[UIImage imageNamed:@"13.png"],[UIImage imageNamed:@"14.png"], nil];

 

//用UIImageView的属性animationImages

//点击按钮触发是调用方法 suspend

[self.myButton addTarget:self action:@selector(suspend) forControlEvents:UIControlEventTouchUpInside];

//将(序列帧)图画数组赋给UIImageView的animationImages属性

    self.myImageView0.animationImages=self.myArray;

    //设置播放动画时间,一次动画播放完的时间

    self.myImageView0.animationDuration=3;

    //设置播放次数,次动画循环播放了多少次

    self.myImageView0.animationRepeatCount=10;

    //开始播放动画

    [self.myImageView0 startAnimating];

 

 

//用定时器NSTimer

//定时器,控制调用方法setNextImage,播放动画

    self.myTimer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(setNextImage) userInfo:nil repeats:YES];

    //当按钮Button被触发时调用suspend方法控制定时器的开与关

    [self.myButton addTarget:self action:@selector(suspend) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.myButton];

 

}

 

//按钮控制图画的停止、播放方法

-(void)suspend

   {

       //用UIImageView的属性animationImages   

    if (numble%2==0) {

        [self.myButton setTitle:@"开始" forState:UIControlStateNormal];

//动画的停止

        [self.myImageView0 stopAnimating ];

    } else {

        [self.myButton setTitle:@"停止" forState:UIControlStateNormal];

//动画的播放

        [self.myImageView0 startAnimating ];

    }

    numble++;

 

 

//定时器

if (num%2==0) {

           //关闭定时器

           [self.myTimer setFireDate:[NSDate distantFuture]];

       } else {

           //开始定时器

           [self.myTimer setFireDate:[NSDate distantPast]];

       }

       num++;

}

 

//定时器控制的图片、动画的播放的方法

-(void)setNextImage

{

//numble%15中的15是图片的数量,也就是帧数,你有多少张图片或者多少帧图片,15就写成多少,你也可以设成一个变量,上面的集合也可设成可变的,集合有多少成员就是多少

    self.myImageView0.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i.png",numble%15]];

    numble++;

}

@end

 

posted @ 2016-03-14 22:11  A唯爱永生_罗韩  阅读(253)  评论(0编辑  收藏  举报