手势

手势

@property (nonatomic, strong) UIView *testView;
///记录旋转角度
@property (nonatomic, assign) CGFloat rotation;
///记录缩放比例
@property (nonatomic, assign) CGFloat scale;
// !!!: 点击
- (void)addTapGestureRecognizer {
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
    [self.view addGestureRecognizer:tapGes];
}
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender {
    NSLog(@"点击");
}
// !!!: 长按
- (void)addLongPressGestureRecognizer {
    UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer  alloc]initWithTarget:self action:@selector(longPressGestureRecognizer:)];
    //用几个手指触屏,默认1,设置多少就必须多少触碰点
    longPressGes.numberOfTouchesRequired = 1;
    //设置最短长按时间,单位为秒(默认0.5)
    longPressGes.minimumPressDuration = 1;
    //设置手势识别期间所允许的手势可移动范围,默认10
    longPressGes.allowableMovement = 10;
    [self.view addGestureRecognizer:longPressGes];
}
- (void)longPressGestureRecognizer:(UILongPressGestureRecognizer *)sender {
//    CGPoint point  = [sender locationInView:self.view];
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"UIGestureRecognizerStateBegan");
    }else if (sender.state == UIGestureRecognizerStateChanged) {
        NSLog(@"UIGestureRecognizerStateChanged");
    }else if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"UIGestureRecognizerStateEnded");
    }
    //其他没啥用
}
// !!!: 快速滑动
- (void)addSwipeGestureRecognizer {
    UISwipeGestureRecognizer *leftSwipeGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    leftSwipeGes.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipeGes];
    UISwipeGestureRecognizer *rightSwipeGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    rightSwipeGes.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipeGes];
}
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)sender {
    NSLog(@"%@",@(sender.direction));
}
// !!!: 慢速滑动
- (void)addPanGestureRecognizer {
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];
    [self.view addGestureRecognizer:panGesture];
}
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)sender {
    CGPoint point = [sender translationInView:self.view];
    if (ABS(point.x) > ABS(point.y)*2) {//30度
        if (point.x > 10) {//速度
            
        }else if (point.x < -10) {
            
        }
    }
    //每次调用之后,需要重置手势的偏移量,否则偏移量会自动累加
    [sender setTranslation:CGPointZero inView:self.view];
}
// !!!: 边缘滑动
- (void)addScreenEdgePanGestureRecognizer {
    UIScreenEdgePanGestureRecognizer * edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureRecognizer:)]; //手势由self来管理
    edgePan.edges = UIRectEdgeRight;
    [self.view addGestureRecognizer:edgePan];
}
- (void)screenEdgePanGestureRecognizer:(UIScreenEdgePanGestureRecognizer *)sender {
    CGFloat progress = fabs([sender translationInView:[UIApplication sharedApplication].keyWindow].x / [UIApplication sharedApplication].keyWindow.bounds.size.width);
    NSLog(@"%@-%@",@(sender.state).description,@(progress).description);
}
// !!!: 缩放
- (void)addPinchGestureRecognizer {
    UIPinchGestureRecognizer *gesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureRecognizer:)];
    //    scale
    [self.view addGestureRecognizer:gesture];
    self.testView.backgroundColor = [UIColor greenColor];
}
- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)sender {
    CGFloat scale = sender.scale;
    scale = self.scale * scale;
    if (sender.state == UIGestureRecognizerStateEnded) {
        self.scale = scale;
    }
    self.testView.transform = CGAffineTransformMake(scale, 0, 0, scale, 0, 0);
}
// !!!: 旋转
- (void)addRotationGestureRecognizer {
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureRecognizer:)];
    //    rotation: //获取旋转角度
    //    velocity: //获取旋转速度
    [self.view addGestureRecognizer:rotationGesture];
    self.testView.backgroundColor = [UIColor greenColor];
}
- (void)rotationGestureRecognizer:(UIRotationGestureRecognizer *)sender {
    // 获取手势旋转的弧度
    CGFloat rotation = sender.rotation;
    rotation += self.rotation;
    if (sender.state == UIGestureRecognizerStateEnded) {
        self.rotation = rotation;
        while (self.rotation < -M_PI*2) {
            self.rotation += M_PI*2;
        }
        while (self.rotation > M_PI*2) {
            self.rotation -= M_PI*2;
        }
    }
    self.testView.transform = CGAffineTransformMakeRotation(rotation);
}
- (UIView *)testView
{
    if (!_testView) {
        _testView = [UIView newAutoLayoutView];
        [self.view addSubview:_testView];
        [_testView autoCenterInSuperview];
        [_testView autoSetDimensionsToSize:CGSizeMake(50, 50)];
    }
    return _testView;
}
- (CGFloat)scale
{
    if (_scale == 0) {
        _scale = 1;
    }
    return _scale;
}

你可能感兴趣的:(手势)