//
// ViewController.m
// KeyFrameAnimation
//
// Created by vincent_guo on 13-9-6.
// Copyright (c) 2013年 vincent_guo. All rights reserved.
//
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController (){
UIImageView *_imageView;
}
@end
@implementation ViewController
- (
void
)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_imageView = [[UIImageView alloc] init];
_imageView.bounds = CGRectMake(0, 0, 80, 80);
_imageView.center = CGPointMake(160, 50);
_imageView.image = [UIImage imageNamed:@
"clock"
];
[self.view addSubview:_imageView];
[_imageView release];
}
-(
void
)click:(id)sender{
[self turnTwoRound2];
}
#pragma mark 使用CABasicAnimation让View向左转两圈,要向右转两圈,把-M_PI负号去掉就可以了
-(
void
)turnTwoRound1 {
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@
"transform.rotation.z"
];
rotationAnimation.toValue = [NSNumber numberWithFloat: -M_PI * 2.0 ];
rotationAnimation.duration = 0.5;
rotationAnimation.repeatCount = 2;
//动画执行两次
[_imageView.layer addAnimation:rotationAnimation forKey:@
"rotationAnimation"
];
}
#pragma mark 使用CAKeyframeAnimation让View向左转两圈,要向右转两圈,把-M_PI负号去掉就可以了
-(
void
)turnTwoRound2{
CAKeyframeAnimation *rotateAnimation = [CAKeyframeAnimation animationWithKeyPath:@
"transform.rotation.z"
];
rotateAnimation.duration = 0.5;
//先从原始位置向左转180度,再向左转180度是-M_PI*4,不是-M_PI*2哦,自己好好理解下)
rotateAnimation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:-M_PI * 2],
[NSNumber numberWithFloat:-M_PI * 4], nil];
[_imageView.layer addAnimation:rotateAnimation forKey:@
"rotationAnimation"
];
}
@end
转自:http://www.cnblogs.com/vincent-guo/p/3305428.html