ios开发——实用技术篇Swift篇&多点触摸与手势识别
多点触摸与手势识别
//点击事件 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篇&多点触摸与手势识别的更多相关文章
- ios开发——实用技术篇Swift篇&播放MP3
播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...
- ios开发——实用技术篇Swift篇&地址薄、短信、邮件
//返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...
- ios开发——实用技术篇Swift篇&拍照
拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...
- ios开发——实用技术篇Swift篇&照片选择
照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...
- ios开发——实用技术篇Swift篇&系统声音
系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...
- ios开发——实用技术篇Swift篇&视频
视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...
- ios开发——实用技术篇Swift篇&录音
录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...
- ios开发——实用技术篇Swift篇&加速计和陀螺仪
加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...
- ios开发——实用技术篇OC篇&iOS的主要框架
iOS的主要框架 阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...
随机推荐
- codedorces 260 div2 A题
水题,扫描一遍看是否出现价格低质量高的情况. #include<cstdio> #include<string> #include<vector> #include ...
- 《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 && 检查一个字符串是文本还是二进制
过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: impor ...
- Tkinter教程之Checkbutton篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811306 #Tkinter教程之Checkbutton篇#Checkbutton又称为多选按 ...
- 在eclipse中配置spark 0.9.1源码的开发环境
一.准备工作: 1.下载release版spark 0.9.1,本次用的版本是spark-0.9.1-bin-cdh4. 2.下载scala IDE 3.0.2,这个版本ide的支持scala 2.1 ...
- 关于put 上传图片的解决方式
客户端: 因为put只支持单一类型的资源进行传输,所以不能使用像 Multipart/form-data这样的content-type进行描述,而只能使用像image/jpeg .image/png的 ...
- 利用phantomjs模拟QQ自动登录
之前为了抓取兴趣部落里的数据,研究了下QQ自动登录. 当时搜索了一番,发现大部分方法都已经失效了,于是准备自己开搞. 第一个想到的就是参考网上已有方案的做法,梳理登陆js的实现,通过其他语言重写.考虑 ...
- sizeof 字符数组
比较 #include <stdio.h> #include <string.h> int main(int argc, const char *argv[]) { char ...
- Red5源代码分析 - 关键类及其初始化过程
原文地址:http://semi-sleep.javaeye.com/blog/348768 Red5如何响应rmpt的请求,中间涉及哪些关键类? 响应请求的流程如下: 1.Red5在启动时会调用RT ...
- HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
- POJ 1062 昂贵的聘礼 (最短路)
昂贵的聘礼 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/M Description 年轻的探险家来到了一个印第安部落里.在那里 ...