Search code examples
swiftuipangesturerecognizeruilongpressgesturerecogni

UIPanGestureRecognizer for programmatically added UIView


I want to create circle after long pressure gesture and make it move with pan gesture while still pressing finger on screen. When finger is moved from screen, circle should be removed from it. Circle creation with long tap gesture works fine, just it doesn't move. Code is below:

@IBAction func spaceLongPressed(_ guesture: UILongPressGestureRecognizer) {
    let location = guesture.location(in: space)
    var panGesture = UIPanGestureRecognizer()

    circleView = UIView(frame: CGRect(x: location.x - space.bounds.height/4, y: location.y - space.bounds.height/4, width: space.bounds.height/2, height: space.bounds.height/2))

    if guesture.state == .began {

        circleView.backgroundColor = UIColor.red
        circleView.isUserInteractionEnabled = true
        circleView.tag = 100
        circleView.layer.cornerRadius = circleView.bounds.height/2
        space.addSubview(circleView)

        panGesture = UIPanGestureRecognizer(target: space, action: #selector(didPan(sender:)))
        space.addGestureRecognizer(panGesture)
    }

    if guesture.state == .ended {
        if let viewWithTag = self.view.viewWithTag(100) {
            viewWithTag.removeFromSuperview()
        }
    }
}

@objc func didPan(sender: UIPanGestureRecognizer) {

    if sender.state == .began {
    let translation = sender.translation(in: sender.view)

    let changeX = (sender.view?.center.x)! + translation.x
    let changeY = (sender.view?.center.y)! + translation.y

    sender.view?.center = CGPoint(x: changeX, y: changeY)
    sender.setTranslation(CGPoint.zero, in: sender.view)
    }
}

Object "space" is of UIButton type - I want to create circle in button.


Solution

  • No need to create panGesture for updating position. You can get same effect using long press gesture. (I change little bit of the above code, especially view frame while creating).

    var circleView: UIView!
    
    @IBAction func spaceLongPressed(_ guesture: UILongPressGestureRecognizer) {
        let location = guesture.location(in: view)
        let size = CGSize(width: 100, height: 100)
        circleView = UIView(frame: CGRect(origin: location, size: size))
    
        if guesture.state == .began {
            circleView.backgroundColor = UIColor.red
            circleView.isUserInteractionEnabled = true
            circleView.tag = 100
            circleView.layer.cornerRadius = circleView.bounds.height/2
            view.addSubview(circleView)
        }
    
        if guesture.state == .changed {
            let position = guesture.location(in: view)
            if let viewWithTag = self.view.viewWithTag(100) {
                viewWithTag.center = position
            }
        }
    
        if guesture.state == .ended {
            if let viewWithTag = self.view.viewWithTag(100) {
                viewWithTag.removeFromSuperview()
            }
        }
    }
    

    Output:

    abc