1.UIImage Animation图片动画
#define FPS 30.0 - (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage animatedImageNamed: @"ship-anim" duration:5*1/FPS]; self.imageView.image = image; }
2.NSTimer 计时器实现动画的方式
2.1基本方法:
#define FPS 30.0 - (void)viewDidLoad { [super viewDidLoad]; self.count = 0; self.imageView.alpha = 0; //使用1/FPS计算出每一帧花费多长时间 [NSTimer scheduledTimerWithTimeInterval:1/FPS target:self selector:@selector(changeAlpha:) userInfo:nil repeats:YES]; } -(void)changeAlpha:(NSTimer *)timer{ self.count++; //计算出每一帧需要修改的alpha值 self.imageView.alpha+=1/(FPS*5); //根据完成动画的所有帧数决定 //停止计时器 if(self.count==FPS*5){ [timer invalidate]; NSLog(@"....."); } }
2.2NStimer Animation 飞机飞行
#define FPS 30.0// 帧率 #define DURATION 2.0 // 动画时长 - (void)viewDidLoad { [super viewDidLoad]; self.startCenter = self.imageView.center; self.count = 0; } - (IBAction)start:(UIButton *)sender { [NSTimer scheduledTimerWithTimeInterval:1/FPS target:self selector:@selector(move:) userInfo:nil repeats:YES]; } //每一帧都来执行move方法,修改图片的中心点 //匀速动画:当前值 = 开始值+当前帧数*(结束值-开始值)/(帧率*动画时长) -(void)move:(NSTimer *)timer{ self.count++; CGPoint center = self.imageView.center; center.y = self.startCenter.y + self.count*(70-self.startCenter.y)/(FPS*DURATION); self.imageView.center = center; if(self.count >= FPS*DURATION){ [timer invalidate]; } }
2.3变速公式
- (IBAction)start:(id)sender { [NSTimer scheduledTimerWithTimeInterval:1/FPS target:self selector:@selector(move:) userInfo:nil repeats:YES]; } //变速公式: //当前值= 上一次的值+(目标值-上一次的值)*渐进因子 -(void)move:(NSTimer *)timer{ CGPoint center = self.imageView.center; center.y = center.y+(70-center.y)*0.08; self.imageView.center = center; if(center.y<=70){ [timer invalidate]; } }
3.UIView Aniamtion
3.1基本方法
- (IBAction)start:(id)sender { CGPoint center = self.imageView.center; center.y = 70; [UIView animateWithDuration:3 animations:^{ //此处只要设定要被添加动画的视图 动画结束时的状态是什么 self.imageView.center = center; }]; }
3.2其他方法
- (void)viewDidLoad { [super viewDidLoad]; //先记录此刻摆放的位置,作为 //动画结束时的位置 CGRect labelEndFrame = self.label.frame; CGRect imageViewEndFrame = self.imageView.frame; //设置动画前label的起始位置 CGRect labelStartFrame = labelEndFrame; labelStartFrame.origin.x = -labelStartFrame.size.width; self.label.frame = labelStartFrame; CGRect imageViewStartFrame = imageViewEndFrame; imageViewStartFrame.origin.y = self.view.bounds.size.height; self.imageView.frame = imageViewStartFrame; //添加动画 [UIView animateWithDuration:2 animations:^{ //设置两个需要做动画的视图的结束位置 self.label.frame = labelEndFrame; self.imageView.frame = imageViewEndFrame; }]; } - (IBAction)move:(id)sender { CGPoint center = self.imageView.center; center.y -= 300; CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI); [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{ self.imageView.center = center; self.imageView.transform = transform; } completion:nil]; }
时间: 2024-12-09 17:46:21