手势识别--拖动,旋转,缩放

来源:互联网 发布:淘宝运营开放平台 编辑:程序博客网 时间:2024/06/13 07:04


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.textLabel.text = NSLocalizedString(@"哈哈哈",@"sssss");


    self.imageV.userInteractionEnabled =YES;

//    拖动

    [self panGes];

//    捏合

    [selfpinchGes];

//    旋转

    [selfrotation];

    

}

//通过代理 、delegate进行判断是否支持多个手势同事进行

//shouldRecognizeSimultaneouslyWithGestureRecognizer

//是否能允许同事支持多个手势

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

{

    return YES;

}


-(void)rotation {

//    旋转

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationGes:)];

    [self.imageVaddGestureRecognizer:rotation];

}

-(void)rotationGes:(UIRotationGestureRecognizer *)rotationGes

{

    self.imageV.transform =CGAffineTransformRotate(self.imageV.transform, rotationGes.rotation);

    rotationGes.delegate = self;

//    复位

    [rotationGes setRotation:0];

    NSLog(@"旋转");

    

}



-(void)pinchGes

{

//    捏合

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)];

    pinch.delegate = self;

    [self.imageVaddGestureRecognizer:pinch];

}

-(void)pinch:(UIPinchGestureRecognizer *)pinch

{

    NSLog(@"%s",__func__);

    self.imageV.transform =CGAffineTransformScale(self.imageV.transform, pinch.scale, pinch.scale);

//        进行复位

    [pinch setScale:1];

}




#pragma mark ----拖动

-(void)panGes

{

    //    添加手势

    UIPanGestureRecognizer *Pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];

    [self.imageVaddGestureRecognizer:Pan];

}

//拖动View时调用

-(void)pan:(UIPanGestureRecognizer *)pan

{

//    偏移量获取(可以 直接获取,不用计算了)  相当于原始的偏移量!!!

     CGPoint transP =  [pan translationInView:self.imageV];

    NSLog(@"%@",NSStringFromCGPoint(transP));

    self.imageV.transform  = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);

//    让它相对于上一次 吧上次的值给清空,进行复位

    [pan setTranslation:CGPointZeroinView:self.imageV];

    

    NSLog(@"拖动View");

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end


阅读全文
0 0