Search code examples
swiftuiviewviewcontrolleruipangesturerecognizer

detect UIView center position in regards to container View - Swift - Programmatically


I have a UIView card added over a ViewController view:

self.view.addSubview(cardView)
cardView.anchor(top: self.topWhiteView.bottomAnchor, leading: view.leadingAnchor, bottom: buttonsStackView.topAnchor, trailing: view.trailingAnchor, padding: .init(top: 15, left: 10, bottom: 10, right: 10))

I added a panGestureRecogniser on it with which I can drag the card. The pan method:

@objc func handlePan(gesture : UIPanGestureRecognizer){
    
    
    switch gesture.state {
    case .changed:
        handleChanged(gesture)
    case .ended:
        handleEnded(gesture)
    default:
        ()
    }
    
    
}


fileprivate func handleChanged(_ gesture: UIPanGestureRecognizer) {
    
    
    let translationY = gesture.translation(in: nil).y
    var scaleValue : CGFloat = 1
    let multiplier = 1.1*translationY
    if translationY != 0 {
        scaleValue = 1 - (abs(translationY*multiplier)/abs(translationY)/3000)
    }


    let scale = CGAffineTransform(scaleX: scaleValue, y: scaleValue)

    let translation = gesture.translation(in: nil)
    let rotationRadiant: CGFloat = translation.x / 20
    let rotationDegree = (rotationRadiant * .pi) / 180
    let rotationalTransformation = CGAffineTransform(rotationAngle: rotationDegree)
    self.transform = rotationalTransformation.translatedBy(x: translation.x, y: translation.y).concatenating(scale)
    
    
   }


fileprivate func handleEnded(_ gesture: UIPanGestureRecognizer) {
    
    UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.1, options: [.curveEaseOut, .allowUserInteraction], animations: {
        
        self.transform = CGAffineTransform.identity
        
    })
}

I want to know how to detect the center position of the cardView whenever the card moves.

But differently from other Stack Overflow questions I would like to do it in cardView class, without using any delegates.


Solution

  • Not sure I understood your needs exactly, but try the following

    class CardView: UIView {
        override var center: CGPoint {
            get { super.center }
            set {
                super.center = newValue
                didMove(to: newValue)
            }
        }
    
        private func didMove(to point: CGPoint) {
            // do anything needed with new position
        }
    }