IOS动画隐式,显式,翻页
// ViewController.m
// IOS动画0817
//
// Created by 张艳锋 on 15/8/17.
// Copyright (c) 2015年 张艳锋. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageview;
- (IBAction)doAnimationButton:(id)sender;
- (IBAction)doLeftButton:(id)sender;
- (IBAction)doRightButton:(id)sender;
- (IBAction)doUpButton:(id)sender;
- (IBAction)doDownButton:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)doAnimationButton:(id)sender {
/********隐式动画********/
/*
[UIView beginAnimations:nil context:NULL];//动画开始标志
//定义一个仿射变换
CGAffineTransform moveTrans=CGAffineTransformMakeTranslation(300, 600);
// CGAffineTransformMakeTranslation//更改位置的函数
// CGAffineTransformMakeRotation//k控制旋转
//CGAffineTransformMakeScale//控制缩放
[_imageview.layer setAffineTransform:moveTrans];
[UIView commitAnimations];//提交动画
*/
/********显式动画********/
//定义显示动画的时候,我们不必定义CALayer的变化,也不必执行它,而是通过CABasicAnimation逐个定义动画,其中每个动画都含有各自的属性,然后通过addAnimation:方法添加到图层中
/*
CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"opacity"];//opacity表示透明度
basic.duration=6.0;//6秒内透明度变为零
basic.fromValue=[NSNumber numberWithFloat:0.2];//初始透明度
basic.toValue=[NSNumber numberWithFloat:1.0];//最终透明度
basic.cumulative=YES;//设置透明度递增
[_imageview.layer addAnimation:basic forKey:@"animateOpacity"];
CGAffineTransform moveTrans1=CGAffineTransformMakeTranslation(300, 600);
CABasicAnimation *basic_move=[CABasicAnimation animationWithKeyPath:@"transform"];//准备函数(为平移做准备)
basic_move.duration=6.0;
//CATransform3DMakeAffineTransform仿射变换为3D效果(仿射不能直接应用于显式动画)
basic_move.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(moveTrans1)];
[_imageview.layer addAnimation:basic_move forKey:@"animateTransform2"];
*/
/********关键帧动画********/
/*
CAKeyframeAnimation *keyAnimation=[CAKeyframeAnimation animationWithKeyPath:@"opacity"];//透明度
keyAnimation.duration=6.0;//动画时间
keyAnimation.values=[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.6],[NSNumber numberWithFloat:1.0], nil];//
keyAnimation.keyTimes=[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.3],[NSNumber numberWithFloat:1.0], nil];
[_imageview.layer addAnimation:keyAnimation forKey:@"animateOpcaty"];
CGAffineTransform moveTrans2=CGAffineTransformMakeTranslation(300, 600);
CABasicAnimation *basic_move2=[CABasicAnimation animationWithKeyPath:@"transform"];
basic_move2.duration=6.0;
basic_move2.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(moveTrans2)];
[_imageview.layer addAnimation:basic_move2 forKey:@"animateTransform2"];
*/
}
//系统自带反转动画(上下左右)
- (IBAction)doLeftButton:(id)sender {
//三次向左不返回
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5f];//动画时间
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];//设置动画曲线方式(动画的总体变化时间曲线,包含开始慢后来快,开始快后来慢,均匀曲线)
[UIView setAnimationRepeatAutoreverses:NO];//设置动画是否做一次反向操作
[UIView setAnimationRepeatCount:3];//执行次数
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];//
[UIView commitAnimations];
}
- (IBAction)doRightButton:(id)sender {
//三次向右有返回
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:3];//执行次数
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[UIView commitAnimations];
}
- (IBAction)doUpButton:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView commitAnimations];
}
- (IBAction)doDownButton:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView commitAnimations];
}