ios 手势的基本使用

手势类型

UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势。其子类有:

UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)

属性state

另外,UIGestureRecognizer类中有一个属性state,表示手势的当前状态,如开始、正在..中、结束等。其子类也继承了这个属性,有以下状态:

UIGestureRecognizerStatePossible、Began、Changed、Ended、Cancelled、Failed。

基本使用

要什么手势,就创建什么手势的对象,同时设置监听方法(自定义),然后添加给对应控件,如:

点击手势
QQ.png
长按手势
QQ.png
捏合手势
QQ.png
旋转手势
QQ.png
轻扫手势
QQ.png

PS: 一个轻扫手势只能有一个方向。若要有多个方向,则再添加轻扫对象,不同方向的轻扫可以设对应的操作。

QQ.png
QQ.png

注意:UIImageView默认不能与用户交互。

  • 手势还可以添加代理
    点按tap、常按longPress(移动时也会持续调用)、轻扫swipe、拖到pan、捏合pinch、旋转Rotation。
QQ.png

每一个手势都有对应的代理方法

QQ.png
QQ.png
QQ.png
QQ.png
QQ.png
  • 同时支持多个手势
    协议
    设代理(要同时支持哪些手势,就把相关的手势对象设置代理
QQ.png
QQ.png

代理方法

QQ.png

应用举例

  • 实现图片拖动、缩放、旋转。(亲测可用)
#pragma mark - 添加 Imgview
-(void)addMaskImageView{
    
    UIImageView *Imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"manHair1"]];
    Imgv.clipsToBounds = YES;
    Imgv.hidden = YES;
    Imgv.userInteractionEnabled = YES;
    [_personBackImgV addSubview:Imgv];
    _maskImgV = Imgv;
    double m = 150/_maskImgV.image.size.width;
    _maskImgV.frame = CGRectMake(20, 100, 150, _maskImgV.image.size.height*m);
    
    //添加拖到、旋转、缩放 手势
    UIPanGestureRecognizer *gesture1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onDrag:)];
    [_maskImgV addGestureRecognizer:gesture1];

    UIRotationGestureRecognizer *gesture2 = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotating:)];
    [_maskImgV addGestureRecognizer:gesture2];

    UIPinchGestureRecognizer *gesture3 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(onScale:)];
    [_maskImgV addGestureRecognizer:gesture3];
    
}
#pragma mark - 缩放图片 处理
-(void)onScale:(UIPinchGestureRecognizer*)gesture{
    
    gesture.view.transform = CGAffineTransformScale(gesture.view.transform, gesture.scale, gesture.scale);
    gesture.scale = 1;
}

#pragma mark - 旋转图片 处理
-(void)onRotating:(UIRotationGestureRecognizer*)gesture{
    
    gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, gesture.rotation);
    gesture.rotation = 0;
}

#pragma mark - 移动图片 处理
-(void)onDrag:(UIPanGestureRecognizer*)gesture{
    
    CGPoint translation = [gesture translationInView:self.view];
    CGPoint center = gesture.view.center;
    center.x += translation.x;
    center.y += translation.y;
    gesture.view.center = center;
    [gesture setTranslation:CGPointZero inView:self.view];
    
//    if (gesture.state == UIGestureRecognizerStateEnded){
//        //UIGestureRecognizerStateBegan /UIGestureRecognizerStateChanged
//
//    }
}
  • 实现单手旋转,缩放图片
    UIImageView *Imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"manHair1"]];
    Imgv.clipsToBounds = NO;
    Imgv.hidden = YES;
    Imgv.userInteractionEnabled = YES;
    [_personBackImgV addSubview:Imgv];
    _maskImgV = Imgv;
    double m = 150/_maskImgV.image.size.width;
    _maskImgV.frame = CGRectMake(20, 100, 150, _maskImgV.image.size.height*m);
    
    //添加拖到手势
    UIPanGestureRecognizer *gesture1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onDrag:)];
    [_maskImgV addGestureRecognizer:gesture1];
    
    UIImageView *imgvCtrl = [[UIImageView alloc] init];
    imgvCtrl.image = [UIImage imageNamed:@"52px红"];
    imgvCtrl.userInteractionEnabled = YES;
    [_maskImgV addSubview:imgvCtrl];
    _imgvCtrl = imgvCtrl;
    [_imgvCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(_maskImgV).offset(15);
        make.width.height.equalTo(@30);
    }];
    
    UIPanGestureRecognizer *gesture5 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onScaleAndRotate:)];
    [_imgvCtrl addGestureRecognizer:gesture5];
//   手势回调方法
-(void)onScaleAndRotate:(UIPanGestureRecognizer*)gesture{
    
    UIView *viewCtrl = gesture.view;
    UIView *viewImg = _maskImgV;
    
    CGPoint center = viewImg.center;
    CGPoint prePoint = viewCtrl.center;
    CGPoint translation = [gesture translationInView:gesture.view];
    CGPoint curPoint = CGPointMake(prePoint.x+translation.x, prePoint.y+translation.y);
    
    // 计算缩放
    CGFloat preDistance = [self getDistance:prePoint withPointB:center];
    CGFloat curDistance = [self getDistance:curPoint withPointB:center];
    CGFloat scale = curDistance / preDistance;
    
    // 计算弧度
    CGFloat preRadius = [self getRadius:center withPointB:prePoint];
    CGFloat curRadius = [self getRadius:center withPointB:curPoint];
    CGFloat radius = curRadius - preRadius;
    radius = - radius;
    
    CGAffineTransform transform = CGAffineTransformScale(viewImg.transform, scale, scale);
    viewImg.transform = CGAffineTransformRotate(transform, radius);
    
    [gesture setTranslation:CGPointZero inView:viewCtrl];
}
-(CGFloat)getDistance:(CGPoint)pointA withPointB:(CGPoint)pointB{
    CGFloat x = pointA.x - pointB.x;
    CGFloat y = pointA.y - pointB.y;
    
    return sqrt(x*x + y*y);
}

-(CGFloat)getRadius:(CGPoint)pointA withPointB:(CGPoint)pointB{
    CGFloat x = pointA.x - pointB.x;
    CGFloat y = pointA.y - pointB.y;
    return atan2(x, y);
}

参考资料

http://blog.csdn.net/luguogege/article/details/50611283

你可能感兴趣的:(ios 手势的基本使用)