您的位置:首页 > 移动开发 > Swift

Swift UITouch 的使用

2016-07-20 12:48 211 查看
touch 允许多点触碰

在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=====================");
}


}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: