Swift UITouch 的使用
2016-07-20 12:48
211 查看
touch 允许多点触碰
在viewLoad()中
self.view.multipleTouchEnabled = true;
}
在viewLoad()中
self.view.multipleTouchEnabled = true;
/* override (重写): 方法名、参数、返回值相同。2.子类方法不能缩小父类方法的访问权限 3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。4.存在于父类和子类之间 5.方法被定义为final不能被重写; overload (重载):参数类型、个数、顺序至少有一个不相同;2.不能重载只有返回值不同的方法名 3.存在于父类和子类、同类中 */
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { for touch :AnyObject in touches { let t : UITouch = touch as! UITouch; //在屏幕上连续拍击两次,背景变为灰色 if (t.tapCount == 2) { self.view.backgroundColor = UIColor.grayColor(); }else if(t.tapCount == 1){ self.view.backgroundColor = UIColor.brownColor(); } print("begin"); } } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { for touch : AnyObject in touches { let t : UITouch = touch as! UITouch; print(t.locationInView(self.view)); } } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { //两点触摸时,计算两点之间的距离,以及角度 if touches.count == 2 { //获取触摸点 let frist = (touches as NSSet).allObjects[0] as! UITouch; let second = (touches as NSSet).allObjects[1] as! UITouch; //获取触摸点坐标 let fristPoint = frist.locationInView(self.view); let secondPoint = second.locationInView(self.view); //计算两点之间的距离 let distanceX = fristPoint.x - secondPoint.x; let distanceY = fristPoint.y - secondPoint.y; let distance = sqrt(distanceX * distanceX + distanceY * distanceY); print("两点之间的距离:",distance); //计算两点间的角度 let height = secondPoint.y - fristPoint.y; let width = fristPoint.x - secondPoint.x; let rads = atan(height/width); let degrees = 180.0 * rads * 3.1415; print("两点之间的角度:",degrees); } <pre name="code" class="html">override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { print("event cancelled====================="); }
}
相关文章推荐
- TabView的使用去除上面的空白, tabview点击后去除效果
- swift 移动支付之【微信支付】开发步骤
- swift 移动支付之【支付宝支付】详细步骤
- Swift 基本语法
- Swift 字符串切割 java substring
- Ubuntu 16.04上安装 Swift 3.0及问题解答
- swift相比oc的优势
- 半吊子学习Swift--天气预报程序-准备工作
- Swift 3.0 去掉 C 风格循环后怎么办?
- Swift中按钮监听方法私有化之后崩溃解决方式
- Swift开发之 (01) 语法
- 柱状图表实现(swift)
- Swift3的playground中对UI直接测试支持的改变
- Swift3的playground中对UI直接测试支持的改变
- Swift3的playground中对UI直接测试支持的改变
- 自定义TextField(宽高可改变),内部文字左右缩进的实现
- Swift 字符串字符索引String.charAt(index:Int)
- swift实现归档解档
- 32.怎样在Swift中实现TabBar和导航视图结合的项目?
- Swift中FMDB的使用教程和事务的使用