IOS swift多点触控

来源:互联网 发布:linux www服务器搭建 编辑:程序博客网 时间:2024/05/21 02:49
 override func viewDidLoad() {        super.viewDidLoad()        self.view.multipleTouchEnabled=true//是否开启多点触摸        // Do any additional setup after loading the view, typically from a nib.    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {        //获得焦点的坐标//        var l:CGPoint=((touches as NSSet).anyObject())!.locationInView(self.view) as CGPoint//        println("x:\(l.x),y:\(l.y)")//        println("touchesBegan按下")    }    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {       // println("touchesEnded抬起")    }    private var lastDistance:CGFloat = 0.0    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {//        println(touches.count)//        //获得每个焦点//        for touche in touches{//            //获得焦点的坐标//            var l:CGPoint = (touche as AnyObject).locationInView(self.view) as CGPoint//            println("x:\(l.x),y:\(l.y)")//        }//        //        println("touchesMoved移动")                if touches.count == 2 {            var p1:CGPoint=(touches as NSSet).allObjects[0].locationInView(self.view) as CGPoint            var p2:CGPoint=(touches as NSSet).allObjects[1].locationInView(self.view) as CGPoint            var xx = p1.x - p2.x            var yy = p1.y - p2.y            var currenDistance:CGFloat=sqrt(xx*xx+yy*yy)            if lastDistance == 0.0{                lastDistance = currenDistance            }else{                if lastDistance-currenDistance > 3{                    println("缩小")                    image.transform=CGAffineTransformScale(image.transform, 0.9, 0.9)                }else if lastDistance-currenDistance <  -3{                    println("放大")                    image.transform=CGAffineTransformScale(image.transform, 1.1, 1.1)                }                lastDistance = currenDistance            }        }            }    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {       // println("touchesCancelled")    }

0 0
原创粉丝点击