iOS 用Swipe手勢和動畫實(shí)現(xiàn)循環(huán)播放圖片示例

主要想法
- 添加3個ImageView展示圖片,實(shí)現(xiàn)圖片的無限循環(huán)。
- 使用Swipe手勢識別用戶向右或向左滑動圖片。
- 使用CATransition給ImageView.layer添加動畫,展示圖片更換的效果。
實(shí)現(xiàn)
在storyboard添加三個UIImageView,用來展示圖片。而數(shù)組imageArray則用來保存圖片對象。
@interface ViewController () @property (strong, nonatomic) IBOutlet UIImageView *middleImage; @property (strong, nonatomic) IBOutlet UIImageView *leftImage; @property (strong, nonatomic) IBOutlet UIImageView *rightImage; @property (strong, nonatomic) NSMutableArray *imageArray; @end
在viewDidLoad方法中設(shè)置一些初始參數(shù)
- (void)viewDidLoad {
[super viewDidLoad];
[self initData];
[self initView];
[self circleSwipeToMiddleImage];
}
- (void)initData {
self.imageArray = [NSMutableArray new];
NSString *imageName;
for (int i = 0; i < 5; i++) {
imageName = [NSString stringWithFormat:@"image%i", i];
[self.imageArray addObject:[UIImage imageNamed:imageName]];
}
}
中間的UIImageView(middleImage)最開始展示的第一張圖。
- (void)initView {
self.middleImage.image = self.imageArray[0];
//在imageView中添加外框,比較容易區(qū)分三張圖片的位置
[self addBorder:self.middleImage];
[self addBorder:self.leftImage];
[self addBorder:self.rightImage];
}
- (void)addBorder:(UIImageView *)imageView {
imageView.layer.borderWidth = 1.0;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
接著在self.view上添加swipe手勢,分別是向左和向右輕掃。swipe手勢必須要指定direction輕掃方向,否則默認(rèn)是向右輕掃。
#pragma mark - 圖片循環(huán)播放
- (void)circleSwipeToMiddleImage {
UISwipeGestureRecognizer *gesture1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(circleSwipeImageToRight)];
gesture1.direction = UISwipeGestureRecognizerDirectionRight;
self.view.userInteractionEnabled = YES;
UISwipeGestureRecognizer *gesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(circleSwipeImageToLeft)];
gesture2.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:gesture1];
[self.view addGestureRecognizer:gesture2];
}
然后實(shí)現(xiàn)輕掃響應(yīng)方法。
向右輕掃,middleImage顯示下一張圖片,則圖片的下標(biāo)index是當(dāng)前展示圖片的下標(biāo) + 1。而為了實(shí)現(xiàn)無限循環(huán)并不超出數(shù)組的下標(biāo)范圍,則需要%圖片數(shù)據(jù)的張數(shù)。
/**
向右輕掃響應(yīng)方法
*/
- (void)circleSwipeImageToRight {
UIImage *currentImage = self.middleImage.image;
NSInteger index = [self.imageArray indexOfObject:currentImage];
index = (index + 1) % self.imageArray.count;
[self changeAnimation:index toRight:YES];
}
向左輕掃,middleImage顯示上一張圖片,則圖片的下標(biāo)index是當(dāng)前展示圖片的下標(biāo) - 1。而為了實(shí)現(xiàn)無限循環(huán)并不超出數(shù)組的下標(biāo)范圍,則需要加上圖片的張數(shù)之后在%圖片的張數(shù)。
/**
向左輕掃響應(yīng)方法
*/
- (void)circleSwipeImageToLeft {
UIImage *currentImage = self.middleImage.image;
NSInteger index = [self.imageArray indexOfObject:currentImage];
index = (index - 1 + self.imageArray.count) % self.imageArray.count;
[self changeAnimation:index toRight:NO];
}
最后是對middleImage.layer添加動畫。
#pragma mark - 添加動畫
/**
為middleImage添加動畫效果
@param index 圖片數(shù)組下標(biāo)
@param toRight 是否是向右滑動
*/
- (void)changeAnimation:(NSInteger)index toRight:(BOOL)toRight {
CATransition *transition = [CATransition animation];
transition.type = kCATransitionReveal; //設(shè)置動畫過渡的方式
if (toRight) {
//向右滑動,則圖片是由左向右運(yùn)動
transition.subtype = kCATransitionFromLeft;
}
else {
//向左滑動,則圖片是由右向左運(yùn)動
transition.subtype = kCATransitionFromRight;
}
//將動畫添加middleIamge.layer上
[self.middleImage.layer addAnimation:transition forKey:nil];
NSInteger count = self.imageArray.count;
if (index >= 0 && index < count) {
//更改middleImage展示的圖片
self.middleImage.image = self.imageArray[index];
}
}
還有,圖片可以選中了之后直接拉到項(xiàng)目的Assets.xcassets里面

最終效果如下:

其實(shí)在這個項(xiàng)目中,leftImage和rightImage都沒有顯示圖片,可以去掉,為了展示有多張圖片的效果,可以在middleImage后面添加一個加了邊框的UIView。
而在這個項(xiàng)目中,有一個局限,就是transition.type 只能指定是kCATransitionReveal格式,其他的格式的過渡效果都比較差??梢允筶eftImage和rightImage展示圖片,然后將位置調(diào)整一下,并且修改transition.type看一下效果。下面是更改為kCATransitionPush的效果。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)音樂播放器圖片旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)音樂播放器圖片旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10
ios8 UITableView設(shè)置 setSeparatorInset UIEdgeInsetsZero不起作用的解決
這篇文章主要介紹了ios8 UITableView設(shè)置 setSeparatorInset UIEdgeInsetsZero不起作用的解決辦法(去掉15px空白間距)的相關(guān)資料,需要的朋友可以參考下2016-02-02
iOS通過Runtime實(shí)現(xiàn)友盟統(tǒng)計(jì)的實(shí)例代碼
本篇文章主要介紹了iOS通過Runtime實(shí)現(xiàn)友盟統(tǒng)計(jì)的實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
iOS UICollectionView實(shí)現(xiàn)橫向滑動
這篇文章主要為大家詳細(xì)介紹了iOS UICollectionView實(shí)現(xiàn)橫向滑動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
iOS使用pageViewController實(shí)現(xiàn)多視圖滑動切換
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法
下面小編就為大家分享一篇ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS使用GCDSocketManager實(shí)現(xiàn)長連接的方法
下面想就為大家分享一篇iOS使用GCDSocketManager實(shí)現(xiàn)長連接的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

