宽容他人,放过自己。

各种手势总结_手势识别器

Posted on By anchoriteFili

132018496299281.png

获取view上点击的位置坐标

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
#pragma mark 获取现在的点击位置
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    
    NSLog(@"x ===== %f y ===== %f",touchPoint.x,touchPoint.y);
//    int x = (CGRectGetWidth(self.view.frame) - 26) / 2 + CGRectGetMinX(self.view.frame);
//    int y = CGRectGetMinY(sender.frame) + CGRectGetMinY(_liveView.live_menu_panel.frame);
//    [self beginAnimationWithX:touchPoint.x withY:touchPoint.y];
    
}

总结

@synthesize imageView;

CGFloat lastScaleFactor=1;//放大、缩小
CGFloat  netRotation;//旋转
CGPoint netTranslation;//平衡
NSArray *images;//图片数组
int imageIndex=0;//数组下标

- (void)viewDidLoad
{
    //1、创建手势实例,并连接方法handleTapGesture,点击手势
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
    //设置手势点击数,双击:点2下
    tapGesture.numberOfTapsRequired=2;
    // imageView添加手势识别
    [imageView addGestureRecognizer:tapGesture];
    //释放内存
    [tapGesture release];
    
    //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
    UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
    [imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别
    [pinchGesture release];
    
    //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
    UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];
    [imageView addGestureRecognizer:rotateGesture];
    [rotateGesture release];
    
    //4、拖手势
    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
   // [imageView addGestureRecognizer:panGesture];
    [panGesture release];
    
    //5、划动手势
    images=[[NSArray alloc]initWithObjects:@"cell.jpg",@"heihua.jpg",@"xuanyi.jpg", nil];
    //右划
    UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
    [imageView addGestureRecognizer:swipeGesture];
    [swipeGesture release];
    //左划
    UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右
    [imageView addGestureRecognizer:swipeLeftGesture];
    [swipeLeftGesture release];
    
    //6、长按手势
    UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
    //长按时间为1秒
    longpressGesutre.minimumPressDuration=1;
    //允许15秒中运动
    longpressGesutre.allowableMovement=15;
    //所需触摸1次
    longpressGesutre.numberOfTouchesRequired=1;
    [imageView addGestureRecognizer:longpressGesutre];
    [longpressGesutre release];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//双击屏幕时会调用此方法,放大和缩小图片
-(IBAction)handleTapGesture:(UIGestureRecognizer*)sender{
    //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小
    if(sender.view.contentMode==UIViewContentModeScaleAspectFit){
        //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView
        sender.view.contentMode=UIViewContentModeCenter;
    }else{
        sender.view.contentMode=UIViewContentModeScaleAspectFit;
    }
}
//捏的手势,使图片放大和缩小,捏的动作是一个连续的动作
-(IBAction)handlePinchGesture:(UIGestureRecognizer*)sender{
    //得到sender捏手势的大小
    CGFloat factor=[(UIPinchGestureRecognizer*)sender scale];
    if(factor>1){
        //图片放大
        sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor+(factor-1), (lastScaleFactor+(factor-1)));
                                                         
    }else{
        //缩小
        sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor);
                                                         
    }
    //状态是否结束,如果结束保存数据
    if(sender.state==UIGestureRecognizerStateEnded){
        if(factor>1){
            lastScaleFactor+=(factor-1);
        }else{
            lastScaleFactor*=factor;
        }
    }
}
//旋转手势
-(IBAction)handleRotateGesture:(UIGestureRecognizer*)sender{
    //浮点类型,得到sender的旋转度数
    CGFloat rotation=[(UIRotationGestureRecognizer*)sender rotation];
    //旋转角度CGAffineTransformMakeRotation
    CGAffineTransform transform=CGAffineTransformMakeRotation(rotation+netRotation);
    //改变图像角度
    sender.view.transform=transform;
    //状态结束,保存数据
    if(sender.state==UIGestureRecognizerStateEnded){
        netRotation+=rotation;
    }
     
}
//拖手势
-(IBAction)handlePanGesture:(UIGestureRecognizer*)sender{
    //得到拖的过程中的xy坐标
    CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:imageView];
    //平移图片CGAffineTransformMakeTranslation
    sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y);
    //状态结束,保存数据
    if(sender.state==UIGestureRecognizerStateEnded){
        netTranslation.x+=translation.x;
        netTranslation.y+=translation.y;
    }
    
}
//划动手势
-(IBAction)handleSwipeGesture:(UIGestureRecognizer*)sender{
    //划动的方向
    UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer*) sender direction];
    //判断是上下左右
    switch (direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"up");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"down");
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"left");
            imageIndex++;//下标++
            break;
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"right");
            imageIndex--;//下标--
            break;
        default:
            break;
    }
    //得到不越界不<0的下标
    imageIndex=(imageIndex<0)?([images count]-1):imageIndex%[images count];
    //imageView显示图片
    imageView.image=[UIImage imageNamed:[images objectAtIndex:imageIndex]];
    
}
//长按手势
-(IBAction)handleLongpressGesture:(UIGestureRecognizer*)sender{
    //创建警告
    UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"Image options" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save Image",@"Copy", nil];
    //当前view显示警告
    [actionSheet showInView:self.view];
    [actionSheet release];
}
-(void)dealloc{
    [images release];
    [imageView release];
    [super dealloc];
}

各手势的创建

#pragma mark 创建点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
//    点击次数设置
    tap.numberOfTapsRequired = 1;
//    触屏次数的设置
    tap.numberOfTouchesRequired = 1;
//    添加点击手势
    [self.view addGestureRecognizer:tap];
    [tap release];
    
#pragma mark 创建平移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
    [pan release];
    
#pragma mark 创建捏合手势
    UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [self.view addGestureRecognizer:pinchGest];
    [pinchGest release];
    
#pragma mark 创建旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [self.view addGestureRecognizer:rotation];
    [rotation release];
    
#pragma mark 创建轻扫手势,上下与左右两两分开能够全部实现
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//    设置轻扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipe];
    [swipe release];
    
    UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipe1.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipe1];
    [swipe1 release];

各手势触发事件的实现

#pragma mark 轻扫事件实现
- (void)swipe:(UISwipeGestureRecognizer *)swipeGest {
    NSLog(@"轻扫");
}

#pragma mark 旋转手势实现
- (void)rotation:(UIRotationGestureRecognizer *)rotationGest {
//    设置View的transform,来设置旋转
    self.redView.transform = CGAffineTransformRotate(self.redView.transform, rotationGest.rotation);
//    设置转速
    rotationGest.rotation = 1;
}

#pragma mark 捏合手势实现
- (void)pinch:(UIPinchGestureRecognizer *)pinchGest {
    self.redView.transform = CGAffineTransformScale(self.redView.transform, pinchGest.scale, pinchGest.scale);
}

#pragma mark 平移手势实现
- (void)pan:(UIPanGestureRecognizer *)panGest {
//    如果触摸的是textField,执行方法
    if (self.touchView == self.textField) {
        CGPoint translation1 = [panGest translationInView:self.textField];
        [panGest setTranslation:CGPointZero inView:self.textField];
        self.textField.transform = CGAffineTransformTranslate(self.textField.transform, translation1.x, translation1.y);
//        如果触摸的是redView执行方法
    }else if (self.touchView == self.redView)  {
        CGPoint translation = [panGest translationInView:self.redView];
        [panGest setTranslation:CGPointZero inView:self.redView];
        self.redView.transform  = CGAffineTransformTranslate(self.redView.transform, translation.x, translation.y);
    }
}

#pragma mark 点击事件收回键盘
- (void)tap:(UITapGestureRecognizer *)tapGest {
    [self.view endEditing:YES];
}

在触摸屏时触发的各个事件

#pragma mark 触屏开始是触发事件_获取触屏开始时的点坐标
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    CGPoint startPoint = [[touches anyObject] locationInView:self];
    self.startPoint = startPoint;
}

#pragma mark 在屏上移动时触发事件
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
//    让_target执行_action方法
    [_target performSelector:_action withObject:self.redView];
    
//    设置代理方法
    [_delegate changeColor:self.redView];
    [_delegate changePosition:self.redView];
    [_delegate changeColorAndPosition:self.redView];
    
//    float red = (arc4random() % 256) / 255.0;
//    float green = (arc4random() % 256) / 255.0;
//    float blue = (arc4random() % 256) / 255.0;
//    
//    UIColor *randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:0.8];
//    self.redView.backgroundColor = randomColor;
    
//    获取触摸点
    UITouch *touch = [touches anyObject];
//    移动前点击的点
    CGPoint prePoint = [touch previousLocationInView:self];
//    现在的点击点
    CGPoint nowPoint = [touch locationInView:self];
//    计算偏移量
    float offSetx = nowPoint.x - prePoint.x;
    float offSety = nowPoint.y - prePoint.y;
//    获取触摸的View
    UIView *touchView = [touch view];
    
//    如果触摸的View为redView,则改变redView的偏移量
    if (touchView == self.redView) {
        CGPoint center = self.redView.center;
        center.x += offSetx;
        center.y += offSety;
        self.redView.center = center;
    }
}

#pragma mark 触屏结束时触发事件_模拟划屏手势
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//    获取触摸停止时的点坐标
    CGPoint endPoint = [[touches anyObject] locationInView:self];
//    求出开始和结束时的坐标差
    float dx = endPoint.x - self.startPoint.x;
    float dy = endPoint.y - self.startPoint.y;
//    根据坐标差来实现屏的移动方向
    if (dx > 50 && fabs(dy) <= 50) {
        NSLog(@"向右");
    }else if (dx < - 50 && fabs(dy) <= 50) {
        NSLog(@"向左");
    }else if (fabs(dx) <= 50 && dy > 50) {
        NSLog(@"向下");
    }else if (fabs(dx) <= 50 && dy < - 50) {
        NSLog(@"向上");
    }
    
}

#pragma mark 触屏取消时触发事件
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    
}