Swift 通過touchesBegan 方法擷取使用者點擊的view,類比連續點擊效果

來源:互聯網
上載者:User

標籤:eve   ack   ima   img   ide   over   view   space   convert   

    var mV:UIView!

    override func viewDidLoad() {

        super.viewDidLoad()

        mV = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100 ))

        mV.backgroundColor = UIColor.red

        self.view.addSubview(mV)

    }

 

 

 

   var flag = false

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch:AnyObject in touches{

//            方法一

//            //擷取使用者點擊的座標

//            var point = (touch as AnyObject).location(in: self.view)

//            //將使用者點擊的點座標,從self.view.layer轉換到mV.View.layer的點座標

//            point = self.mV.layer.convert(point, from: self.view.layer)

//            print("point\(point)")

//

//            let point2 = self.mV.layer.convert(point, to: self.view.layer)

//            print("point2\(point2)")

//

//            if self.mV.layer.contains(point ){

//                print(1)

//            }

            

 

            //方法二

            //擷取使用者點擊的座標

            var point = (touch as AnyObject).location(in: self.view)

            //返回在圖層層次中包含point的view.layer的最遠子代,即擷取到使用者點擊的View的layer

            let layer = view.layer.hitTest(point)

            

            if layer == mV.layer{

                //類比聯機點擊效果

                let t:UITouch = touch  as! UITouch

                let i = t.tapCount

                if t.tapCount >= 2{

                    flag = true

                }

                if flag{

                    print(i)

                }

            }

            

        }

       

        flag = false

    }

 

Swift 通過touchesBegan 方法擷取使用者點擊的view,類比連續點擊效果

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.