图片浏览(CATransition)转场动画
Main.storyboard
ViewController.m
//
//? ViewController.m
//? 8A04.圖片瀏覽(轉場動畫)
//
//? Created by huan on 16/2/4.
//? Copyright ? 2016年 huanxi. All rights reserved.
//
?
#import "ViewController.h"
#define AnimationDuration 2
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
-(IBAction)tapView:(UITapGestureRecognizer *)sender;
@property (nonatomic, strong) NSMutableArray *imgs;
@property (nonatomic, assign) NSInteger currentImgIndex;//當前的索引
@end
?
@implementation ViewController
?
-(NSMutableArray *)imgs{
? ? if (!_imgs) {
? ? ? ? _imgs = [NSMutableArray array];
? ? ? ? for (NSInteger i = 1; i < 10; i++) {
? ? ? ? ? ? NSString *imgName = [NSString stringWithFormat:@"%ld",i];
? ? ? ? ? ? [_imgs addObject:imgName];
? ? ? ? }
? ? }
? ? return _imgs;
}
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view, typically from a nib.
? ? NSLog(@"%@",self.imgs);
}
?
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
?
-(IBAction)tapView:(UITapGestureRecognizer *)tap{
? ? //實現判斷圖片的左半邊還是右半邊
? ? //獲取觸摸點
? ? CGPoint point = [tap locationInView:tap.view];
? ? NSLog(@"%@", NSStringFromCGPoint(point));
?? ?
? ? if (point.x <= tap.view.bounds.size.width *0.5) {
? ? ? ? NSLog(@"上一張");
? ? ? ? [self previous];
? ? }else{
? ? ? ? NSLog(@"下一張");
? ? ? ? [self next];
? ? }
?? ?
}
?
-(void)previous{
? ? //判斷當前圖片是不是第一張
? ? if (self.currentImgIndex == 0) {
? ? ? ? return;
? ? }
?? ?
? ? //減索引 改圖片
? ? self.currentImgIndex --;
? ? self.imageView.image = [UIImage imageNamed:self.imgs[self.currentImgIndex]];
? ? //轉場動畫
? ? CATransition *animation = [CATransition animation];
? ? animation.type = @"push";
? ? //默認就是fromLeft
? ? animation.subtype = @"fromLeft";
? ? animation.duration = AnimationDuration;
? ? [self.imageView.layer addAnimation:animation forKey:nil];
}
?
/**
?* 提示:轉場動畫的類型(type)和子類型(subtype)能用字符串常量就用字符串常量
?*/
?
?
/**
?*******************************************************
?type:動畫類型(比如:滴水效果,翻轉效果...)
?-------------------------------------------------------
?fade kCATransitionFade 交叉淡化過渡
?moveIn kCATransitionMoveIn 新視圖移到舊視圖上面
?push kCATransitionPush 新視圖把舊視圖推出去
?reveal kCATransitionReveal 將舊視圖移開,顯示下面的新視圖
?pageCurl ? ? ? ? ? ? ? 向上翻一頁
?pageUnCurl ? ? ? ? ? ? 向下翻一頁
?rippleEffect ? ? ? ? ? ? 滴水效果
?suckEffect 收縮效果,如一塊布被抽走
?cube ? ? ? ? ? ? ? ? ? 立方體效果
?oglFlip? ? ? ? ? ? ? 上下左右翻轉效果
?rotate ? ? 旋轉效果
?cameraIrisHollowClose 相機鏡頭關上效果(不支持過渡方向)
?cameraIrisHollowOpen 相機鏡頭打開效果(不支持過渡方向)
?
?*******************************************************
?subtype: 動畫方向(比如說是從左邊進入,還是從右邊進入...)
?------------------------------------------------------
?kCATransitionFromRight;
?kCATransitionFromLeft;
?kCATransitionFromTop;
?kCATransitionFromBottom;
?
?當 type 為@"rotate"(旋轉)的時候,它也有幾個對應的 subtype,分別為:
?90cw 逆時針旋轉 90°
?90ccw 順時針旋轉 90°
?180cw 逆時針旋轉 180°
?180ccw? 順時針旋轉 180°
?**/
-(void)next{
?? ? ? //判斷當前圖片是不是最好一張
? ? if(self.currentImgIndex == self.imgs.count - 1){
? ? ? ? NSLog(@"已經是最好一張");
? ? ? ? return;
? ? }
?? ?
? ? //加索引 改圖片
? ? self.currentImgIndex ++;
? ? self.imageView.image = [UIImage imageNamed:self.imgs[self.currentImgIndex]];
? ? //設置圖片的時候,使用轉場動畫
? ? CATransition *animation = [CATransition animation];
?? ?
? ? //設置轉場動畫的類型
//? ? `fade', `moveIn', `push' and `reveal'.
? ? //fade 漸變 moveIn 直接移動
? ? animation.type = @"rotate";
//? ? animation.type = kCATransitionPush;
? ? //設置轉場動畫的子類型
//? ? `fromLeft', `fromRight', `fromTop' and
//? ? * `fromBottom'? fromLeft 從左邊開始推
? ? animation.subtype = @"90cw";
? ? animation.duration = AnimationDuration;
? ? [self.imageView.layer addAnimation:animation forKey:nil];
}
?
@end
?
?
轉載于:https://www.cnblogs.com/Lu2015-10-03/p/5191336.html
總結
以上是生活随笔為你收集整理的图片浏览(CATransition)转场动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTTP与HTTPS握手的那些事
- 下一篇: 【视频教程】JEECG 入门视频教程大全