前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ios手势复习值之换图片-转场动画(纯代码)

ios手势复习值之换图片-转场动画(纯代码)

作者头像
用户1219438
发布2018-02-01 11:38:52
6680
发布2018-02-01 11:38:52
举报
文章被收录于专栏:AliceAlice

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

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

      2 index 用来表示图片的索引

      3 ISLeft 判断是不是向左滑  

下边是详细的代码:

代码语言:javascript
复制
- (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];
    
}

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

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

代码语言:javascript
复制
-(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]];

    

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-10-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档