AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = naVC;
[rootVC release];
[naVC release];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@property(nonatomic, retain)UIImageView *imageView;
@end
@implementation RootViewController
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:self.imageView];
[_imageView release];
self.imageView.image = [UIImage imageNamed:@"1.jpg"];
self.imageView.layer.borderWidth = 1;
UIButton *viewButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:viewButton];
viewButton.frame = CGRectMake(100, 350, 150, 50);
[viewButton setTitle:@"UIView动画" forState:UIControlStateNormal];
viewButton.layer.borderWidth = 1;
[viewButton addTarget:self action:@selector(viewAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *transformButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:transformButton];
transformButton.frame = CGRectMake(100, 450, 150, 50);
[transformButton setTitle:@"旋转缩小" forState:UIControlStateNormal];
transformButton.layer.borderWidth = 1;
[transformButton addTarget:self action:@selector(transformAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *layerButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:layerButton];
layerButton.frame = CGRectMake(100, 550, 150, 50);
[layerButton setTitle:@"layer动画" forState:UIControlStateNormal];
layerButton.layer.borderWidth = 1;
[layerButton addTarget:self action:@selector(layerAction:) forControlEvents:UIControlEventTouchUpInside];
}
// UIView动画
- (void)viewAction:(UIButton *)button {
// // UIView的动画第一种
// // Duration动画的运行时间
// [UIView animateWithDuration:5 animations:^{
// // 动画部分都写在block里
// self.imageView.frame = CGRectMake(0, 0, 300, 300);
// self.imageView.alpha = 0;
// }];
// // UIView动画第二种
// [UIView animateWithDuration:5 animations:^{
// // 所需动画部分
// self.imageView.frame = CGRectMake(0, 0, 300, 300);
// self.imageView.alpha = 0;
// } completion:^(BOOL finished) {
NSLog(@"动画内容结束");
// [UIView animateWithDuration:5 animations:^{
// self.imageView.frame = CGRectMake(100, 100, 200, 200);
// self.imageView.alpha = 1;
// }];
// }];
// UIView动画的第三种 呼吸效果
// 第二个参数: 动画延时
// | 按位或, 用其所在位的值可表示其本身
NSLog(@"%ld", UIViewAnimationOptionAutoreverse);
NSLog(@"%ld", UIViewAnimationOptionRepeat);
NSLog(@"%ld", UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse);
[UIView animateWithDuration:5 delay:0 options:8 | 16 animations:^{
//
self.imageView.frame = CGRectMake(0, 0, 300, 300);
self.imageView.alpha = 0;
} completion:^(BOOL finished) {
}];
// // UIView的第四个动画, 颤抖动画
// // 第三个参数: 控制颤抖的频率, 越小幅度越大
// // 第四个参数: 控制动画弹出的初速度, 越大越快
// [UIView animateWithDuration:5 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:5 options:UIViewAnimationOptionRepeat animations:^{
// self.imageView.frame = CGRectMake(100, 100, 300, 300);
// } completion:^(BOOL finished) {
//
// }];
}
// 通过transform对视图的样式进行修改
- (void)transformAction:(UIButton *)button {
// 通过transform, 使视图进行旋转, 缩小等操作
//
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_2);
// 控制缩放
// 第二三参数: 设置缩放比例, 一左右即可
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 1.5, 0.7);
// 控制偏移量
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 100, 30);
}
- (void)layerAction:(UIButton *)button {
// // layer主要是控制视图的显示内容, 比如样式, 内容都是layer来呈现, UIView的动画也是基于layer
// // 旋转
// CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// // 设置起始的角度
// basicAnimation.fromValue = [NSNumber numberWithFloat:0.0];
// // 设置旋转的角度
// basicAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
// // 设置动画的执行时长
// basicAnimation.duration = 3;
// // 设置动画的播放次数
// // NSIntegerMax 整数的最大值
// basicAnimation.repeatCount = NSIntegerMax;
// // 把动画添加到图片的layer上
// [self.imageView.layer addAnimation:basicAnimation forKey:@"basicAnimation"];
// // 设置是否回到原位
// basicAnimation.autoreverses = YES;
// // layer第二种动画
// CATransition *transition = [CATransition animation];
// // 设置动画的样式 rippleEffect 波纹效果
// transition.type = @"pageCurl";
// // 设置动画时间和重复次数
// transition.duration = 3;
// transition.repeatCount = NSIntegerMax;
// // 把动画添加到layer上
// [self.imageView.layer addAnimation:transition forKey:@"transition"];
/** type
*
* 各种动画效果 其中除了'fade', `moveIn', `push' , `reveal' ,其他属于私有的API.
* ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
* @"cube" 立方体翻滚效果
* @"moveIn" 新视图移到旧视图上面
* @"reveal" 显露效果(将旧视图移开,显示下面的新视图)
* @"fade" 交叉淡化过渡(不支持过渡方向) (默认为此效果)
* @"pageCurl" 向上翻一页
* @"pageUnCurl" 向下翻一页
* @"suckEffect" 收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
* @"rippleEffect" 滴水效果,(不支持过渡方向)
* @"oglFlip" 上下左右翻转效果
* @"rotate" 旋转效果
* @"push"
* @"cameraIrisHollowOpen" 相机镜头打开效果(不支持过渡方向)
* @"cameraIrisHollowClose" 相机镜头关上效果(不支持过渡方向)
*/
/** type
*
* kCATransitionFade 交叉淡化过渡
* kCATransitionMoveIn 新视图移到旧视图上面
* kCATransitionPush 新视图把旧视图推出去
* kCATransitionReveal 将旧视图移开,显示下面的新视图
*/
// CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
// // 设置动画的时长
// basicAnimation.duration = 3;
// // 设置动画重复次数
// basicAnimation.repeatCount = NSIntegerMax;
// // 设置起始值
// basicAnimation.fromValue = [NSNumber numberWithFloat:0];
// basicAnimation.toValue = [NSNumber numberWithFloat:5];
// [self.imageView.layer addAnimation:basicAnimation forKey:@"basicAnimation"];
// // CABasicAnimation动画就是在修改视图的transform属性, 从而达到动画的效果
// 关键帧动画
// 通过一帧一帧的点或者角度, 完成整个动画效果的设置
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// 创建一个路径, 用来记录移动的每一帧(坐标)
CGMutablePathRef path = CGPathCreateMutable();
// 指定移动的起始位置, 为了移动图片, 所以图片的中心位置就是起点
// 第一个参数: 用来记录的路径
// 第二个参数: 备用的参数, 用不上, NULL
// 第三四个参数: x, y分别对应图片的中心xy坐标位置
CGPathMoveToPoint(path, NULL, self.imageView.center.x, self.imageView.center.y);
// 再设置其他经过的点
CGPathMoveToPoint(path, NULL, 100, 100);
CGPathMoveToPoint(path, NULL, 10, 10);
CGPathMoveToPoint(path, NULL, 20, 100);
CGPathMoveToPoint(path, NULL, 140, 200);
CGPathMoveToPoint(path, NULL, 170, 190);
// 设置曲线路径
CGPathAddCurveToPoint(path, NULL, 200, 200, 100, 120, 40, 60);
CGPathAddCurveToPoint(path, NULL, 80, 10, 20, 100, 300, 100);
CGPathAddCurveToPoint(path, NULL, 10, 60, 200, 180, 30, 100);
CGPathAddCurveToPoint(path, NULL, 50, 90, 110, 10, self.imageView.center.x, self.imageView.center.y);
// 将整个路径添加到动画对象中
keyframe.path = path;
// 设置时间和重复次数
keyframe.repeatCount = NSIntegerMax;
keyframe.duration = 2;
// 动画添加到layer上
[self.imageView.layer addAnimation:keyframe forKey:@"keyframe"];
}