手势

目录
  1.1 点击手势
  1.2 长按手势
  1.3 移动手势
  1.4 旋转手势
  1.5 缩放手势
  1.6 方向滑动手势
1.1 点击手势

//轻触 彩蛋
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

//触摸次数
tap.numberOfTapsRequired = 2;

//触摸点数量 
//tap.numberOfTouchesRequired = 2;

[imageView addGestureRecognizer:tap];
1.2 长按手势

UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

[imageView addGestureRecognizer:longPress];


//长按事件
- (void)longPress:(UILongPressGestureRecognizer*)longPress{
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"开始长按");
        
    }
    
    if (longPress.state == UIGestureRecognizerStateChanged) {
        
        NSLog(@"长按中");
        
    }
    
    if (longPress.state == UIGestureRecognizerStateEnded) {
        
        NSLog(@"长按结束");
        
    }
    
}
1.3 移动手势

UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

[imageView addGestureRecognizer:pan];


//移动事件
- (void)pan:(UIPanGestureRecognizer*)pan{
    
    CGPoint point = [pan translationInView:self.view];
    
    imageView.center = CGPointMake(imageView.center.x + point.x, imageView.center.y + point.y);
    
    //以当前点为原点计算
    
    [pan setTranslation:CGPointZero inView:self.view];
    
}

1.4 旋转手势

UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

rotation.delegate=self;

[imageView addGestureRecognizer:rotation];


- (void)rotation:(UIRotationGestureRecognizer*)rotation{
    
    _imageView.transform = CGAffineTransformMakeRotation(_rotation + rotation.rotation);
    
    if (rotation.state == UIGestureRecognizerStateEnded) {
        
        _rotation += rotation.rotation;
        
    }
    
}
1.5 缩放手势

UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];

pinch.delegate=self;

[imageView addGestureRecognizer:pinch];


- (void)pinch:(UIPinchGestureRecognizer*)pinch{
    
    //1.1 1.1
    _imageView.bounds = CGRectMake(0, 0, _imageView.bounds.size.width * pinch.scale, _imageView.bounds.size.height * pinch.scale);
    
    [pinch setScale:1];
    
}
1.6 方向滑动手势

需要分开写
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];


- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        
    }
    
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容