首先创建storyboard
#import "ViewController.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UIImageView *showImage;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//组合动画
- (IBAction)group:(id)sender {
CAAnimationGroup * group = [CAAnimationGroup animation];
group.duration = 3.0f;
group.repeatCount = INFINITY;
//放大缩小(transform.scale里的属性)
CABasicAnimation * scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//把图片大小转换成动画播放的是时常,所以得转换成NSNumber,从fromValue到toValue;
scale.fromValue = [NSNumber numberWithFloat:1.0];
scale.toValue = [NSNumber numberWithFloat:2.0];
//设置透明度(opacity里的属性)
CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"opacity"];
//应为透明度是从1到0得,但是动画播放的是时常,所以得转换成NSNumber,从fromValue到toValue;
basic.fromValue = [NSNumber numberWithFloat:1];
basic.toValue = [NSNumber numberWithFloat:0];
//将两个动画放到group里
NSArray * arr = [NSArray arrayWithObjects:scale,basic, nil];
group.animations = arr;
[self.train.layer addAnimation:group forKey:@"group"];
//[self.train.layer removeAnimationForKey:@"group"]; //移除动画
}
//透明度
- (IBAction)opacaty:(id)sender {
CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"opacity"];
basic.fromValue = [NSNumber numberWithFloat:1];
basic.toValue = [NSNumber numberWithFloat:0];
basic.duration = 1;
basic.autoreverses = YES;
basic.repeatCount = INFINITY;
[self.train.layer addAnimation:basic forKey:@"scale"];
}
//layer动画能还原
- (IBAction)layerFrame:(id)sender {
CABasicAnimation * basic = [CABasicAnimation animationWithKeyPath:@"position"];
basic.toValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
basic.duration = 2.0f;
[self.train.layer addAnimation:basic forKey:@"donghua"];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 3.0f;
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:2.0];
animation.autoreverses = YES;
animation.repeatCount = INFINITY;
[self.train.layer addAnimation:animation forKey:@"放大"];
[self.train.layer removeAnimationForKey:@"放大"];
}
- (IBAction)layerScale:(id)sender {
CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
ani.fromValue = [NSNumber numberWithFloat:1.0];
ani.toValue = [NSNumber numberWithFloat:2.0];
ani.duration = 2.0;
ani.autoreverses = YES;
[self.train.layer addAnimation:ani forKey:@"scale"];
}
- (IBAction)frameAction:(id)sender {
//动画一
// [UIView beginAnimations:@"frame" context:nil];
// [UIView setAnimationDuration:2];//控制动画速度
// [self.showImage setFrame:CGRectMake(0, 0, 150, 50)];//改frame大小
// [self.showImage setAlpha:0.2];
// [UIView commitAnimations];//提交动画
//动画二
[UIView animateWithDuration:2 animations:^{
[self.showImage setCenter:CGPointMake(20, 180)];//改变动画坐标
}];
}
//放大
- (IBAction)scale:(id)sender {
// [UIView animateWithDuration:2 animations:^{
// self.showImage.transform = CGAffineTransformScale(self.showImage.transform, 1.5, 1.5);//图片持续放大
// self.showImage.transform = CGAffineTransformMakeScale(2, 2);//放大
//delay延迟时间
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationOptionCurveLinear animations:^{
[self.showImage setFrame:CGRectMake(60,20, 100, 90)];
} completion:^(BOOL finished) {
}];
}
//翻转视图
- (IBAction)fanzhuan:(id)sender {
UIImageView * aImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 202, 188)];
[aImage setImage:[UIImage imageNamed:@"2.png"]];
[UIView transitionWithView:self.train duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[self.showImage removeFromSuperview]; //将showImage从俯视图赏移除
[self.train addSubview:aImage];
} completion:NULL];
}