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     
时间: 2025-01-01 11:34:23

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

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

1 //返回按钮事件 2 @IBAction func backButtonClick() 3 { 4 self.navigationController?.popViewControllerAnimated(true) 5 } 6 7 //新增联系人 8 @IBAction func addPeople () 9 { 10 //取得电话薄句柄 11 var error:Unmanaged<CFError>? 12 var addressBook: ABAddressBookRef? = AB

ios开发——实用技术篇Swift篇&amp;播放MP3

播放MP3 1 // MARK: - 播放MP3 2 /*----- mp3 ------*/ 3 //定时器- 4 func updateTime() 5 { 6 //获取音频播放器播放的进度,单位秒 7 var cuTime:Float = Float(audioPlayer.currentTime) 8 9 //更新进度条 10 jinDuSlider.value = cuTime 11 12 //获取总时间 13 var duTime:Float = Float(audioPlayer.

ios开发——实用技术篇Swift篇&amp;系统声音

系统声音 1 // MARK: - 系统声音 2 /*----- 系统声音 ------*/ 3 @IBAction func systemSound() 4 { 5 //建立的SystemSoundID对象 6 var soundID: SystemSoundID = 0 7 8 //获取声音文件地址 9 var path = NSBundle.mainBundle().pathForResource("SaoMa", ofType: "wav") 10 11 /

ios开发——实用技术篇Swift篇&amp;录音

录音 1 // MARK: - 录音 2 /*----- 录音 ------*/ 3 4 var recorder:AVAudioRecorder? //录音器 5 var player:AVAudioPlayer? //播放器 6 var recorderSettingsDic:[NSObject : AnyObject]? //录音器设置参数数组 7 var volumeTimer:NSTimer!//定时器线程, 刷新音量 8 var aacPath:String? //录音存储路径 9

ios开发——实用技术篇Swift篇&amp;照片选择

照片选择 1 // MARK: - 选择照片 2 /*----- 选择照片 ------*/ 3 @IBAction func addImageButtonClick() 4 { 5 let actionSheet = UIActionSheet(title: "请选择", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "从相

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

加速计和陀螺仪 1 //返回按钮事件 2 @IBAction func backButtonClick() 3 { 4 self.navigationController?.popViewControllerAnimated(true) 5 } 6 7 8 @IBOutlet var xLabel:UILabel! 9 @IBOutlet var yLabel:UILabel! 10 @IBOutlet var zLabel:UILabel! 11 12 @IBOutlet var orient

ios开发——实用技术篇Swift篇&amp;视频

视频 1 // MARK: - 播放视频 2 /*----- 播放视频 ------*/ 3 4 5 func moviePlayerPreloadFinish(notification:NSNotification) 6 { 7 println("播放完毕") 8 } 9 10 //声明一个媒体播放器 11 var moviePlayer:MPMoviePlayerController? 12 13 @IBAction func playMV() 14 { 15 let filePa

ios开发——实用技术篇Swift篇&amp;拍照

拍照 1 // MARK: - 拍照 2 func fromPhotograph() 3 { 4 if UIImagePickerController.isSourceTypeAvailable(.Camera) 5 { 6 //创建图片控制器 7 let picker = UIImagePickerController() 8 9 //设置代理 10 picker.delegate = self 11 12 //设置来源 13 picker.sourceType = UIImagePicker

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

iOS的主要框架         阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core Graphics框架帮助你创建图形 Core Animation允许你创建高级的动画和虚拟效果 OpenGL ES 框架提供2D和3D绘图工具 将别的框架添加到工程里 本文是<Sunvey the Major Framworks>一文的翻译 框架是一个目录,这个目录包含了共享库,访问共享库里代码