UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(10,30, 100, 100)];
view.backgroundColor = [UIColoryellowColor];
[self.viewaddSubview:view];
/**
* iOS中的动画有两大类
* 1.UIView的试图动画
* 2.Layer的动画
* UIView的动画也是基于Layer的动画
*/
[UIViewbeginAnimations:@"ddd"context:nil]; // 设置动画 ddd为动画名称
[UIViewsetAnimationDuration:3.0f];// 定义动画持续时间
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut]; // 定义动画加速减速方式
[UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromLeftforView:view cache:YES]; // 设置动画的样式 forView为哪个view实现这个动画效果
[UIViewsetAnimationDelay:1.0f]; // 设置动画延迟多久执行
[UIViewsetAnimationDelegate:self];// 设置动画的代理,实现动画执行前后的方法 在commitAnimation之前设置
[UIViewsetAnimationDidStopSelector:@selector(stop)]; // 设置动画结束后执行的方法
[UIViewsetAnimationWillStartSelector:@selector(star)];// 设置动画将要开始执行的方法
// view.frame = CGRectMake(10, 30, 200, 200);
// view.transform = CGAffineTransformMakeTranslation(10, 10); // 设置偏移量 相对于最初的 只能偏移一次
// view.transform = CGAffineTransformTranslate(view.transform, 10, 10); // 设置偏移量 可以偏移多次
// view.transform = CGAffineTransformMakeRotation(M_PI / 4.0); // 设置旋转度 只能旋转一次
// view.transform = CGAffineTransformRotate(view.transform, M_PI / 2.0f); // 设置旋转度 可以旋转多次
// view.transform = CGAffineTransformMakeScale(2.0f, 2.0f); // 设置大小 只能改变一次 数值相对于本来的几倍
// view.transform = CGAffineTransformScale(view.transform, 1.1f, 1.1f); // 设置大小 可以改变多次
// view.transform = CGAffineTransformIdentity; // 回到当初的样子 执行一次
// view.transform = CGAffineTransformInvert(view.transform); // 得到相反的样子 大小 方向 位置执行多次
[UIViewcommitAnimations]; // 提交动画
// Block方式
[UIViewanimateWithDuration:3.0fanimations:^{
// 这里相当于在begin和commit之间
}completion:^(BOOL finished) {
//这里相当于动画执行完成后要执行的方法,可以继续嵌套block
}];
}
/**
*typedef enum {
*UIViewAnimationTransitionNone, //普通状态
*UIViewAnimationTransitionFlipFromLeft, //从左往右翻转
*UIViewAnimationTransitionFlipFromRight, //从右往左翻转
*UIViewAnimationTransitionCurlUp, //向上翻页
*UIViewAnimationTransitionCurlDown, //向下翻页
*} UIViewAnimationTransition;
*typedef enum {
*UIViewAnimationCurveEaseInOut,
*UIViewAnimationCurveEaseIn,
*UIViewAnimationCurveEaseOut,
*UIViewAnimationCurveLinear
*} UIViewAnimationCurve;
*/
- (void)stop
{
NSLog(@"Animate stop");
}
- (void)star
{
NSLog(@"Animate start");
}
UIView *view = [[UIView alloc] init];
view.bounds = CGRectMake(0, 0, 100, 200);
view.center = CGPointMake(160, 200);
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
// CAAnimation有多个子类
#if 0
// CABasicAnimation
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
// @""里的字符串有多种,一定要填对,动画才会执行opacity设置透明度 bounds.size设置大小
[animation setFromValue:[NSNumber numberWithFloat:1.0f]]; // 设置透明度从几开始
[animation setToValue:[NSNumber numberWithFloat:0.3f]]; // 设置透明度到几结束
[animation setDuration:1.0f]; // 设置动画时间
[animation setRepeatCount:5]; // 设置重复次数
[animation setRepeatDuration:5.0f]; // 设置限制重复次数
[animation setAutoreverses:NO]; // 设置是否从1.0到0.3再从0.3到1.0为一次 如果设置为NO则1.0到0.3为一次
[animation setRemovedOnCompletion:YES]; // 完成时移除动画 默认也是
[view.layer addAnimation:animation forKey:@"abc"]; // 执行动画
#elif 0
// CAKeyframeAnimation
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // 设置view从初始位置经过一系列点
NSArray *positionArray = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(100, 20)], // center
[NSValue valueWithCGPoint:CGPointMake(40, 80)],
[NSValue valueWithCGPoint:CGPointMake(30, 60)],
[NSValue valueWithCGPoint:CGPointMake(20, 40)],
[NSValue valueWithCGPoint:CGPointMake(0, 100)], nil]; // 设置点
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.3f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:0.6f],
[NSNumber numberWithFloat:0.1f],
[NSNumber numberWithFloat:1.0f], nil]; // 设置移动过程的时间
[animation setKeyTimes:times];
[animation setValues:positionArray];
[animation setDuration:5.0f]; // 设置动画时间
[view.layer addAnimation:animation forKey:@"dd"]; // 执行动画
#elif 1
// CATransition
CATransition *animation = [CATransition animation];
animation.duration = 5.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeRemoved;
/*
kCATransitionFade;
kCATransitionMoveIn;
kCATransitionPush;
kCATransitionReveal;
*/
/*
kCATransitionFromRight;
kCATransitionFromLeft;
kCATransitionFromTop;
kCATransitionFromBottom;
*/
animation.type = kCATransitionMoveIn;
animation.subtype = kCATransitionFromBottom;
[view.layer addAnimation:animation forKey:@"dd"]; // 执行动画
// type 也可以直接用字符串
/*
cube
suckEffect 卷走
oglFlip 翻转
rippleEffect 水波
pageCurl 翻页
pageUnCurl
cameraIrisHollowOpen
cameraIrisHollowClose
*/
#endif