UISwipeGestureRecognizer 侧滑手势

iOS中肯定用到手势。下面是简单的介绍一下侧滑手势 就是UISwipeGestureRecognizer。



#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController


- (void)viewDidLoad {

 

   [super viewDidLoad];

  UIImageView *imageView = [[UIImageViewalloc]init];

  imageView.backgroundColor = [UIColoryellowColor];

  imageView.image = [UIImageimageNamed:@"01"];

  CGFloat imageViewW = 300;

  CGFloat imageViewH =imageViewW *400/640;

  CGFloat imageViewX =(self.view.frame.size.width - imageViewW)/2;

  CGFloat imageViewY =200;

  imageView.userInteractionEnabled =YES;

  imageView.frame =CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);

  [self.viewaddSubview:imageView];

  

  /*

   最好是一个方向写一个手势

   */

  

  //

  UISwipeGestureRecognizer *swipe1=  [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];

  //这是两个手指才能侧滑默认是1个手指

//  swipe1.numberOfTouchesRequired = 2;

  swipe1.direction=UISwipeGestureRecognizerDirectionRight;

  

  [imageView addGestureRecognizer:swipe1];

  

  //

  UISwipeGestureRecognizer *swipe2=  [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];

  swipe2.direction=UISwipeGestureRecognizerDirectionLeft;

  

  [imageView addGestureRecognizer:swipe2];

  

  //

  UISwipeGestureRecognizer *swipe3=  [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];

  swipe3.direction=UISwipeGestureRecognizerDirectionUp;

  

  [imageView addGestureRecognizer:swipe3];

  

  //

  UISwipeGestureRecognizer *swipe4=  [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];

  swipe4.direction=UISwipeGestureRecognizerDirectionDown;

  

  [imageView addGestureRecognizer:swipe4];

  

  

}


-(void)swipe:(UISwipeGestureRecognizer *)swipe{

  

  

  switch (swipe.direction) {

    caseUISwipeGestureRecognizerDirectionRight:

      [self makeMove:swipeandX:100andY:0];

      NSLog(@"right");

      break;

    caseUISwipeGestureRecognizerDirectionLeft:

      [self makeMove:swipeandX:-100andY:0];

      NSLog(@"left");

      break;

    caseUISwipeGestureRecognizerDirectionUp:

      [self makeMove:swipeandX:0andY:-100];

      NSLog(@"up");

      break;

    caseUISwipeGestureRecognizerDirectionDown:

      [self makeMove:swipeandX:0andY:100];

      NSLog(@"down");

      break;

    default:

      break;

  }

  

}


//添加一个动画让他更明显的显示出侧滑的方向

-(void)makeMove:(UISwipeGestureRecognizer *)swipe andX:(CGFloat)x andY:(CGFloat)y{

  

  CGFloat time = 0.1;

  [UIViewanimateWithDuration:timeanimations:^{

    

    swipe.view.transform =CGAffineTransformMakeTranslation(x, y);

    

  } completion:^(BOOL finished) {

    

      [UIViewanimateWithDuration:timeanimations:^{

        

        swipe.view.transform =CGAffineTransformIdentity;

        

      }];

    

  }];

  

  

  

}



@end


我们可以用这个手势做一些简单的动画使我们的APP有更好的用户体验。若有地方写的不对或者不好,求大神指出,谢谢啦


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值