@interface ViewController ()
@property (nonatomic,strong) UIImageView *imageView
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
[self.view addSubview:self.imageView]
}
- (UIImageView *)imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]
_imageView.layer.cornerRadius = 100
_imageView.layer.masksToBounds = YES
_imageView.center = self.view.center
_imageView.image = [UIImage imageNamed:@"zrx4.jpg"]
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]
animation.duration = 10
animation.fromValue = [NSNumber numberWithFloat:0.f]
animation.toValue = [NSNumber numberWithFloat: M_PI *2]
animation.autoreverses = NO
animation.fillMode = kCAFillModeForwards
animation.repeatCount = MAXFLOAT
[_imageView.layer addAnimation:animation forKey:nil]
}
return _imageView
}
@end

@interface ViewController ()
@property (nonatomic,strong) UIImageView *imageView
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
[self.view addSubview:self.imageView]
// [self setUpAlphaAnimation]
// [self setUpSharkAnimation]
// [self setUpCAKeyframeAnimationUseValues]
// [self setUpCAKeyframeAnimationUsePath]
// [self setUpCAKeyframeAnimationUsekeyTimes]
[self setUpAnimationGroup]
}
- (UIImageView *)imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]
_imageView.layer.cornerRadius = 100
_imageView.layer.masksToBounds = YES
_imageView.center = self.view.center
_imageView.image = [UIImage imageNamed:@"zrx4.jpg"]
}
return _imageView
}
///闪烁效果
- (void)setUpAlphaAnimation {
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"opacity"]
animation.fromValue = [NSNumber numberWithFloat:1.0f]
animation.toValue = [NSNumber numberWithFloat:0.0f]
animation.autoreverses = YES
animation.duration = 1.0f
animation.repeatCount = MAXFLOAT
animation.removedOnCompletion = NO
animation.fillMode = kCAFillModeForwards
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]
[self.imageView.layer addAnimation:animation forKey:@"alpha"]
}
///单摆效果
- (void)setUpSharkAnimation {
//设置旋转原点
self.imageView.layer.anchorPoint = CGPointMake(0.5, 0)
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]
//角度转弧度(这里用1,-1简单处理一下)
rotationAnimation.toValue = [NSNumber numberWithFloat:1]
rotationAnimation.fromValue = [NSNumber numberWithFloat:-1]
rotationAnimation.duration = 1.0f
rotationAnimation.repeatCount = MAXFLOAT
rotationAnimation.removedOnCompletion = NO
rotationAnimation.autoreverses = YES
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
rotationAnimation.fillMode = kCAFillModeForwards
[self.imageView.layer addAnimation:rotationAnimation forKey:@"shark"]
}
///边缘跑圈效果
- (void)setUpCAKeyframeAnimationUseValues {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]
animation.keyPath = @"position"
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(50, 50)]
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 50, 50)]
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 50, [UIScreen mainScreen].bounds.size.height-50)]
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(50, [UIScreen mainScreen].bounds.size.height-50)]
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(50, 50)]
animation.values = @[value1,value2,value3,value4,value5]
animation.repeatCount = MAXFLOAT
animation.removedOnCompletion = NO
animation.fillMode = kCAFillModeForwards
animation.duration = 6.0f
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
[self.imageView.layer addAnimation:animation forKey:@"values"]
}
///矩形线路
- (void)setUpCAKeyframeAnimationUsePath {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]
animation.keyPath = @"position"
CGMutablePathRef path = CGPathCreateMutable()
//矩形线路
CGPathAddRect(path, NULL, CGRectMake(50,50, [UIScreen mainScreen].bounds.size.width - 100,[UIScreen mainScreen].bounds.size.height - 100))
animation.path = path
CGPathRelease(path)
animation.repeatCount = MAXFLOAT
animation.removedOnCompletion = NO
animation.fillMode = kCAFillModeForwards
animation.duration = 6.0f
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
[self.imageView.layer addAnimation:animation forKey:@"path"]
}
///左右挪动效果
- (void)setUpCAKeyframeAnimationUsekeyTimes {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]
animation.keyPath = @"position.x"
animation.values = @[@0, @20, @-20, @20, @0]
animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ]
animation.duration = 5
animation.additive = YES
[self.imageView.layer addAnimation:animation forKey:@"keyTimes"]
}
///旋转缩放动画组效果
- (void)setUpAnimationGroup {
CABasicAnimation * animationScale = [CABasicAnimation animation]
animationScale.keyPath = @"transform.scale"
animationScale.toValue = @(0.1)
CABasicAnimation *animationRota = [CABasicAnimation animation]
animationRota.keyPath = @"transform.rotation"
animationRota.toValue = @(M_PI_2)
CAAnimationGroup * group = [[CAAnimationGroup alloc] init]
group.duration = 3.0
group.fillMode = kCAFillModeForwards
group.removedOnCompletion = NO
group.repeatCount = MAXFLOAT
group.animations = @[animationScale,animationRota]
[self.imageView.layer addAnimation:group forKey:nil]
}
@end