手势操作

1.创建手势识别的实例

2.设置手势识别器的属性

3.将手势识别添加附加到指定的视图上

4.编写监听的方法

手势的状态:

recognizer.state

{

// 常用的四个:

UIGestureRecognizerStateBegan  // 开始

UIGestureRecognizerStateChanged// 改变

UIGestureRecognizerStateEnded // 结束

UIGestureRecognizerStateCancelled// 取消

}

获取这个状态时坐标:

CGPoint loc=[recognizer locationInView:self];

点按:

#pragma  mark - 点按
- (IBAction)tapBtnClick {

    // 创建手势的实例

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
      // 设置属性:
    // 敲几下
    tap.numberOfTapsRequired=2;
    tap.numberOfTouchesRequired=2; // 几个手指敲
    // 添加到这个view中

    [self.myView addGestureRecognizer:tap];

}
  // 实现方法
- (void)tapAction:(UIGestureRecognizer *)recognizer{

    NSLog(@"我被点按了");
}
长按:
#pragma  mark - 长按

- (IBAction)longPressBtnClick:(id)sender {

    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];

    [self.myView addGestureRecognizer:longPress];

}
- (void)longPressAction:(UIGestureRecognizer *)recognizer{

    // 要设置他只执行一次,因为在这个时候是会有小bug的

    if (recognizer.state!=UIGestureRecognizerStateBegan) {
        return;
    }

    NSLog(@"被长按了");
}
缩放:- (IBAction)scaleBtnClick:(id)sender {

    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];

    pinch.delegate=self;

    [self.myView addGestureRecognizer:pinch];
}

- (void)pinchAction:(UIPinchGestureRecognizer *)recognizer{

    // 咱们是去去缩放这个view 就是去改变这个的transform的属性
    CGFloat scale= recognizer.scale;

    self.myView.transform=CGAffineTransformScale(self.myView.transform, scale, scale);

    recognizer.scale=1;

}
拖拽:
#pragma  mark - 拖拽

- (IBAction)panBtnClick:(id)sender {

    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

    [self.myView addGestureRecognizer:pan];
}

- (void )panAction:(UIPanGestureRecognizer *)recognizer{

    // 拖拽也是去改变它的transform

    // 1.获取移动的距离

    CGPoint distance=[recognizer translationInView:self.view];

    //2.移动图片框

    self.myView.transform=CGAffineTransformTranslate(self.myView.transform, distance.x, distance.y);

    //3.清除移动距离

    [recognizer setTranslation:CGPointZero inView:self.view];
}
旋转
#pragma  mark - 旋转

- (IBAction)rotaionBtnClick:(id)sender {

    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotaionAction:)];

    rotation.delegate=self;

    [self.myView addGestureRecognizer:rotation];
}

- (void)rotaionAction:(UIRotationGestureRecognizer *)recognizer{

    CGFloat angle=recognizer.rotation;

    self.myView.transform=CGAffineTransformRotate(self.myView.transform, angle);

    recognizer.rotation=0;
}
轻扫:
#pragma  mark - 轻扫

- (IBAction)swipBtnclick:(id)sender {

    UISwipeGestureRecognizer *swip1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];

   // swip1.description=UISwipeGestureRecognizerDirectionRight;
    [self.myView addGestureRecognizer:swip1];

    UISwipeGestureRecognizer *swip2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];

     swip2.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.myView addGestureRecognizer:swip2];

}

- (void)swipAction:(UISwipeGestureRecognizer *)recognizer{

    //
    NSLog(@"轻扫");

    // 根据方向进行判断

    if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
        // 向左
        self.myView.transform=CGAffineTransformTranslate(self.myView.transform, -200, 0);

        // 再通过动画返回

        [UIView animateWithDuration:0.5 animations:^{

            self.myView.transform=CGAffineTransformIdentity ;

        }];

    }else{

        // 向右

        self.myView.transform=CGAffineTransformTranslate(self.myView.transform, 200, 0);

        // 再通过动画返回

        [UIView animateWithDuration:0.5 animations:^{

            self.myView.transform=CGAffineTransformIdentity ;

        }];

    }

}
多手势操作:
#pragma mark - 多手势识别 三步

/*
 // 创建代理

// 遵循协议

 //实现方法
 */

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

    return YES;

}
时间: 2024-08-06 07:58:24

手势操作的相关文章

iOS开发——仿Clear纯手势操作的UITableView

前言 在Clear应用中,用户无需任何按钮,纯靠不同的手势就可以完成对ToDoItem的删除.完成.添加.移动.具体来说,功能上有左划删除,右划完成,点击编辑,下拉添加.捏合添加.长按移动.这里将这些功能实现并记录. 左划删除与右划完成 所谓的左右滑动,就是自定义一个cell然后在上面添加滑动手势.在处理方法中计算偏移量,如果滑动距离超过cell宽度一半,就删除它,或者是为文本添加删除线等来完成它:如果没有超过一半,那么就用动画把cell归位. 效果图如下: 关键代码如下: - (void)ha

手势操作(单击手势,长按手势,策划手势)

1.策划手势操作 // // ViewController.m // 1-28策划手势 // // Created by ma c on 16/1/28. // Copyright © 2016年 bjsxt. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (strong, nonatomic) UISwipeGestureRecognizer *rec

iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotationGestureRecognizer(旋转) UITapGestureRecognizer(点按) UILo

Android学习指南之三十八:Android手势操作编程[转]

手势操作在我们使用智能设备的过程中奉献了不一样的体验.Android开发中必然会进行手势操作方面的编程.那么它的原理是怎样的呢?我们如何进行手势操作编程呢? 手势操作原理 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchListener监听,在其onTouch()方法里获得该MotionEvent对象. 3. 通过GestureDetector(手势识别器)转发次MotionEvent对

iOS-画板程序(手势操作无)

// // HMPaintView.h // 画板 // // Created by YaguangZhu on 15/9/10. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <UIKit/UIKit.h> @interface HMPaintView : UIView @property(nonatomic,assign)CGFloat width; @property(nonatomic,strong

自定义Imageview控件实现多种手势操作 (拖动、水平缩放、竖直缩放、等比例缩放、双击、长按)

项目中需要使用自定义控件的多种手势操作,之前在网上查阅资料的时候发现能找到的一般是只实现了其中的几种,这次就把我做的控件分享一下,人人为我,我为人人嘛,哈哈! 这个自定义控件实现的主要功能是控件的拖动和缩放(注意:不是对控件中的图片进行操作,话说很多帖子都把这两个混了),其中缩放可以按照三个方向进行,就是水平.竖直和等比例.双击操作只做了一个提示,长按加上了一个简单的弹出菜单. 抱歉的是没有足够的时间写详细注释了,如果跟你需要的功能相同就请直接调用,要是需要改代码就费点神自己读懂代码吧,看不懂的

Android的手势操作识别

摘要 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchListener监听,在其onTouch()方法里获得该MotionEvent对象. 3. 通过GestureDetector(手势识别器)转发次MotionEvent对象 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchLi

Android手势操作使用Fling,Scroll等Gesture

Android手势操作  一盏灯, 一片昏黄: 一简书, 一杯淡茶. 守着那一份淡定, 品读属于自己的寂寞. 保持淡定, 才能欣赏到最美丽的风景! 保持淡定, 人生从此不再寂寞. 前言利用手势操作在现在的APP中越来越普及,大多数时候使用Fling,Scroll等Gesture能大幅度提高用户的操作体验,特别是大屏手机返回键程越来越大的现状下.  在Android系统下,手势识别是通过Gestur... douban.com/doulist/38811866/douban.com/doulist

转载:Windows 8 输入: 边缘手势, 手势操作, 手势识别

重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别 作者:webabcd 介绍 重新想象 Windows 8 Store Apps 之 手势 监测边缘手势 手势操作 - Manipulate 的应用(位移手势,缩放手势,旋转手势) 手势识别 - GestureRecognizer 的应用 示例 1.演示如何监测边缘手势 Input/Touch/EdgeGestureDemo.xaml <Page x:Class="XamlDemo.I