1 建立动画块
//请求标志着动画块的开始
[UIViewbeginAnimations:@"动画回调" context:nil];
//定义动画加速和减速方式,
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
UIViewAnimationCurveEaseInOut, // slow at beginning and end//淡入淡出
UIViewAnimationCurveEaseIn, // slow at beginning//淡入
UIViewAnimationCurveEaseOut, // slow at end//淡出
UIViewAnimationCurveLinear //线性
};
//动画的执行时间
[UIViewsetAnimationDuration:1.5f];
//设置代理
[UIViewsetAnimationDelegate:self];
//animationDidStop:finished:context:带两个参数,这两个参数在回调函数中作为函数参数传入
[UIViewsetAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
//视图的过度,过渡效果UIKit封装了四种,分别是左旋转,右旋转,向上翻页,向下翻页
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlDownforView:imageViewcache:YES];
//标志着动画块的结束
[UIViewcommitAnimations];
#pragma -mark 动画结束实行 的方法
- (void)animationDidStop:(NSString *)name finished:(NSString *)finished context:(NSString *)context
简单说一下这个方法,其中的finished,如果动画正常结束时,它的值为1,否则为0;;在为了区分动画是否结束时有很大的作用
2 使用Core Animation Transittion
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//touch 事件中 获取点击的view
UITouch *touch = [touches anyObject];
UIView *view = touch.view;
//创建一个CATransaction对象并设置代理,动画时长和动画加速减速
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 2.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
//设置动画模式
animation.type = kCATransitionPush;
//动画模式有以下四种
/* Common transition types. */
// NSString * const kCATransitionFade
// NSString * const kCATransitionMoveIn
// NSString * const kCATransitionPush
// NSString * const kCATransitionReveal
//设置动画的方向
animation.subtype = kCATransitionFromBottom;
/* Common transition subtypes. */
// NSString * const kCATransitionFromRight
// NSString * const kCATransitionFromLeft
// NSString * const kCATransitionFromTop
// NSString * const kCATransitionFromBottom
[view.layer addAnimation:animation forKey:@"move in"];
}
---翻页动画
//翻页的动画
[UIView beginAnimations:@"动画回调" context:imageView];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:imageView cache:YES];
/*
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
*/
[UIView commitAnimations];
---旋转动画
//旋转的动画,本工程用xib创建界面,要去掉Use AutoLayout...不然每一次旋转都会自动变化得不到想要的结果
CGAffineTransform transform = CGAffineTransformRotate(imageView.transform, M_PI/4);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[imageView setTransform:transform];
[UIView commitAnimations];
还有一些角度的宏定义
/* Even though these might be more useful as long doubles, POSIX requires
that they be double-precision literals. */
#define M_E 2.71828182845904523536028747135266250 /* e */
#define M_LOG2E 1.44269504088896340735992468100189214 /* log2(e) */
#define M_LOG10E 0.434294481903251827651128918916605082 /* log10(e) */
#define M_LN2 0.693147180559945309417232121458176568 /* loge(2) */
#define M_LN10 2.30258509299404568401799145468436421 /* loge(10) */
#define M_PI 3.14159265358979323846264338327950288 /* pi */
#define M_PI_2 1.57079632679489661923132169163975144 /* pi/2 */
#define M_PI_4 0.785398163397448309615660845819875721 /* pi/4 */
#define M_1_PI 0.318309886183790671537767526745028724 /* 1/pi */
#define M_2_PI 0.636619772367581343075535053490057448 /* 2/pi */
#define M_2_SQRTPI 1.12837916709551257389615890312154517 /* 2/sqrt(pi) */
#define M_SQRT2 1.41421356237309504880168872420969808 /* sqrt(2) */
#define M_SQRT1_2 0.707106781186547524400844362104849039 /* 1/sqrt(2) */
几个动画,淡入淡出,翻转,移动(按照自己的坐标)
case danru:
{
CABasicAnimation *anmi = [CABasicAnimation animationWithKeyPath:@"opacity"];
//"opacity"是一个系统的key,关键字
[anmi setFromValue:[NSNumber numberWithFloat:1.0]];//初始值
[anmi setFromValue:[NSNumber numberWithFloat:0.1]];//结束值
[anmi setDuration:2];//时间
[anmi setRepeatCount:2];//重复两次
[anmi setAutoreverses:YES];//让淡入淡出的效果在模糊状态上,不立刻变得清晰
[imageView.layer addAnimation:anmi forKey:@"这个key"];
//要把这个动画 添加到layer层,,(如果想在一个动画结束后再继续别的动画,这个key就起作用)
}
case overturn:
{
CABasicAnimation *anmi = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
//bounds.size 也是一个系统key
[anmi setFromValue:[NSValue valueWithCGSize:CGSizeMake(-207, -305)]];
//FromValue,,1CGSizeMake里的参数 为负数的时候 才会翻转..第一个是左右,第二个是上下
[anmi setToValue:[NSValue valueWithCGSize:imageView.bounds.size]];
[anmi setDuration:10];
//[anmi setRemovedOnCompletion:YES];//动画结束后从图层移除这个动画
[imageView.layer addAnimation:anmi forKey:@"qqq"];
break;
}
case move3:
{
CAKeyframeAnimation *anmi = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSArray *point = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(100, 20)],[NSValue valueWithCGPoint:CGPointMake(10, 200)],[NSValue valueWithCGPoint:CGPointMake(250, 150)], nil];
[anmi setValues:point];
[anmi setDuration:3];
[imageView.layer addAnimation:anmi forKey:@"222"];
break;
}