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

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

添加三个属性 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]];

    

}

 

转载于:https://www.cnblogs.com/lishanshan/p/4887714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值