1.從Bundle內加載圖片數組
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]];
2.用UIImageView作序列幀動畫app
[zhaoImage setAnimationImages:imageList]; [zhaoImage setAnimationDuration:1.0]; // [zhaoImage setAnimationRepeatCount:1]; [zhaoImage startAnimating];
3.UIButton設置標題文字、顏色ide
[button1 setTitle:@"別摸我" forState:UIControlStateNormal]; [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
4.UIButton添加點擊事件動畫
[button1 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchDown];
實例:Tom貓atom
@property (nonatomic, strong) NSMutableArray *cymbalArray;//銅片 @property (nonatomic, strong) NSMutableArray *fartArray;//放屁 @property (nonatomic, strong) NSMutableArray *eatArray;//吃 @property (nonatomic, strong) NSMutableArray *drinkArray;//喝 @property (nonatomic, strong) NSMutableArray *scratchArray;//抓 @property (nonatomic, strong) NSMutableArray *pieArray;//扔球 @property (nonatomic, strong) NSMutableArray *blinkArray;//眨眼 @property (nonatomic, strong) NSMutableArray *yawnArray;//哈欠 @property (nonatomic, strong) NSMutableArray *sneezeArray;//打噴嚏 @property (nonatomic, strong) NSMutableArray *listenArray;//聽 @property (nonatomic, strong) NSMutableArray *talkArray;//說 @property (nonatomic, strong) NSMutableArray *foot_leftArray;//左腳 @property (nonatomic, strong) NSMutableArray *foot_rightArray;//右腳 @property (nonatomic, strong) NSMutableArray *stomachArray;//胃 @property (nonatomic, strong) NSMutableArray *angryArray;//生氣 @property (nonatomic, strong) NSMutableArray *happyArray;//開心 @property (nonatomic, strong) NSDictionary *dict;//全局字典 @property (nonatomic, strong) NSMutableDictionary *somenewDict;//局部字典 @property (nonatomic, strong) NSMutableArray *somenewMutableArray;//局部可變數組 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString *path = [[NSBundle mainBundle] pathForResource:@"Tomcat" ofType:@"plist"]; NSURL *url = [NSURL fileURLWithPath:path]; self.dict = [NSDictionary dictionaryWithContentsOfURL:url]; NSLog(@"%@", url); self.somenewDict = [NSMutableDictionary dictionary]; self.somenewMutableArray = [NSMutableArray array]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)animationAction:(UIButton *)sender { if (![_bgImageView isAnimating]) { switch (sender.tag) { case KfartTag: [self setSomeAnimationWithAnimationName:@"fart"]; NSLog(@"haha"); break; case Kcymbal: [self setSomeAnimationWithAnimationName:@"cymbal"]; break; case Kscratch: [self setSomeAnimationWithAnimationName:@"scratch"]; break; case Keat: [self setSomeAnimationWithAnimationName:@"eat"]; break; case Kdrink: [self setSomeAnimationWithAnimationName:@"drink"]; break; case Kpie: [self setSomeAnimationWithAnimationName:@"pie"]; break; case Ksneeze: [self setSomeAnimationWithAnimationName:@"sneeze"]; break; case Khappy: [self setSomeAnimationWithAnimationName:@"happy"]; break; case Kangry: [self setSomeAnimationWithAnimationName:@"angry-tail"]; break; case Kfoot_left: [self setSomeAnimationWithAnimationName:@"foot_left"]; break; case Kfoot_right: [self setSomeAnimationWithAnimationName:@"foot_right"]; break; default: break; } } } - (void) setSomeAnimationWithAnimationName:(NSString *)animationName{ NSLog(@"haha2"); _somenewDict = _dict[animationName]; for (NSInteger x = 0; x < [_somenewDict[@"frames"] integerValue]; x++) { NSString *imageFile = [NSString stringWithFormat:_somenewDict[@"imageFormat"], x]; UIImage *image = [UIImage imageNamed:imageFile]; [_somenewMutableArray addObject:image]; } [_bgImageView setAnimationImages:_somenewMutableArray]; [_bgImageView setAnimationRepeatCount:1]; [_bgImageView setAnimationDuration:[_somenewDict[@"frames"] integerValue] / 10]; [_bgImageView startAnimating]; NSLog(@"haha3"); [_somenewMutableArray removeAllObjects]; } @end