使用前
需引入QuartzCore
.framework
, 并在相关文件中加入 #import "QuartzCore/QuartzCore
.h
"
定义
shakeFeedbackOverlay为UIImageView
设置
self
.shakeFeedbackOverlay
.alpha
=
0
.0
;
self
.shakeFeedbackOverlay
.layer
.cornerRadius
=
1
0
.0
;
1
、图像左右抖动
CABasicAnimation* shake = [
CABasicAnimation
animationWithKeyPath
:
@"transform.rotation.z"
];
shake
.fromValue
= [
NSNumber
numberWithFloat
:-M_PI/
3
2
];
shake
.toValue
= [
NSNumber
numberWithFloat
:+M_PI/
3
2
];
shake
.duration
=
0
.1
;
shake
.autoreverses
=
YES
;
shake
.repeatCount
=
4
;
[
self
.shakeFeedbackOverlay
.layer
addAnimation
:shake
forKey
:
@"shakeAnimation"
];
self
.shakeFeedbackOverlay
.alpha
=
1
.0
;
[
UIView
animateWithDuration
:
2
.0
delay
:
0
.0
options
:
UIViewAnimationOptionCurveEaseIn
|
UIViewAnimationOptionAllowUserInteraction
animations
:^{
self
.shakeFeedbackOverlay
.alpha
=
0
.0
;
2
、图像顺时针旋转
CABasicAnimation* shake = [
CABasicAnimation
animationWithKeyPath
:
@"transform.rotation.z"
];
shake
.fromValue
= [
NSNumber
numberWithFloat
:
0
];
shake
.toValue
= [
NSNumber
numberWithFloat
:
2
*M_PI];
shake
.duration
=
0
.8
; shake
.autoreverses
=
NO
;
shake
.repeatCount
=
1
;
[
self
.shakeFeedbackOverlay
.layer
addAnimation
:shake
forKey
:
@"shakeAnimation"
];
self
.shakeFeedbackOverlay
.alpha
=
1
.0
;
[
UIView
animateWithDuration
:
1
0
.0
delay
:
0
.0
options
:
UIViewAnimationOptionCurveEaseIn
|
UIViewAnimationOptionAllowUserInteraction
animations
:^{
self
.shakeFeedbackOverlay
.alpha
=
0
.0
; }
completion
:nil
];
3
、图像关键帧动画
CAKeyframeAnimation
*animation = [
CAKeyframeAnimation
animation
];
CGMutablePathRef
aPath = CGPathCreateMutable();
CGPathMoveToPoint(aPath,
nil
,
2
0
,
2
0
);
CGPathAddCurveToPoint(aPath,
nil
,
1
6
0
,
3
0
,
2
2
0
,
2
2
0
,
2
4
0
,
4
2
0
);
animation
.path
= aPath;
animation
.autoreverses
=
YES
;
animation
.duration
=
2
;
animation
.timingFunction
= [
CAMediaTimingFunction
functionWithName
:kCAMediaTimingFunctionEaseOut];
animation
.rotationMode
=
@"auto"
;
[ballView
.layer
addAnimation
:animation
forKey
:
@"position"
];
4
、组合动画
CAAnimationGroup
CABasicAnimation
*flip = [
CABasicAnimation
animationWithKeyPath
:
@"transform.rotation.y"
];
flip
.toValue
= [NSNumbernumberWithDouble:-M_PI];
CABasicAnimation
*scale= [
CABasicAnimation
animationWithKeyPath
:
@"transform.scale"
];
scale
.toValue
= [NSNumbernumberWithDouble:
1
2
];
scale
.duration
=
1
.5
;
scale
.autoreverses
=
YES
;
CAAnimationGroup
*group = [
CAAnimationGroup
animation
];
group
.animations
= [
NSArray
arrayWithObjects
:flip, scale,
nil
];
group
.timingFunction
= [
CAMediaTimingFunction
functionWithName
:kCAMediaTimingFunctionEaseInEaseOut];
group
.duration
=
3
;
group
.fillMode
= kCAFillModeForwards;
group
.removedOnCompletion
=
NO
;
[ballView
.layer
addAnimation
:group
forKey
:
@"position"
];
5
、指定时间内旋转图片
- (
void
)startRotate
{
self
.rotateTimer
= [
NSTimer
scheduledTimerWithTimeInterval
:
0
.02
target
:
self
selector
:
@selector
(rotateGraduation)
userInfo
:nil
repeats
:
YES
];
}
- (
void
)stopTimer
{
if
([
self
.rotateTimerisValid
])
{
[
self
.rotateTimerinvalidate
];
self
.rotateTimer
=
nil
;
}
}
- (
void
)rotateGraduation
{
self
.timeCount
--;
if
(
self
.timeCount
==
0
)
{
[
self
stopTimer
];
self
.timeCount
=
2
5
;
}
else
{
static
CGFloat
radian =
150
* (
M_2_PI
/
3
6
0
);
CGAffineTransform
transformTmp =
self
.lightImageView
.transform
;
transformTmp = CGAffineTransformRotate(transformTmp, radian);
self
.lightImageView
.transform
= transformTmp;
};
}
调用方法
self
.timeCount
=
2
5
;
[
self
startRotate
];