Swift UITouch 的使用

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

}

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值