ios开发——实用技术篇Swift篇&多点触摸与手势识别

多点触摸与手势识别

 

  1         
  2         //点击事件
  3         var atap = UITapGestureRecognizer(target: self, action: "tapDo:")
  4         self.view.addGestureRecognizer(atap)
  5         atap.numberOfTapsRequired = 1 //单击次数
  6         atap.numberOfTouchesRequired = 1 //手指个数
  7         
  8         //拖动事件
  9         var aPan = UIPanGestureRecognizer(target: self, action: "handlenPan:")
 10         self.view.addGestureRecognizer(aPan)
 11         aPan.minimumNumberOfTouches = 1 //最少手指个数
 12         aPan.maximumNumberOfTouches = 3 //最多手指个数
 13         
 14         //长按事件
 15         var aLongPress = UILongPressGestureRecognizer(target: self, action: "longPress:")
 16         self.view.addGestureRecognizer(aLongPress)
 17         aLongPress.minimumPressDuration = 1 //需要长按的时间,最小0.5s
 18 
 19         //捏合事件
 20         var aPinch = UIPinchGestureRecognizer(target: self, action: "pinchDo:")
 21         self.view.addGestureRecognizer(aPinch)
 22         
 23         //旋转事件
 24         var aRotation = UIRotationGestureRecognizer(target: self, action: "rotatePiece:")
 25         self.view.addGestureRecognizer(aRotation)
 26         
 27         //轻扫事件--左轻扫
 28         var leftSwipe = UISwipeGestureRecognizer(target: self, action: "leftSwipe:")
 29         self.view.addGestureRecognizer(leftSwipe)
 30         leftSwipe.direction =  UISwipeGestureRecognizerDirection.Left
 31         
 32         //轻扫事件--右轻扫
 33         var rightSwipe = UISwipeGestureRecognizer(target: self, action: "rightSwipe:")
 34         self.view.addGestureRecognizer(rightSwipe)
 35         rightSwipe.direction =  UISwipeGestureRecognizerDirection.Right
 36         
 37         //轻扫事件--上轻扫
 38         var upSwipe = UISwipeGestureRecognizer(target: self, action: "upSwipe:")
 39         self.view.addGestureRecognizer(upSwipe)
 40         upSwipe.direction =  UISwipeGestureRecognizerDirection.Up
 41         
 42         //轻扫事件--下轻扫
 43         var downSwipe = UISwipeGestureRecognizer(target: self, action: "downSwipe:")
 44         self.view.addGestureRecognizer(downSwipe)
 45         downSwipe.direction =  UISwipeGestureRecognizerDirection.Down
 46     }
 47 
 48     override func didReceiveMemoryWarning() {
 49         super.didReceiveMemoryWarning()
 50         // Dispose of any resources that can be recreated.
 51     }
 52     
 53 
 54     /*
 55     // MARK: - Navigation
 56 
 57     // In a storyboard-based application, you will often want to do a little preparation before navigation
 58     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
 59         // Get the new view controller using segue.destinationViewController.
 60         // Pass the selected object to the new view controller.
 61     }
 62     */
 63 
 64     
 65     //触摸事件
 66     
 67     //手指首次触摸到屏幕
 68     
 69         
 70 //    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
 71         
 72         
 73         //2015年5月2后修改,另外:touches --》(touches as NSSet)
 74     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
 75         println("touchesBegan")
 76         
 77         //获取touches数量
 78         let numTouches = touches.count
 79         
 80         //获取点击屏幕的次数
 81         let tapTouches = (touches as NSSet).anyObject()?.tapCount
 82         
 83         //获取事件发生时间
 84         let timestamp = event.timestamp
 85         
 86         //获取当前相对于self.view的坐标
 87         let locationPoint = (touches as NSSet).anyObject()?.locationInView(self.view)
 88         
 89         //获取上一次相对于self.view的坐标
 90         let previousPoint = (touches as NSSet).anyObject()?.previousLocationInView(self.view)
 91         
 92         //允许使用手势
 93         self.view.userInteractionEnabled = true
 94         
 95         //支持多点触摸
 96         self.view.multipleTouchEnabled = true
 97         
 98         println("\(tapTouches)")
 99         
100         
101         //判断如果有两个触摸点
102         if touches.count == 2
103         {
104             //获取触摸集合
105             let twoTouches = (touches as NSSet).allObjects
106             
107             //获取触摸数组
108             let first:UITouch = twoTouches[0] as! UITouch //第1个触摸点
109             let second:UITouch = twoTouches[1]as! UITouch //第2个触摸点
110             
111             //获取第1个点相对于self.view的坐标
112             let firstPoint:CGPoint = first.locationInView(self.view)
113             
114             //获取第1个点相对于self.view的坐标
115             let secondPoint:CGPoint = second.locationInView(self.view)
116             
117             //计算两点之间的距离
118             let deltaX = secondPoint.x - firstPoint.x;
119             let deltaY = secondPoint.y - firstPoint.y;
120             let initialDistance = sqrt(deltaX*deltaX + deltaY*deltaY )
121             
122             println("两点间距离是:\(initialDistance)")
123         }
124     }
125     
126     //手指在移动
127 //    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
128     
129     //2015年5月2后修改
130     override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
131         
132         println("touchesMoved")
133     }
134     
135     //触摸结束
136 //    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
137     
138     //2015年5月2后修改
139     override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
140         
141         println("touchesEnded")
142     }
143     
144     //触摸意外终止
145     //模拟器演示:鼠标拖动的同时,按键盘command+shift+h 相当于点击手机home键,退出应用,触发touchesCancelled事件,在打电话、等情况下也会触发
146 //    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
147     
148     //2015年5月2后修改
149     override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
150         
151         println("touchesCancelled")
152     }
153     
154     
155     
156     
157     
158     
159     //手势
160     
161     //点击事件
162     func tapDo(sender:UITapGestureRecognizer)
163     {
164         
165         println("点击事件")
166     }
167     
168     //拖动事件
169     func handlenPan(sender:UIPanGestureRecognizer)
170     {
171         println("拖动事件")
172         
173         if sender.state == .Began
174         {
175             //拖动开始
176         }
177         else if sender.state == .Changed
178         {
179             //拖动过程
180         }
181         else if sender.state == .Ended
182         {
183             //拖动结束
184         }
185     }
186     
187     //长摁事件
188     func longPress(sender:UILongPressGestureRecognizer)
189     {
190         println("长摁事件")
191         
192         
193     }
194     
195     //捏合事件
196     func pinchDo(sender:UIPinchGestureRecognizer)
197     {
198         println("捏合")
199     }
200 
201     //旋转事件
202     func rotatePiece(sender:UIRotationGestureRecognizer)
203     {
204         println("旋转")
205     }
206 
207     
208     //轻扫事件--左轻扫
209     func leftSwipe(sender:UISwipeGestureRecognizer)
210     {
211         println("左轻扫")
212     }
213     
214     //轻扫事件--右轻扫
215     func rightSwipe(sender:UISwipeGestureRecognizer)
216     {
217         println("右轻扫")
218     }
219     
220     //轻扫事件--上轻扫
221     func upSwipe(sender:UISwipeGestureRecognizer)
222     {
223         println("上轻扫")
224     }
225     
226     //轻扫事件--下轻扫
227     func downSwipe(sender:UISwipeGestureRecognizer)
228     {
229         println("下轻扫")
230     }
231     

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。