iosswiftuipangesturerecognizer

[Swift-iOS]Dragging down view using UIPanGestureRecognizer, not stretching


I'm trying to drag down a specific view to make the height go longer than before using UIPanGestureRecognizer. However, it keeps on going back to the normal size that i had initially programmed. The view that i'm trying to stretch keeps on shaking and goes back to the initial height. Any guesses why?

    let CalendarDrag = UIPanGestureRecognizer(target: self, action: #selector(didDragCalendar))
    lineView.isUserInteractionEnabled = true
    lineView.addGestureRecognizer(CalendarDrag)

}


@objc func didDragCalendar(sender: UIPanGestureRecognizer) {
    let velocity = sender.velocity(in: self.view) //속도
    let translation = sender.translation(in: self.view) //위치
    let height = self.topView.frame.maxY
    
    if sender.state == .ended{
        if velocity.y>0{
            calendar.scope = .month
            print("down")
        }else{
            calendar.scope = .week
            print("up")
        }
    }else{
        if height <= height+translation.y && height+translation.y <= height+230{
            self.topView.frame = CGRect(x: 0, y: 0, width: self.topView.frame.width, height: height+translation.y)
            UIView.animate(withDuration: 0, animations: {
                self.view.layoutIfNeeded()
                sender.setTranslation(CGPoint.zero, in: self.view)
            })
        }
    }
}

For more information, I'm right now trying to make FSCalendar to show in "month" and "week" view according to the UIPanGesture that user make.


Solution

  • You are changing the frame height of your view. Don't do that.

    Have an instance var to a height constraint, and change the constant value for that constraint. Then when you call layoutIfNeeded() from your animation, the constraint will change your height for you.