天天看点

iOS--手势识别UIGestureRecognizer

1.能用手势识别就用,不要用touch…。

2.UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为没使用它的自雷才能处理具体的手势。

1.UITapGestureRecongnnizer(敲击)

2.UIPinchGestureRecognizer(捏合,用于缩放)

3.UIPanGestureRecognizer(拖拽)

4.UISwipeGestureRecognizer(清扫)

#pragma mark - 代理方法
/**
 *  当点击view的时候,会先调用这个方法
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint pos = [touch locationInView:touch.view];
    if (pos.x <= self.iconView.frame.size.width * ) {
        return YES;
    }
    return NO;
}

- (void)testTap
{
    // 1.创建手势识别器对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    // 连续敲击2次,手势才能识别成功
    tap.numberOfTapsRequired = ;
    tap.numberOfTouchesRequired = ;

    // 2.添加手势识别器对象到对应的view
    [self.iconView addGestureRecognizer:tap];

    // 3.添加监听方法(识别到了对应的手势,就会调用监听方法)
    [tap addTarget:self action:@selector(tapView)];
}
           
- (void)viewDidLoad
{
    [super viewDidLoad];

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView)];

    swipe.direction = UISwipeGestureRecognizerDirectionUp;

    [self.redView addGestureRecognizer:swipe];
}

- (void)swipeView
{
    NSLog(@"swipeView");
}

- (void)testLongPress
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
    [longPress addTarget:self action:@selector(longPressView)];

    // 至少长按2秒
    longPress.minimumPressDuration = ;

    // 在触发手势之前,50px范围内长按有效
    longPress.allowableMovement = ;

    [self.redView addGestureRecognizer:longPress];
}

- (void)longPressView
{
    NSLog(@"长按了红色的view");
}
           
#import "MJViewController.h"

@interface MJViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self testPinchAndRotate];
}

#pragma mark - 手势识别器的代理方法

/**
 *  是否允许多个手势识别器同时有效
 *  Simultaneously : 同时地
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

#pragma mark - 缩放 + 旋转
- (void)testPinchAndRotate
{
    [self testPinch];
    [self testRotate];
}

#pragma mark - 缩放手势(捏合手势)
- (void)testPinch
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    pinch.delegate = self;
    [self.iconView addGestureRecognizer:pinch];
}

- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = ; // 这个真的很重要!!!!!
}

#pragma mark - 旋转手势
- (void)testRotate
{
    UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
    recognizer.delegate = self;
    [self.iconView addGestureRecognizer:recognizer];
}

- (void)rotateView:(UIRotationGestureRecognizer *)recognizer
{
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    recognizer.rotation = ; // 这个很重要!!!!!
}

@end
           
#import "MJViewController.h"

@interface MJViewController ()
@property (weak, nonatomic) IBOutlet UIView *purpleView;

@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [self.purpleView addGestureRecognizer:pan];
}

- (void)panView:(UIPanGestureRecognizer *)pan
{

    switch (pan.state) {
        case UIGestureRecognizerStateBegan: // 开始触发手势

            break;

        case UIGestureRecognizerStateEnded: // 手势结束

            break;

        default:
            break;
    }

    // 1.在view上面挪动的距离
    CGPoint translation = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x += translation.x;
    center.y += translation.y;
    pan.view.center = center;

    // 2.清空移动的距离
    [pan setTranslation:CGPointZero inView:pan.view];
}

@end
           

继续阅读