如何使用CGAffineTransform正确拖放

问题描述 投票:1回答:2

我想这样做的阻力,并通过与UIPanGestureRecognizer自动调心跌幅为的UITextView在屏幕的中央。对于第一拖动它的作品不错,但如果我尝试做了一遍我的拖拽开始用的UITextView的初始化点。我不知道如何解决它。

首先将

enter image description here

第二拖曳

enter image description here

@objc func handlePan(gesture: UIPanGestureRecognizer) {
    let translation = gesture.translation(in: nil)

    switch gesture.state {
    case .changed:
        dragingText.transform = CGAffineTransform(translationX: translation.x, y: translation.y)

    case .ended:

        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            self.dragingText.transform = CGAffineTransform(translationX: 0, y: translation.y)

        }, completion: nil)
    default:
        ()
    }
}
swift animation uipangesturerecognizer
2个回答
0
投票

CGAffine变换不需要你的任务。尝试:

情况下.changed:textview.center = CGPoint(X:textview.x + translation.x,Y:textview.y + translation.y)


0
投票

解决的问题

 @objc func handlePan(gesture: UIPanGestureRecognizer) {
    let translation = gesture.translation(in: nil)

    switch gesture.state {
    case .changed:

        dragingText.center = CGPoint(x: dragingText.center.x + translation.x, y: dragingText.center.y + translation.y )
        gesture.setTranslation(CGPoint.zero, in: nil)

    case .ended:

        let isInside = isInsideDragableAres()

        if isInside.0 {
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.7, options: .curveEaseOut, animations: {
                self.dragingText.center = CGPoint(x: self.view.center.x, y: self.dragingText.center.y + translation.y )
            }, completion: nil)
        }
        else{
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.7, options: .curveEaseOut, animations: {
                self.dragingText.center = CGPoint(x: self.view.center.x, y: isInside.1! )
            }, completion: nil)
        }

    default:
        ()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.