手势识别之平移、缩放、长按、旋转、滑动

前面了解了手势识别的点击以及代理方法,其他的几个手势识别都是差不多

#import "ViewController.h"



@interface ViewController ()

@property(nonatomic,strong)UIImageView *imgView;



@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

   

    self.imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"test.jpg"]];

    self.imgView.frame=CGRectMake(100, 100, 100, 100);

    self.imgView.userInteractionEnabled=YES;

    self.imgView.multipleTouchEnabled=YES;

    [self.view addSubview:self.imgView];

    

     //拖动

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

    [self.imgView addGestureRecognizer:panGestureRecognizer];

    

    //滑动手势

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

    //方向

    swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;

    [self.imgView addGestureRecognizer:swipeGestureRecognizer];

    

    //长按手势

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

     //按的最短时间

    longPressGestureRecongnizer.minimumPressDuration=2.0;

    // 长按手势识别之前点击次数

    longPressGestureRecongnizer.numberOfTapsRequired=1;

    //需要几个手指

    longPressGestureRecongnizer.numberOfTouchesRequired=1;

    //运行偏移量

    longPressGestureRecongnizer.allowableMovement=10.0;

    [self.imgView addGestureRecognizer:longPressGestureRecongnizer];

    

    //旋转

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

    [self.imgView addGestureRecognizer:rotationGestureRecognizer];

    

    //缩放

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

    [self.imgView addGestureRecognizer:pinchGestureRecognizer];

    

    

}

//平移

-(void)panGesture:(UIPanGestureRecognizer*)pan

{

    //在View中的位置

    CGPoint point=[pan locationInView:pan.view];

    //在View中的移动量 以手指按下的为原点

    CGPoint point1=[pan translationInView:self.view];

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

//    NSLog(@"%@",NSStringFromCGPoint(point1));

    CGPoint temp=self.imgView.center;

    temp.x+=point1.x;

    temp.y+=point1.y;

    self.imgView.center=temp;

    //偏移量是增加的应该设为0

    [pan setTranslation:CGPointZero inView:pan.view];

}

//长按

-(void)longPressGesture:(UIGestureRecognizer*)gestureRecognizer

{

    NSLog(@"UILongPressGestureRecognizer");

}

 //滑动

-(void)swipeGesture:(UIGestureRecognizer *)gestureRecognizer

{

     NSLog(@"UISwipeGestureRecognizer");

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    

}

//旋转

-(void)rotationGesture:(UIRotationGestureRecognizer *)gestureRecognizer

{

    NSLog(@"旋转的角度:%lf",gestureRecognizer.rotation);

//    self.imgView.transform=CGAffineTransformMakeRotation(gestureRecognizer.rotation);

    self.imgView.transform=CGAffineTransformRotate(self.imgView.transform, gestureRecognizer.rotation);

    gestureRecognizer.rotation=0;

}

//缩放

-(void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture

{

    NSLog(@"%lf",pinchGesture.scale);

//    self.imgView.transform=CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);

    self.imgView.transform=CGAffineTransformScale(self.imgView.transform, pinchGesture.scale, pinchGesture.scale);

    //回初始值

    pinchGesture.scale=1.0;

}



@end

 

你可能感兴趣的:(缩放)