多点触摸与手势识别

         //点击事件
         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. WebView(网络视图)的两种使用方式

    WebView(网络视图)能加载显示网页,可以将其视为一个浏览器.它使用了WebKit渲染引擎加载显示网页,实现WebView有以下两种不同的方法:第一种方法的步骤:1.在要Activity中实例化W ...

  2. How to Calculate difference between two dates in C# z

    Do you need to find the difference in number of days, hours or even minute between the two date rang ...

  3. C语言内存地址基础

    来源:http://blog.jobbole.com/44845/ 从计算机内存的角度思考C语言中的一切东东,是挺有帮助的.我们可以把计算机内存想象成一个字节数组,内存中每一个地址表示 1 字节.比方 ...

  4. lightoj 1021 (数位DP)

    题意:给你一个b进制的数,再给你一个十进制数k,你可以重新排列b进制数的每一位得到其他b进制数,问你这些数中有多少可以整除k? 思路:数位dp. #include <cstdio> #in ...

  5. Java进程占用CPU资源过多分析

    问题描述: 生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析: 1,程序属于CPU密集型,和开发沟通 ...

  6. 【C++对象模型】构造函数语意学之一 默认构造函数

    默认构造函数,如果程序员没有为类定义构造函数,那么编译器会在[需要的时候]为类合成一个构造函数,而[需要的时候]分为程序员需要的时候和编译器需要的时候,程序员需要的时候应该由程序员来做工作,编译器需要 ...

  7. dbms_file_transfer使用简介

    dbms_file_transfer这个包可以在两个位置传输文件,分别可以有以下位置: a 从一个asm diskgroup传输到另外一个asm diskgroup b 从一个asm diskgrou ...

  8. Codeforces 375

    A 7的所有的余数都可以用1,6,8,9排列得到,然后搞一下就可以了. B 可以用类似于单调队列的东西搞.具体看代码: /* * Problem: B. Maximum Submatrix 2 * A ...

  9. HDU 4891 The Great Pan (模拟)

    The Great Pan 题目链接: http://acm.hust.edu.cn/vjudge/contest/123554#problem/D Description As a programm ...

  10. Spring EL hello world example

    The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...