其实转场动画,我们一直在使用,UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果。
动画相关属性:
1.type :动画过度类型
2.subtype:动画过度方向
3.startProgress:动画起点(在整体动画的百分比)
4.endProgress:动画终点(在整体动画的百分比)
苹果官方给我们提供了四中动画类型
//设置动画类型
提供了:
kCATransitionFade 渐变
kCATransitionMoveIn 进入
kCATransitionPush 推入
kCATransitionReveal 移除
*/
这几种效果不够炫酷,
那么看看下面的动画类型吧,或许你会用上(官方私有的动画类型)
本文也牵扯到一点懒加载的东西。
接下来让我们看代码。
// Created by imac on 15/9/16.
// Copyright (c) 2015年 imac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
UIImageView *_imageView;
//用来记录图片的索引
NSInteger _index;
}
@property (nonatomic, strong)NSMutableArray *arr;
@end
@implementation ViewController
//懒加载,
- (NSMutableArray *)arr
{
if (_arr == nil) {
_arr = [NSMutableArray array];
return _arr;
}
return _arr;
}
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
_imageView.image = [UIImage imageNamed:@"1.jpeg"];
//打开用户交互
_imageView.userInteractionEnabled = YES;
[self.view addSubview:_imageView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//懒加载,当用户点击屏幕再去创建加载资源
for (int i = 1; i < 7; i ++) {
NSString *str = [NSString stringWithFormat:@"%d.jpeg",i];
UIImage *images = [UIImage imageNamed:str];
[self.arr addObject:images];
}
//记录点击的次数,用来切换图片
_index++;
//防止数组越界。
if (_index == self.arr.count){
_index = 0;
}
//切换图片
_imageView.image = self.arr[_index];
/*=--添加动画效果--*/
//创建转场动画对象
CATransition *transition = [[CATransition alloc] init];
transition.duration = 1;
/*
//设置动画类型
官方 提供了:
kCATransitionFade 渐变
kCATransitionMoveIn 进入
kCATransitionPush 推入
kCATransitionReveal 移除
*/
//transition.type = kCATransitionFade;
//设置动画类型
transition.type = @"cube";
//设置动画过渡的方向
transition.subtype = kCATransitionFromRight;
[_imageView.layer addAnimation:transition forKey:nil];
}
到这里为止,动画也回顾的差不多了。在编程的路上是无止境的,不要放弃,你所遇到的困难,都是为了筛选那些不能坚持的人。