目标:实现通过手势进行图片的切换   通过左扫右扫 来实现(纯代码)

添加三个属性 1uiImageView 用来显示图片的view

      2 index 用来表示图片的索引

      3 ISLeft 判断是不是向左滑

下边是详细的代码:

- (void)viewDidLoad {
[super viewDidLoad];
self.index = 0;
self.ISLeft = YES;
_imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
// _imageView.backgroundColor = [UIColor redColor];
_imageView.contentMode = UIViewContentModeScaleAspectFit;//合适的大小 self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png",_index]]; [self.view addSubview:_imageView]; //用户交互设置
self.imageView.userInteractionEnabled = YES; //添加扫动得手势
UISwipeGestureRecognizer *swipL = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
swipL.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageView addGestureRecognizer:swipL]; UISwipeGestureRecognizer *swipR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
swipR.direction = UISwipeGestureRecognizerDirectionRight;
[self.imageView addGestureRecognizer:swipR]; }

设置转场动画 :在手势里边进行实现

手势的实现以及转场动画:

-(void)swip:(UISwipeGestureRecognizer *)sender

{

    if(sender.direction == UISwipeGestureRecognizerDirectionLeft)

    {

        if(self.index>=0)

        {

            if(self.index>0)

            {

                self.index--;

            }

            else

            {

                self.index =3;

            }

            self.ISLeft = YES;

        }

    }

    else

    {

        self.ISLeft = NO;

        if(self.index<3)

        {

            self.index++;

            if(self.index ==3)

            {

                self.index =0;

            }

        }

    }

    //转场动画

    CATransition *trans = [[CATransition alloc]init];
//转场动画的类型
trans.type =@"push";
//判断是不是向左滑
if(self.ISLeft) { trans.subtype = kCATransitionFromTop; } else { trans.subtype = kCATransitionFromBottom; } trans.duration = 0.5f; [self.imageView.layer addAnimation:trans forKey:@"trans"]; //切换图画 self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",_index]]; }

最新文章

  1. MemCache超详细解读
  2. 上传文件fileupload
  3. js倒计时跳转链接
  4. Linux学习:netstat命令
  5. 海美迪Q系列视频文明书
  6. webservice 第一节 .net SoapHeader验证
  7. 在Docker中运行asp.net core 跨平台应用程序
  8. python yaml用法详解
  9. 在DataFrame数据表里面提取需要的行
  10. 线性筛素数和理解 洛谷P3383
  11. mvc 母版页保持不刷新
  12. day15_python_1124
  13. jsxyhelu的GitHub使用方法
  14. 今年新鲜出炉的30个流行Android库,你一定需要
  15. [Winform]使用winform制作远程桌面管理工具
  16. New Concept English Two 4
  17. Linux☞如何修改文件权限
  18. MongoDB-Ubuntu环境下安装
  19. ios8设置application badge value
  20. JavaScript中的map方法

热门文章

  1. Spark、Shark集群安装部署及遇到的问题解决
  2. 协定类型不具有 ServiceContractAttribute 特性
  3. poj3233
  4. 在MacOSX下使用Github管理Xcode代码
  5. 高清摄像头MIPI接口与ARM处理器的连接
  6. MySQL高可用性大杀器之MHA | 火丁笔记
  7. 某外企mono for android试题
  8. request.setAttribute()用法
  9. Cadence 信号完整性(一)-- 仿真步骤3
  10. memcached的基本命令(安装、卸载、启动、配置相关)