How can I create a UIView at a certain CGPoint?

前端 未结 3 498
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-17 01:44

I need to be able to create and display a UIView at a a certain CGPoint.

So far I have a gesture recogniser that is added as a subview to the main view.

I th

3条回答
  •  灰色年华
    2021-01-17 02:09

    Let's create a Tap Gesture and assign it to a View

    let tapGesture = UITapGestureRecognizer()
    tapGesture.addTarget(self, action: "tappedView:") // action is the call to the function that will be executed every time a Tap gesture gets recognised.
    let myView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    myView.addGestureRecognizer(tapGesture)
    

    Every time you tap a view with the assigned Tap Gesture, this function gets called.

    func tappedView(sender: UITapGestureRecognizer) {
    // Now you ca access all the UITapGestureRecognizer API and play with it however you want.
    
        // You want to center your view to the location of the Tap.
        myView.center = sender.view!.center
    
    }
    

提交回复
热议问题