UI--Gesture

宗政唯
2023-12-01

别的不说 上干货
各种手势用法

1.轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    self.imv.userInteractionEnabled = YES;//因为图片会阻断事件,所以在其上面添加手势的时候一定要打开交互
    [self.imv addGestureRecognizer:tap];
//    点击次数
    tap.numberOfTapsRequired = 2;
//    触控点个数
    tap.numberOfTouchesRequired = 2;
###################
- (void)tapAction{
    NSLog(@"AAAAAA");
}
//imv 是你要作用的对象
2.长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    self.imv.userInteractionEnabled = YES;
    [self addGestureRecognizer:longPress];
#############
- (void)longPress:(UILongPressGestureRecognizer *)longPress{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"hehahah");
    }
}
    3.旋转
    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
    self.imv.userInteractionEnabled = YES;
    [self.imv addGestureRecognizer:rotate];
#######
- (void)rotateAction:(UIRotationGestureRecognizer *)rotateAction{
    self.imv.transform = CGAffineTransformRotate(self.imv.transform, rotateAction.rotation);
    [rotateAction setRotation:0];
}
注:transform 在这里是变换的意思
   4 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    self.imv.userInteractionEnabled = YES;
    [self.imv addGestureRecognizer:pinch];
########
- (void)pinchAction:(UIPinchGestureRecognizer *)pinchAction{
    self.imv.transform = CGAffineTransformScale(self.imv.transform, pinchAction.scale, pinchAction.scale);
    //1是倍数的意思----这是清空之前的状态
    pinchAction.scale = 1;
}
    5.平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    self.imv.userInteractionEnabled = YES;
    [self.imv addGestureRecognizer:pan];
#########
- (void)panAction:(UIPanGestureRecognizer *)sender{
    NSLog(@"pan");

    //取到点的信息
    CGPoint point = [sender translationInView:self.imv];
    self.imv.transform = CGAffineTransformTranslate(self.imv.transform, point.x, point.y);

    //把每次保存的之前一次的移动距离清空
    [sender setTranslation:(CGPointZero) inView:self.imv];
每次平移都会先回到原点,然后再动
    self.imv.transform = CGAffineTransformMakeTranslation(point.x, point.y);
}
注:translation 是平移的意思
translateTransfrom 平移变换
   6. 清扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    self.imv.userInteractionEnabled = YES;
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:swipe];
##########
   7. 边缘识别
    UIScreenEdgePanGestureRecognizer *edge = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:edge action:@selector(tapAction)];
    self.imv.userInteractionEnabled = YES;
    [self addGestureRecognizer:edge];
 类似资料: