主要想法

  • 添加3个ImageView展示图片,实现图片的无限循环。
  • 使用Swipe手势识别用户向右或向左滑动图片。
  • 使用CATransition给ImageView.layer添加动画,展示图片更换的效果。

实现

在storyboard添加三个UIImageView,用来展示图片。而数组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方法中设置一些初始参数

- (void)viewDidLoad {
[super viewDidLoad]; [self initData];
[self initView];
[self circleSwipeToMiddleImage];
} - (void)initData {
self.imageArray = [NSMutableArray new];
NSString *imageName; for (int i = ; i < ; i++) {
imageName = [NSString stringWithFormat:@"image%i", i]; [self.imageArray addObject:[UIImage imageNamed:imageName]];
}
}

中间的UIImageView(middleImage)最开始展示的第一张图。

- (void)initView {
self.middleImage.image = self.imageArray[]; //在imageView中添加外框,比较容易区分三张图片的位置
[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轻扫方向,否则默认是向右轻扫。

#pragma mark - 图片循环播放

- (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];
}

然后实现轻扫响应方法。

向右轻扫,middleImage显示下一张图片,则图片的下标index是当前展示图片的下标 + 1。而为了实现无限循环并不超出数组的下标范围,则需要%图片数据的张数。

/**
向右轻扫响应方法
*/
- (void)circleSwipeImageToRight {
UIImage *currentImage = self.middleImage.image;
NSInteger index = [self.imageArray indexOfObject:currentImage];
index = (index + ) % self.imageArray.count; [self changeAnimation:index toRight:YES];
}

向左轻扫,middleImage显示上一张图片,则图片的下标index是当前展示图片的下标 - 1。而为了实现无限循环并不超出数组的下标范围,则需要加上图片的张数之后在%图片的张数。

/**
向左轻扫响应方法
*/
- (void)circleSwipeImageToLeft {
UIImage *currentImage = self.middleImage.image;
NSInteger index = [self.imageArray indexOfObject:currentImage];
index = (index - + self.imageArray.count) % self.imageArray.count; [self changeAnimation:index toRight:NO];
}

最后是对middleImage.layer添加动画。

#pragma mark - 添加动画

/**
为middleImage添加动画效果 @param index 图片数组下标
@param toRight 是否是向右滑动
*/
- (void)changeAnimation:(NSInteger)index toRight:(BOOL)toRight {
CATransition *transition = [CATransition animation];
transition.type = kCATransitionReveal; //设置动画过渡的方式 if (toRight) {
//向右滑动,则图片是由左向右运动
transition.subtype = kCATransitionFromLeft;
}
else {
//向左滑动,则图片是由右向左运动
transition.subtype = kCATransitionFromRight;
} //将动画添加middleIamge.layer上
[self.middleImage.layer addAnimation:transition forKey:nil]; NSInteger count = self.imageArray.count; if (index >= && index < count) {
//更改middleImage展示的图片
self.middleImage.image = self.imageArray[index];
}
}

还有,图片可以选中了之后直接拉到项目的Assets.xcassets里面

assets.png

最终效果如下:

ChangeImage.gif

其实在这个项目中,leftImage和rightImage都没有显示图片,可以去掉,为了展示有多张图片的效果,可以在middleImage后面添加一个加了边框的UIView。
而在这个项目中,有一个局限,就是transition.type 只能指定是kCATransitionReveal格式,其他的格式的过渡效果都比较差。可以使leftImage和rightImage展示图片,然后将位置调整一下,并且修改transition.type看一下效果。下面是更改为kCATransitionPush的效果。

 

iOS 用Swipe手势和动画实现循环播放图片的更多相关文章

  1. winfrom 循环播放图片

    没啥新东西了,就是遍历和匹配文件名然后获取对象,放到picturebox里面 选中listview中想要查看的图片,然后点击查看按钮,进行↓代码. if (listView1.SelectedItem ...

  2. c# pictureBox 循环播放图片

    c# 1.遍历目录 查找图片 2.在 pictureBox 循环播放 public void PlayThread()//CMD_UpdateBtnStatus cmd { Int32 framera ...

  3. 在项目代码中载入cocostudio导出的动画并循环播放

    须要在代码中引入#include "cocostudio/CocoStudio.h" using namespace cocostudio; ArmatureDataManager ...

  4. iOS scrollview循环播放加缩放

    前些日子一直在研究3d的框架没有时间写博客,不过最后需求改了,也没研究出个啥.这段时间出了新的需求,需要循环播放图片,并且滑动的时候中间的图片有缩放的效果.刚开始想在网上搜索,不过并没有找到合适的de ...

  5. 微信小程序——实现动画循环播放

    今天在做砍价页面的时候需要将一个按钮动起来,效果图如下: 其实它实现的原理很简单,就是循环的缩小放大. css3中的animate 有个属性是 animation-iteration-count 可以 ...

  6. android 自定义无限循环播放的viewPager。轮播ViewPager。实现循环播放 广告,主题内容,活动,新闻内容时。

    前言 实际项目需要一个 播放广告的控件,可能有多个广告图片.每个一段时间更换该图片.简单来说,就是一个 “循环播放图片”的控件. 间隔时间更换图片 一般来说,图片切换时需要有动画效果 需要支持手势,用 ...

  7. iOS开发系列--无限循环的图片浏览器

    --UIKit之UIScrollView 概述 UIKit框架中有大量的控件供开发者使用,在iOS开发中不仅可以直接使用这些控件还可以在这些控件的基础上进行扩展打造自己的控件.在这个系列中如果每个控件 ...

  8. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  9. iOS CATransition 自定义转场动画

    https://www.jianshu.com/p/39c051cfe7dd CATransition CATransition 是CAAnimation的子类(如下图所示),用于控制器和控制器之间的 ...

随机推荐

  1. java语法:字符串数组的赋值

    字符串数组怎么赋值呢? 首先当然得先定义啦:String infoPack[] : 然后想当然的以为在for循环里,new一个数组, String infoPack[i] = imgurls; 事实证 ...

  2. iOS 之 通知

    步骤一,注册消息: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserProfileSu ...

  3. iOS 开发新版 动态库framework

    0. 参考 http://www.cocoachina.com/industry/20140613/8810.html framework+xib参考 : http://blog.csdn.net/x ...

  4. CRS-2800 CRS-4000

    I was installing Clusterware using ASM on VMware shared disks.  When I created the independent persi ...

  5. Could not execute auto check for display colors using command /usr/bin/xdpyinfo.(

    Steps to resolve this issue: 1) login into root user( su -l root) 2) execute this command : xhost +S ...

  6. Cocos2d-x 多分辨率支持

    最近遇到多分辨率支持问题,所以查了一些资料.将一些收获共享一下,以便自己和其他需要的朋友日后参考. 如果我要建立一个cocos2d-x项目,我的目标是支持iphone3G( 480, 320 ),ip ...

  7. SQL索引--基础理论

    1.索引的定义 索引是数据库表中一列或多列的值进行的一种排序,用于快速找出在某一列中特定的值. 2.索引的原理 如果不使用索引,则通常的查询数据中,需要对表中数据做一一对应的比较,直到找出所有相关的行 ...

  8. Charlse 使用小记

    抓包神器Fiddler 是基于微软的 .Net 技术开发的,没办法直接在 Mac/Linux 下使用,而Charlse是Mac下常用的网络封包截取工具.是一个HTTP代理服务器,HTTP监视器,反转代 ...

  9. GCD简介

    什么是GCD 全称是Grand Central Dispatch,可译为"牛逼的中枢调度器" 纯C语言,提供了非常多强大的函数   GCD的优势 GCD是苹果公司为多核的并行运算提 ...

  10. 深圳尚学堂:Android APP的测试流程

    每一个新开发的软件都避免不了测试,我在这里总结了一些Android系统的移动端APP测试的一些测试流程,希望可以给大家一些帮助. 1. UI 测试App主要核ui与实际设计的效果图是否一致:交互方面的 ...