UIView简单动画
//UIKit集成的UIView上的动画,动画块
-(void)beginAndCommitAnimation
{
self.redView.frame = CGRectMake(100, 100, 100, 100);
//开始设置动画
//参数1.当前动画的标签
//参数2.上下文,如果为动画设置代理,context可以作为代理方法的参数
[UIView beginAnimations:@"move" context:@"移动"];
//设置动画时长
[UIView setAnimationDuration:5];
//设置重复次数
[UIView setAnimationRepeatCount:3];
//设置逆向效果
[UIView setAnimationRepeatAutoreverses:YES];
//动画延迟
[UIView setAnimationDelay:0.2];
//动画的速度曲线,缓慢开始,缓慢结束等
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//设置动画的代理
[UIView setAnimationDelegate:self];
//添加动画开始时执行的代理方法
[UIView setAnimationWillStartSelector:@selector(animationDidStart: context:)];
//动画结束后的代理方法
[UIView setAnimationDidStopSelector:@selector(animationDidStop:)];
//设置要改变的属性
CGRect rect = self.redView.frame;
rect.origin.x = 250;
rect.origin.y = 500;
rect.size.width = 50;
rect.size.height = 50;
self.redView.frame = rect;
_redView.alpha = 0.2;
_redView.backgroundColor = [UIColor blueColor];
//提交动画
[UIView commitAnimations];
}
#pragma mark -- 动画的代理方法
-(void)animationDidStart:(NSString *)animID context:(NSString*)context
{
NSLog(@"动画开始的代理--%@",context);
}
-(void)animationDidStop:(NSString *)animID
{
NSLog(@"动画结束--%@",animID);
}
block简单动画
//block动画
-(void)blockAnimation
{
[UIView animateWithDuration:2 animations:^{
//设置动画重复次数和可逆
[UIView setAnimationRepeatCount:5];
[UIView setAnimationRepeatAutoreverses:YES];
//要改变的属性
[self.redView setBackgroundColor:[UIColor greenColor]];
}];
}
block过渡动画
//transition fromView效果
-(void)tranFromViewAnimation
{
//两个界面的翻转效果
/*中间的效果是
[self.redView.supperView addSubView self.greenView];
[self.redView removeFromSuperView];
fromView的参数必须有父视图
*/
//从一个view到另一个view
[UIView transitionFromView:self.redView toView:self.greenView duration:2
options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
NSLog(@"block end");
}];
}
block关键帧动画
//一帧动画
-(void)keyFrameAnimation
{
//动画执行的总时长
//延时
//option动画效果
//animation帧动画的配置
//completion动画结束后
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
//每帧。。
//执行一次动画的时间为 总时长*relativeDuration
//startTime和relativeDuration的范围是0~1之间
//执行完要等待 总时长 才会才会执行下一次 ,在repeat状态下
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
self.redView.frame = CGRectMake(100, 400, 200, 200);
}];
} completion:^(BOOL finished) {
NSLog(@"block end");
}];
}
spring动画
//用spring可以实现弹簧效果或者果冻效果
-(void)springAnimation
{
//damp阻尼 0~1 越大阻尼越大
//velocity速率 速率越大 频率越快
[UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:2 options:UIViewAnimationOptionRepeat animations:^{
self.redView.frame = CGRectMake(50, 500, 200, 200);
} completion:nil];
}