在看iOS6应用开发时,介绍了一种播放动画的效果,其实就是一组数组,里面以此存储了很多张图片,相当于GIF图片的每一帧动画,然后在规定的时间内把他们都播放出来,这样就相当于实现动画了;后来又发现可以用另一种方式实现动画,就是利用Timer,然后再设置一个计数器,根据计数器的不同变化,然后实时改变控件的位置。
直接上代码:
#import "ViewController.h" @interface ViewController (){ int _episodeNumber; //single episode's number int _count; //for the timer; int _amountOfEpisode; //totle number of episodes int hammer_hit_number; //record how many times we have hit the hammer //parametors for the episode 2 hammer: float finalorigin_y; float finalorigin_x; int _currentCount; NSTimer* timer; NSArray* hopAnimation; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //set the button's style [self.goButton.layer setCornerRadius:10.0]; [self.nextButton.layer setCornerRadius:10.0]; hopAnimation = @[ [UIImage imageNamed:@"final_picture_1.png"], [UIImage imageNamed:@"final_picture_2.png"], [UIImage imageNamed:@"final_picture_3.png"], [UIImage imageNamed:@"final_picture_4.png"], [UIImage imageNamed:@"final_picture_5.png"], [UIImage imageNamed:@"final_picture_6.png"], [UIImage imageNamed:@"final_picture_7.png"], [UIImage imageNamed:@"final_picture_8.png"], [UIImage imageNamed:@"final_picture_9.png"], [UIImage imageNamed:@"final_picture_10.png"], [UIImage imageNamed:@"final_picture_11.png"], [UIImage imageNamed:@"final_picture_12.png"], [UIImage imageNamed:@"final_picture_13.png"], [UIImage imageNamed:@"final_picture_14.png"], [UIImage imageNamed:@"final_picture_15.png"], [UIImage imageNamed:@"final_picture_16.png"], [UIImage imageNamed:@"final_picture_17.png"], [UIImage imageNamed:@"final_picture_18.png"], [UIImage imageNamed:@"final_picture_19.png"], [UIImage imageNamed:@"final_picture_20.png"], ]; _amountOfEpisode = 3; // _episodeNumber = rand()%_amountOfEpisode; _episodeNumber = 0; self.titleLabel.frame = CGRectMake(40, 27, 240, 60); self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; // self.titleLabel.numberOfLines = 0; [self refresh]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)changeState{ switch (_episodeNumber) { case 0: if (_count>7) { [timer invalidate]; timer = nil; // [self refresh]; } else { _count++; } switch (_count) { case 0: break; case 1: [self.gogogo_label setHidden:YES]; [self.readyState setHidden:YES]; [self.ongoingState setHidden:NO]; [self.lala_label setHidden:NO]; break; case 2: [self.lala_label setHidden:YES]; [self.keai_label setHidden:NO]; self.ongoingState.frame = CGRectMake(133, 162, 65, 58); break; case 3: [self.ongoingState setHidden:YES]; [self.keai_label setHidden:YES]; [self.finalState setHidden:NO]; break; default: [self.aoh_label setHidden:NO]; if (_count==4) { self.aoh_label.text = @"AH OH!!!"; }else if(_count == 5){ self.aoh_label.text = @"OH!!!"; }else if(_count == 6){ self.aoh_label.text = @"H!!!"; }else{ self.aoh_label.text = @"!!!"; } self.aoh_label.frame = CGRectMake(190, 67+(_count-4)*70, 59, 21); self.finalState.frame = CGRectMake(227, 84+(_count-4)*70, 68, 58); break; } break; case 1://rotate if (_count>6) { [timer invalidate]; timer = nil; // [self refresh]; [self.rotateView stopAnimating]; self.finalState.frame = CGRectMake(227, 351, 68, 58); [self.finalState setHidden:NO]; } else { _count++; switch (_count) { case 0: break; case 1: [self.gogogo_label setHidden:YES]; [self.readyState setHidden:YES]; [self.ongoingState setHidden:NO]; [self.lala_label setHidden:NO]; break; case 2: [self.lala_label setHidden:YES]; [self.keai_label setHidden:NO]; self.ongoingState.frame = CGRectMake(133, 162, 65, 58); break; case 3: [self.ongoingState setHidden:YES]; [self.keai_label setHidden:YES]; [self.finalState setHidden:NO]; break; case 4: [self.finalState setHidden:YES]; [self.aoh_label setHidden:NO]; [self.rotateView setHidden:NO]; [self.rotateView startAnimating]; break; default: if (_count == 6) { self.rotateView.frame = CGRectMake(227, 74+167.5, 68, 167.5); } break; } } break; case 2://hammer if (_count>500) { [timer invalidate]; timer = nil; [self refresh]; } else { _count++; [self.readyState setHidden:YES]; [self.ongoingState setHidden:NO]; if (hammer_hit_number<20&&hammer_hit_number>=0) { self.ongoingState.frame = CGRectMake(10+_count, 351-_count, 65, 58); self.hammerButton.frame = CGRectMake(40+_count, 330-_count, 80, 60); self.gogogo_label.frame = CGRectMake(-50+_count, 320-_count, 100, 40); if (hammer_hit_number < 3&&hammer_hit_number>=1) { self.gogogo_label.text = @"啊!好疼!"; } else if (hammer_hit_number<5&&hammer_hit_number>=3) { self.gogogo_label.text = @"还行,能抗住"; } else if(hammer_hit_number<10&&hammer_hit_number>=5){ self.gogogo_label.text = @"快扛不住了。。。"; } else if(hammer_hit_number>=10&&hammer_hit_number<20){ self.gogogo_label.text = @"你干嘛总是敲我"; } }else if(hammer_hit_number==20) { _currentCount = _count; self.gogogo_label.text = @"我。。不。。行。。了"; finalorigin_y = self.ongoingState.frame.origin.y; finalorigin_x = self.ongoingState.frame.origin.x; NSLog(@"x:%1.2f,y:%1.2f",finalorigin_x,finalorigin_y ); }else { if (hammer_hit_number>25) { self.gogogo_label.text = @"你是不是有瘾!!!!"; }else{ self.gogogo_label.text = @"都不行了还敲!"; } self.ongoingState.frame = CGRectMake(finalorigin_x, finalorigin_y+(_count-_currentCount), 65, 58); NSLog(@"button_X:%1.2f, button_Y:%1.2f",self.ongoingState.frame.origin.x,self.ongoingState.frame.origin.y); self.gogogo_label.frame = CGRectMake(finalorigin_x-70, finalorigin_y+(_count-_currentCount)-30, 100, 40); self.hammerButton.frame = CGRectMake(finalorigin_x+30, finalorigin_y+(_count-_currentCount)-20, 80, 60); } if (self.ongoingState.frame.origin.y>350) { [timer invalidate]; timer = nil; } } break; default: break; } } - (IBAction)go:(id)sender { [self refresh]; switch (_episodeNumber) { case 0: if ([self.goButton.titleLabel.text isEqualToString:@"Go"]) { if (_count == 0) { timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeState) userInfo:nil repeats:YES]; [self.gogogo_label setHidden:NO]; } } else { [self.goButton setTitle:@"Go" forState:UIControlStateNormal]; [timer invalidate]; _count = 0; timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeState) userInfo:nil repeats:YES]; } break; case 1: timer = [NSTimer scheduledTimerWithTimeInterval:1.01 target:self selector:@selector(changeState) userInfo:nil repeats:YES]; [self.gogogo_label setHidden:NO]; break; case 2: timer = [NSTimer scheduledTimerWithTimeInterval:0.06 target:self selector:@selector(changeState) userInfo:nil repeats:YES]; [self.gogogo_label setHidden:NO]; break; default: break; } }
3中场景,case0中是利用timer根据_count的数值变化,实时改变按钮的形态,从而实现动态效果。
case1其实就是类似于播放GIF图片一样,实现PS了20张图片,连续播放就是一组GIF图片了,然后事前在animation数组中将其存储好,然后利用下面的函数
self.rotateView.animationImages = hopAnimation; self.rotateView.animationDuration = 2;
设置动画播放的间隔,这里我们设定2秒之内播放完20帧图片。如果不设置,默认是每秒30帧图片。
case2就相对复杂一点了,涉及到了用户交互,根据用户点击按钮的次数,改变另一个按钮的运行轨迹。但是总的来说,和case0的思路是一样的,只不过逻辑关系要复杂一点,但总的来说依旧不难。
时间: 2024-10-29 04:13:59