手势识别器的使用

//     1、轻拍手势识别器的使用

    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];

    //设置需要轻拍的次数

    tapGesture.numberOfTapsRequired=1;

    //设置需要触摸的个数:

    tapGesture.numberOfTouchesRequired=2;//使用Alt

    //添加手势识别器到被作用的视图

    [aView addGestureRecognizer:tapGesture];

    [tapGesture release];

//    2、创建一个平移手势识别器

    UIPanGestureRecognizer *panGesture=[[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)]autorelease];

    [aView addGestureRecognizer:panGesture];

//    3、创建一个旋转手势识别器

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

    [aView addGestureRecognizer:rotationGesture];

//    4、创建一个缩放手势识别器

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

    [aView addGestureRecognizer:pinchGesture];//捏合

    tapGesture.delegate=self;

    rotationGesture.delegate=self;

    pinchGesture.delegate=self;

//    5、创建一个长按手势识别器

    UILongPressGestureRecognizer *longPressGesture=[[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture:)]autorelease];

    [aView addGestureRecognizer:longPressGesture];

    longPressGesture.minimumPressDuration=0.5;

//    6、创建一个轻扫手势识别器

    UISwipeGestureRecognizer *swipeGesture=[[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)]autorelease];

    [aView addGestureRecognizer:swipeGesture];

    swipeGesture.direction=UISwipeGestureRecognizerDirectionDown;

//    7、创建一个屏幕边缘轻扫识别器

    UIScreenEdgePanGestureRecognizer *screenEdgeGesture=[[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handlescreenEdgeGesture:)]autorelease];

    [self.view addGestureRecognizer:screenEdgeGesture];

    screenEdgeGesture.edges=UIRectEdgeLeft;

//当一个手势识别器对象被识别后,会询问代理对象,是否允许同时识别其他手势识别器。如果此协议方法返回yes,表示允许。默认不允许

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;

}

-(void)handlePanGesture:(UIPanGestureRecognizer *)sender{

    UIView *aView=sender.view;

    //获取手势在视图对应坐标系中产生的位移增量

    CGPoint point=[sender translationInView:aView.superview];

    //在上次视图形变的基础上产生新的形变

    aView.transform=CGAffineTransformTranslate(aView.transform, point.x, point.y);

  //将当前产生的增量归零

    [sender setTranslation:CGPointZero inView:aView.superview];

}

-(void)handleRotationGesture:(UIRotationGestureRecognizer *)sender{

    UIView *aView=sender.view;

    aView.transform=CGAffineTransformRotate(aView.transform, sender.rotation);

    sender.rotation=0;//将旋转产生的角度增量设置为0;

-(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender{

    UIView *aView=sender.view;

    aView.transform=CGAffineTransformScale(aView.transform, sender.scale, sender.scale);

    sender.scale=1;

}

原文地址:https://www.cnblogs.com/sxsy-2015/p/4901176.html