首页 技术 正文
技术 2022年11月13日
0 收藏 477 点赞 4,801 浏览 4415 个字

多点触摸与手势识别

         //点击事件         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("下轻扫")     }     

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,093
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,570
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,417
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,190
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908