多点触摸与手势识别

         //点击事件
         var atap = UITapGestureRecognizer(target: self, action: "tapDo:")
         self.view.addGestureRecognizer(atap)
         atap.numberOfTapsRequired =  //单击次数
         atap.numberOfTouchesRequired =  //手指个数

         //拖动事件
         var aPan = UIPanGestureRecognizer(target: self, action: "handlenPan:")
         self.view.addGestureRecognizer(aPan)
         aPan.minimumNumberOfTouches =  //最少手指个数
         aPan.maximumNumberOfTouches =  //最多手指个数

         //长按事件
         var aLongPress = UILongPressGestureRecognizer(target: self, action: "longPress:")
         self.view.addGestureRecognizer(aLongPress)
         aLongPress.minimumPressDuration =  //需要长按的时间,最小0.5s

         //捏合事件
         var aPinch = UIPinchGestureRecognizer(target: self, action: "pinchDo:")
         self.view.addGestureRecognizer(aPinch)

         //旋转事件
         var aRotation = UIRotationGestureRecognizer(target: self, action: "rotatePiece:")
         self.view.addGestureRecognizer(aRotation)

         //轻扫事件--左轻扫
         var leftSwipe = UISwipeGestureRecognizer(target: self, action: "leftSwipe:")
         self.view.addGestureRecognizer(leftSwipe)
         leftSwipe.direction =  UISwipeGestureRecognizerDirection.Left

         //轻扫事件--右轻扫
         var rightSwipe = UISwipeGestureRecognizer(target: self, action: "rightSwipe:")
         self.view.addGestureRecognizer(rightSwipe)
         rightSwipe.direction =  UISwipeGestureRecognizerDirection.Right

         //轻扫事件--上轻扫
         var upSwipe = UISwipeGestureRecognizer(target: self, action: "upSwipe:")
         self.view.addGestureRecognizer(upSwipe)
         upSwipe.direction =  UISwipeGestureRecognizerDirection.Up

         //轻扫事件--下轻扫
         var downSwipe = UISwipeGestureRecognizer(target: self, action: "downSwipe:")
         self.view.addGestureRecognizer(downSwipe)
         downSwipe.direction =  UISwipeGestureRecognizerDirection.Down
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
     }
     */

     //触摸事件

     //手指首次触摸到屏幕

 //    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

         //2015年5月2后修改,另外:touches --》(touches as NSSet)
     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
         println("touchesBegan")

         //获取touches数量
         let numTouches = touches.count

         //获取点击屏幕的次数
         let tapTouches = (touches as NSSet).anyObject()?.tapCount

         //获取事件发生时间
         let timestamp = event.timestamp

         //获取当前相对于self.view的坐标
         let locationPoint = (touches as NSSet).anyObject()?.locationInView(self.view)

         //获取上一次相对于self.view的坐标
         let previousPoint = (touches as NSSet).anyObject()?.previousLocationInView(self.view)

         //允许使用手势
         self.view.userInteractionEnabled = true

         //支持多点触摸
         self.view.multipleTouchEnabled = true

         println("\(tapTouches)")

         //判断如果有两个触摸点

         {
             //获取触摸集合
             let twoTouches = (touches as NSSet).allObjects

             //获取触摸数组
             let first:UITouch = twoTouches[] as! UITouch //第1个触摸点
             let second:UITouch = twoTouches[]as! UITouch //第2个触摸点

             //获取第1个点相对于self.view的坐标
             let firstPoint:CGPoint = first.locationInView(self.view)

             //获取第1个点相对于self.view的坐标
             let secondPoint:CGPoint = second.locationInView(self.view)

             //计算两点之间的距离
             let deltaX = secondPoint.x - firstPoint.x;
             let deltaY = secondPoint.y - firstPoint.y;
             let initialDistance = sqrt(deltaX*deltaX + deltaY*deltaY )

             println("两点间距离是:\(initialDistance)")
         }
     }

     //手指在移动
 //    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

     //2015年5月2后修改
     override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

         println("touchesMoved")
     }

     //触摸结束
 //    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

     //2015年5月2后修改
     override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

         println("touchesEnded")
     }

     //触摸意外终止
     //模拟器演示:鼠标拖动的同时,按键盘command+shift+h 相当于点击手机home键,退出应用,触发touchesCancelled事件,在打电话、等情况下也会触发
 //    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {

     //2015年5月2后修改
     override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {

         println("touchesCancelled")
     }

     //手势

     //点击事件
     func tapDo(sender:UITapGestureRecognizer)
     {

         println("点击事件")
     }

     //拖动事件
     func handlenPan(sender:UIPanGestureRecognizer)
     {
         println("拖动事件")

         if sender.state == .Began
         {
             //拖动开始
         }
         else if sender.state == .Changed
         {
             //拖动过程
         }
         else if sender.state == .Ended
         {
             //拖动结束
         }
     }

     //长摁事件
     func longPress(sender:UILongPressGestureRecognizer)
     {
         println("长摁事件")

     }

     //捏合事件
     func pinchDo(sender:UIPinchGestureRecognizer)
     {
         println("捏合")
     }

     //旋转事件
     func rotatePiece(sender:UIRotationGestureRecognizer)
     {
         println("旋转")
     }

     //轻扫事件--左轻扫
     func leftSwipe(sender:UISwipeGestureRecognizer)
     {
         println("左轻扫")
     }

     //轻扫事件--右轻扫
     func rightSwipe(sender:UISwipeGestureRecognizer)
     {
         println("右轻扫")
     }

     //轻扫事件--上轻扫
     func upSwipe(sender:UISwipeGestureRecognizer)
     {
         println("上轻扫")
     }

     //轻扫事件--下轻扫
     func downSwipe(sender:UISwipeGestureRecognizer)
     {
         println("下轻扫")
     }
     
 

ios开发——实用技术篇Swift篇&多点触摸与手势识别的更多相关文章

  1. ios开发——实用技术篇Swift篇&播放MP3

    播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...

  2. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  3. ios开发——实用技术篇Swift篇&拍照

    拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...

  4. ios开发——实用技术篇Swift篇&照片选择

    照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...

  5. ios开发——实用技术篇Swift篇&系统声音

    系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...

  6. ios开发——实用技术篇Swift篇&视频

    视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...

  7. ios开发——实用技术篇Swift篇&录音

    录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...

  8. ios开发——实用技术篇Swift篇&加速计和陀螺仪

    加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...

  9. ios开发——实用技术篇OC篇&iOS的主要框架

    iOS的主要框架         阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...

随机推荐

  1. Win7远程登录Ubuntu14.04

    Quote: http://www.xp74.com/article/news/6083.htm Method: One:vnc连接,实现图形化登录 优点:图形化操作,较第二种方法快 缺点:效率不是最 ...

  2. Python中的高级数据结构

    数据结构 数据结构的概念很好理解,就是用来将数据组织在一起的结构.换句话说,数据结构是用来存储一系列关联数据的东西.在Python中有四种内建的数据结构,分别是List.Tuple.Dictionar ...

  3. lighttpd为什么要accept多次呢

    在lighttpd网络模型里面我们可以看到以下代码 /* accept()s at most 100 connections directly * * we jump out after 100 to ...

  4. C++容器学习

    以前自学C++的时候就没怎么看容器,一直以来也没怎么编过C++程序,现在想用C++写点东西,突感容器类型有些生疏,故做此笔记.(参考<C++ primer> 容器:容纳特定类型对象的集合. ...

  5. oc_转_构造对象的方法,以及类的继承

    一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情: 1) 使用alloc方法来分配存储空间 ...

  6. SpannableString 记录(转)

    引用 http://blog.csdn.net/rockcoding/article/details/7231756 TextView是用来显示文本的,有时需要给TextView中的个别字设置为超链接 ...

  7. Java基础题

    问题: 1.请对比一下重载和重写的区别. 2.请对比一下接口和抽象类的异同. 3.写出一个单例模式,并说明其优点. 4.用过String.StringBuffer吗,说出他们的异同. 5.什么是值传递 ...

  8. homework-03 图形化化最大子序列和

    你现在使用的代码规范是什么,  和上课前有什么改进? 我们一开始使用的是C++完成的相关程序.本次因为一些原因,改为C#进行编写.因为2013-10-21在VS2012中,所以所有的代码都已经被VS自 ...

  9. JavaScript——对this指针的新理解

    一直以来对this的理解只在可以用,会用,却没有去深究其本质.这次,借着<JavaScript The Good Parts>,作了一次深刻的理解.(所有调试都可以在控制台中看到,浏览器F ...

  10. (hzau)华中农业大学第四届程序设计大赛网络同步赛 G: Array C

    题目链接:http://acm.hzau.edu.cn/problem.php?id=18 题意是给你两个长度为n的数组,a数组相当于1到n的物品的数量,b数组相当于物品价值,而真正的价值表示是b[i ...