UIView基础动画:
UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持。
UIView基础动画包括:UIView位置大小动画 UIView颜色动画 UIView透明度动画 UIView仿射翻转效果 UIView仿射旋转效果
改变位置的动画:
- (IBAction)changeFrame:(id)sender { // UIView动画 有开始beginAnimation 有结束commitAnimation // 第一步:开始UIView动画 [UIView beginAnimations:@"move" context:nil]; // 第二步:设置动画时长 [UIView setAnimationDuration:3]; // 第三步:设置UIView代理 [UIView setAnimationDelegate:self]; // 第四步:设置相关的对象的frame _textView.frame = CGRectMake(100, 100, 200, 100); // 第五步:结束动画 [UIView commitAnimations]; }
改变颜色的动画:
- (IBAction)changeColor:(id)sender { [UIView beginAnimations:@"color" context:nil]; [UIView setAnimationDuration:2]; [UIView setAnimationDelegate:self]; _textView.backgroundColor = [UIColor orangeColor]; [UIView commitAnimations]; }
改变透明度的动画:
- (IBAction)changeAlpha:(id)sender { [UIView beginAnimations:@"alpha" context:nil]; [UIView setAnimationDuration:5]; [UIView setAnimationDelegate:self]; _textView.alpha = 0.2; [UIView commitAnimations]; }
仿射翻转效果的动画:
- (IBAction)rotationAction:(id)sender { // 第一步:开始动画 [UIView beginAnimations:@"rotation" context:nil]; // 第二步:设置时长 [UIView setAnimationDuration:6.0f]; // 第三步:设置淡入的效果 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 第四步:设置代理 [UIView setAnimationDelegate:self]; // 第五步:设置翻转方向 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_textView cache:YES]; // 第六步:提交 [UIView commitAnimations]; }仿射旋转效果的动画:
- (IBAction)transfromAction:(id)sender { [UIView beginAnimations:@"transform" context:nil]; [UIView setAnimationDuration:3]; [UIView setAnimationDelegate:self]; // 要进行旋转所以需要设置旋转角度 CGAffineTransform trandform = CGAffineTransformMakeRotation(3 * M_PI); // 设置旋转角度的对象 [_textView setTransform:trandform]; [UIView commitAnimations]; }
UIView实现动画效果的时候回调用代理方法,但是不需要遵循代理协议
//开始动画的方法 - (void)animationWillStart:(NSString *)animationID context:(void *)context { NSLog(@"ID = %@ context = %@", animationID, context); } //结束动画的方法 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { NSLog(@"ID = %@ context = %@", animationID, context); }
UIView的Block动画
1、简单的block动画
- (IBAction)easyBlockAnimation:(id)sender { // 第一个参数:设置动画时长 // 第二个参数:动画要显示的效果 __weak typeof(self)weakSelf = self; // [UIView animateWithDuration:3 animations:^{ //// 改变imageView的Center位置 // weakSelf.playImageView.center = self.view.center; // }]; // 带完成事件的 // 第一个参数:设置动画时长 // 第二个参数:动画要显示的效果 // 第三个参数:动画完成时进行的时间 [UIView animateWithDuration:3 animations:^{ weakSelf.playImageView.center = weakSelf.view.center; } completion:^(BOOL finished) { NSLog(@"位置改变了"); }]; }
2、复杂的block动画
- (IBAction)complexBlockAnimation:(id)sender { // 参数一:时长 // 参数二:动画的延迟时间 // 参数三:动画的枚举值 // 参数四:要实现的动画效果 // 参数五:动画完成的时候要做的事情 __weak typeof(self)weakSelf = self; [UIView animateWithDuration:4 delay:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{ weakSelf.playImageView.frame = CGRectMake(10, 100, 100, 100); } completion:^(BOOL finished) { NSLog(@"变小"); }]; }
3、关键帧动画
- (IBAction)keyFramesAnimation:(id)sender { // 参数1:动画时长 // 参数2:延迟时间 // 参数3:枚举值动画效果 // 参数4:开始动画 __weak typeof(self)weakSelf = self; [UIView animateKeyframesWithDuration:5 delay:1 options:UIViewKeyframeAnimationOptionRepeat animations:^{ // 在这个里边需要添加一个方法,即创建block的关键帧 // 参数1:帧动画的开始时间 // 参数2:帧动画的持续时间 // 参数3:真正想做的事 [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{ // 在这个里边实现真正想要实现的效果 weakSelf.playImageView.center = weakSelf.view.center; }]; } completion:^(BOOL finished) { NSLog(@"改变中心点"); }]; }
UIView的Spring动画
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.title = @"spring动画"; // 参数1:持续时间 // 参数2:延迟时间 // 参数3:类似弹簧的效果值0-1 // 参数4:初始化spring的一个速度 // 参数5:枚举值 // 参数6:开始动画 // 参数7:动画完成 __weak typeof(self) weakSelf = self; [UIView animateWithDuration:3 delay:0.1 usingSpringWithDamping:1 initialSpringVelocity:10 options:UIViewAnimationOptionOverrideInheritedCurve animations:^{ weakSelf.imageView.center = self.view.center; } completion:^(BOOL finished) { NSLog(@"GD擦浪嘿"); }]; }