多点触摸与手势识别

         //点击事件
         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. delete drop truncate

    一.相同点 1 truncate.不带where子句的delete.drop都会删除表内的数据2 drop.truncate都是DDL语句(数据定义语言),执行后会自动提交 二.不同点 1trunca ...

  2. Ui篇--layout_weight体验(实现按比例显示)

    在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...

  3. Filezilla中文字符文件看不到或显示乱码的解决办法

    Filezilla确实是跨平台的好软件,可之前我就在ubuntu下郁闷为什么看坛子FTP里竟然是空的.最近换MAC版的FZ结果还是这样就奇怪了. 后来想Filezilla应该是支持字符集转换的,所以在 ...

  4. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(6)

    创建一个新文档(Creating a new document) 现在我们将尝试索引一些文档.对于我们的示例,让我们想象我们正在为我们的博客建立某种CMS.实体之一是博客的文章.使用JSON记法,在以 ...

  5. IOS Swizzle(hook)

    /////////////////////////////////////////////////////////////////////////////////////////////////// ...

  6. JDBC连接工厂类

       看到有些书上数据库连接提供两个工厂类,一个连接工厂类一个关闭工厂类,并且关闭工厂类写了多种重载形式,感觉没有必要,这样写比较简洁一些. /** * 抽象出的连接工厂类,提供连接数据库和关闭连接的 ...

  7. 简易版CSS3 Tab菜单 实用的Tab切换

    今天我们要来分享一款非常简易而又实用的CSS3 Tab菜单,Tab菜单没有非常华丽的动画,但是代码非常简洁易懂,也可以在大部分场合使用,因此也非常实用,如果你需要加入动画效果,也可以自己方便地修改这款 ...

  8. SQL游标遍历数据表

    DECLARE @资产编号 VARCHAR(50) ,@gsid VARCHAR(50) DECLARE test_Cursor CURSOR LOCAL FOR SELECT 资产编号,gsid F ...

  9. Spark系列(五)Master主备切换机制

    Spark Master主备切换主要有两种机制,之中是基于文件系统,一种是基于Zookeeper.基于文件系统的主备切换机制需要在Active Master挂掉后手动切换到Standby Master ...

  10. Hibernate初认识以及HelloWorld

    一.Hibernate初认识 1. Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. 2.对 ...