//
//  nextViewController.m
#import "nextViewController.h"
#import "my.h"
@interface nextViewController ()

@end

@implementation nextViewController
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createView];
    
}

#pragma mark    ------------------------createView
- (void)createView
{
    self.navigationItem.title =@"GESTUR";
    
    UIImageView *vi =[[UIImageView alloc] init];
    
    vi.tag = 100;
    
    vi.frame = CGRectMake(100, 100, 120, 120);
    
    vi.image = [UIImage imageNamed:@"a"];
    
    [self.view addSubview:vi];
    
    vi.userInteractionEnabled = YES ;
    
#pragma mark    ------------------------单击手势
    //一个视图可以附着多个手势,一个手势只能附着在一个视图上
    // 单击手势
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
    
    //点击次数 默认是1
    tap.numberOfTapsRequired =1 ;
    
    //需要几个手指点击  默认是 1
    tap.numberOfTouchesRequired = 1;
    
    //附着在视图上
    [vi addGestureRecognizer:tap];
    
#pragma mark    ------------------------双击手势
    //双击手势
    UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(onDouble:)];
    
    doubleTap.numberOfTapsRequired = 2;
    
    [vi addGestureRecognizer:doubleTap];
    
    //单击在双击手势识别失败之后,在去识别手势
    [tap requireGestureRecognizerToFail:doubleTap];
    
#pragma mark    ------------------------长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)];
    
    longPress.minimumPressDuration = 1;
    
    [vi addGestureRecognizer:longPress];
    
#pragma mark    ------------------------拖动手势
    //拖动手势 Pan
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPan:)];
    
    [vi addGestureRecognizer:pan];
    
#pragma mark    ------------------------捏合手势
    //捏合 手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(onPinch:)];
    [vi addGestureRecognizer:pinch];
    
    //遵守UIGestureRecognizerDelegate协议
    pinch.delegate = self;
    
#pragma mark    ------------------------旋转手势
    //旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotation:)];
    
    [vi addGestureRecognizer:rotation];
    
    rotation.delegate = self;
    
#pragma mark    ------------------------轻扫手势
    //*********************************************************************
    UISwipeGestureRecognizer *swipedown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
    
    //设置滑动的方向
    swipedown.direction = UISwipeGestureRecognizerDirectionDown;
    
    [vi addGestureRecognizer:swipedown];
    
    //*********************************************************************
    
    UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
    
    swipeleft.direction = UISwipeGestureRecognizerDirectionLeft;
    
    [vi addGestureRecognizer:swipeleft];
    
    //*********************************************************************
    
    UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
    
    swiperight.direction = UISwipeGestureRecognizerDirectionRight;
    
    [vi addGestureRecognizer:swiperight];
    
    //*********************************************************************
    
    UISwipeGestureRecognizer *swipeup = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
    
    swipeup.direction = UISwipeGestureRecognizerDirectionUp;
    
    [vi addGestureRecognizer:swipeup];

}

#pragma mark    ------------------------双击手势
- (void)onDouble:(UITapGestureRecognizer *)ondouble
{
    NSLog(@"图片被双击..");
}

#pragma mark    ------------------------ 单击
- (void)onTap:(UITapGestureRecognizer *)tap
{
    NSLog(@"图片被单击了...");
 
    [self creatUIMenuController:tap];
}

#pragma mark------------------------UIMenuController
- (void)creatUIMenuController:(UITapGestureRecognizer *)tap
{
    
    //菜单  是一个单例 NSUserDefaults  UIApplication
    UIMenuController *menu = [UIMenuController sharedMenuController];
    
    UIMenuItem * item1 = [[UIMenuItem alloc] initWithTitle:@"拷贝" action:@selector(onCopy)];
    
    UIMenuItem * item2 = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(onDelete)];
    
    menu.menuItems = @[item1,item2];
    
    CGPoint point = [tap locationInView:self.view];
    
    NSLog(@"point.x=%f",point.x);
    NSLog(@"point.y=%f",point.y);
    
    //设定坐标
    [menu setTargetRect:CGRectMake(point.x, point.y, 80, 50) inView:self.view];
    
    //显示menu
    [menu setMenuVisible:YES animated:YES];
    
    [self becomeFirstResponder];
}

- (void)onCopy
{
    
}

- (void)onDelete
{
    UIImageView *iv = (id)[self.view viewWithTag:100];
    [iv removeFromSuperview];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}
#pragma mark    ------------------------ 长压
- (void)onPress:(UITapGestureRecognizer *)tap
{
    NSLog(@"图片被长压");
}

#pragma mark    ------------------------ 拖动
- (void)onPan:(UITapGestureRecognizer *)tap
{
    NSLog(@"图片被拖动...");
    
    UIImageView *iv = (id)[self.view viewWithTag:100];
    
    //找到拖动的位置
    CGPoint point = [tap locationInView:self.view];
    
    if(point.x>=self.view.bounds.size.width-10)
        point.x = self.view.bounds.size.width-10;
    
    iv.frame = CGRectMake(point.x, point.y, iv.frame.size.width, iv.frame.size.height);
    
    NSLog(@"%f,%f",point.x,point.y);
    
    //iv.center = point;
}

#pragma mark    ------------------------ 捏合 手势
-(void)onPinch:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"捏合手势");
    
    //放大 缩小
    UIImageView *vi =(id)[self.view viewWithTag:100];
    
    vi.transform = CGAffineTransformScale(vi.transform, pinch.scale, pinch.scale); //  x的缩放比例  y的的缩放比例   相对与与
    
    [pinch setScale:1];  //   缩放比例   1相当与不缩放
}

#pragma mark    ------------------------旋转
- (void)onRotation:(UIRotationGestureRecognizer *)rot
{
    NSLog(@"旋转手势");
    
    UIImageView *vi =(id)[self.view viewWithTag:100];
    
    vi.transform = CGAffineTransformRotate(vi.transform, rot.rotation);
    
    [rot setRotation:0];
}

//pan拖动事件的时候轻扫功能不可用
#pragma mark    ------------------------轻扫
- (void)onSwipe:(UISwipeGestureRecognizer *)swipe
{
    
    UIImageView *vi =(id)[self.view viewWithTag:100];
    
    CGPoint point = [swipe locationInView:self.view];
    
    if(swipe.direction == UISwipeGestureRecognizerDirectionDown)
    {
        static int i = 0;
        NSLog(@"轻扫手势 swip down = %d次",++i);
        
        vi.frame = CGRectMake(100, point.y+2, vi.frame.size.width, vi.frame.size.height);
    }
    else if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        static int i = 0;
        NSLog(@"轻扫手势 swip left = %d次",++i);
    }
    else if(swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {
        static int i = 0;
        NSLog(@"轻扫手势 swip right = %d次",++i);
    }
    else if(swipe.direction == UISwipeGestureRecognizerDirectionUp)
    {
        static int i = 0;
        
        NSLog(@"轻扫手势 swip up = %d次",++i);//???????
        NSLog(@"point.y=%f",point.y);
        vi.frame = CGRectMake(100, point.y-200, vi.frame.size.width, vi.frame.size.height);
        
        NSLog(@"point.y=%f",point.y);
    }
    
    
}

#pragma mark    ------------------------实现代理协议相似的方法//<UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{

//允许相似的手势接受事件,如果方法返回yes就是说允许相似的手势识别事件
    return YES;
}
@end

TouchAndGuest触摸事件和手势的更多相关文章

  1. iOS中的触摸事件和手势处理

    iOS中的事件可以分为三大类: 1> 触摸事件 2> 加速计事件 3> 远程控制事件 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并 ...

  2. iOS开发之触摸事件及手势

    1.iOS中的事件 在用户使用app过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型: 2.响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并 ...

  3. iOS 触摸事件与手势识别器(Gesture Recognizers)

    Gesture Recognizers与触摸事件分发 通过一个问题引出今天的知识: 1.大家应该都遇见过 当需要给tableView 添加一个tap 手势识别 但是tableView 的上的事件(滑动 ...

  4. iOS的触摸事件的用法以及和手势识别器的区别

    1.首先来介绍下触摸事件和手势识别器的利与弊 触摸事件和手势识别器二者之间有直接的关系 手势识别器是在触摸事件的基础上演变过来的 当我们用到触摸事件时 默认的uiview是没有什么效果的 只能自定义v ...

  5. iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...

  6. 移动端-js触摸事件

    开发者工具 在移动开发中,一种较为容易的做法是,先在桌面上开始原型设计,然后再在打算要支持的设备上处理移动特有的部分.多点触摸正是难以在PC上进行测试的那些功能之一,因为大部分的PC都没有触摸输入. ...

  7. 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 转载来自崔江涛(KenshinCui) 链接:http://www.cnblogs.com/kenshincui/p/3950646.html 概览 iPhone的成功很大一 ...

  8. Android 手势&amp;触摸事件 MotionEvent

    1.http://blog.csdn.net/omg_2012/article/details/7881443 这篇相当好啊 2.http://blog.csdn.net/android_tutor/ ...

  9. Android 手势&触摸事件

    在刚开始学Android的时候,就觉得Google的文档不咋样,在研究手势时,更加的感觉Google的文档写得实在是太差了.很多常量,属性和方法,居然连个描述都没有. 没有描述也就罢了,但是OnGes ...

随机推荐

  1. UTF-8 BOM(EF BB BF)

    原标题:link标签和script标签跑到body下面,网页顶部有空白,出现“锘匡豢”乱码,UTF-8 BOM,EF BB BF 来自:http://tunps.com/link-and-script ...

  2. 近期最久未使用页面淘汰算法———LRU算法(java实现)

    请珍惜小编劳动成果,该文章为小编原创,转载请注明出处. LRU算法,即Last Recently Used ---选择最后一次訪问时间距离当前时间最长的一页并淘汰之--即淘汰最长时间没有使用的页 依照 ...

  3. javascript实现单例模式

    1.简单实现单例模式: var singleTon = function(){ var _pria = 'private value'; var show_pria = function(){ con ...

  4. VSS错误:The Sourcesafe Web service cannot be accessed at the specified address

    第一次使用正常,今天再次打开vs项目的时候就突然连不上vss的服务器了.       手动修改连接的时候会让输入一个address(http的) (一般正常的连接会是浏览的方式找到服务器文件的地址的) ...

  5. 根据当前IP获取当时所在信息

    现在很多系统,都要在登录时候,确定当前用户所在的位置.这里记录一个C#使用Http的方式获取当前IP所在的位置信息.主要使用的api是新浪的接口. public partial class sina ...

  6. WPF 路由事件

    最近想封装一个关于手势的控件,但是由其他的控件覆盖之后发现不能触发,据说是有一些事件在定义的时候就处理过e.Handle了. 定义的时候就处理了,就是为了控件能够正常的工作,别如Button.Mous ...

  7. jsp包含的讲解

    <%@page language="java" import="java.util.*" pageEncoding="UTF-8"%& ...

  8. ScrollView嵌套recyclerView出现的滑动问题

    记得以前在解决scrollView与ListView嵌套问题时,那个时候是自定义了listView去测量listView高度,今天项目中刚 好碰到了要用recycerView,同样也是嵌套在scrol ...

  9. nodejs的mysql模块学习(九)连接池集群

    连接池集群 连接池集群可以提供多个主机连接 创建连接池集群 //创建连接池集群 var poolCluster = mysql.createPoolCluster(); //添加配置 config是一 ...

  10. Ubuntu快速显示桌面的方法

    在Ubuntu环境下,按下Ctrl+D就能最小化所有窗口,立刻显示桌面,类似xp下的显示桌面按钮功能.不过这是需要经过快捷键设置的.以下是设置方法: 1.找到"系统设置" 2.进入 ...