ViewController的 .h文件數組
#import <UIKit/UIKit.h>ide
@interface ViewController : UIViewController字體
@property (strong,nonatomic) UIImageView *myImageView0;動畫
@property (strong,nonatomic) UIButton *myButton;atom
@property (strong,nonatomic) NSArray *myArray;orm
@property (strong,nonatomic) NSTimer *myTimer;圖片
@endget
//ViewController的 .m文件animation
#import "ViewController.h"string
@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