常见手势使用

来源:互联网 发布:最新淘宝店铺教程视频 编辑:程序博客网 时间:2024/06/06 17:55

常用的有以下几种

敲击UITapGestureRecognizer捏合(一般用于缩放)UIPinchGestureRecognizer拖拽UIPanGestureRecognizer轻扫UISwipeGestureRecognizer旋转UIRotationGestureRecognizer长按UILongPressGestureRecognizer
- (void)addTap{    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];    tap.numberOfTapsRequired = 2;//点击次数    [self.imageView addGestureRecognizer:tap];    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpress:)];    [self.imageView addGestureRecognizer:longPress];    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];    //设置轻扫方向    swipe.direction = UISwipeGestureRecognizerDirectionUp;    [self.imageView addGestureRecognizer:swipe];    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];    [self.imageView addGestureRecognizer:rotation];    UIPinchGestureRecognizer  *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];    [self.imageView addGestureRecognizer:pinch];    UIPanGestureRecognizer  *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];    [self.imageView addGestureRecognizer:pan];}- (void)tap:(UITapGestureRecognizer *)tap{    NSLog(@"%s",__func__);}- (void)longpress:(UILongPressGestureRecognizer *)longpress{    //长按一般有俩种,按下和弹起(一般用弹起)    if(longpress.state == UIGestureRecognizerStateEnded){        NSLog(@"%s",__func__);    }}- (void)swipe:(UISwipeGestureRecognizer *)swipe{    NSLog(@"%s",__func__);}- (void)rotation:(UIRotationGestureRecognizer *)rotat{    NSLog(@"%s",__func__);    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform,rotat.rotation);    //默认传递的旋律的角度都是相对于最开始的位置    //复位    rotat.rotation = 0;}- (void)pinch:(UIPinchGestureRecognizer *)pinch{    NSLog(@"%s",__func__);    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale,pinch.scale);    //复位,缩放比例为1    pinch.scale = 1;}-(void)pan:(UIPanGestureRecognizer *)pan{    NSLog(@"%s",__func__);    //获取手势的移动,也就是相对于最开始的位置    CGPoint transP  = [pan translationInView:self.imageView];    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);    //复位    [pan setTranslation:CGPointZero inView:self.imageView];}