从一个位置移动到另一个位置后,UIButton 框架发生了变化

UIButton Frame changed After Move from one Position To Another

我在 StoryBoard 中有 1 个 UIButton,如下面的屏幕所示,我按照 .

从一个位置移动到另一个位置 UIButton

编辑

我想旋转的另一件事 UIButton 为此我尝试了下面的代码并且它工作正常但是在旋转 UIButton 之后当我尝试从 1 移动 UIButton 位置时放置到另一个然后 UIButton 框架被改变。

整个 UIButton 代码。

class DraggableButton: UIButton {

var localTouchPosition : CGPoint?
var lastRotation: CGFloat = 0

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // ROTATION CODE
    let rotate = UIRotationGestureRecognizer(target: self, action:#selector(rotatedView(_:)))
    self.addGestureRecognizer(rotate)
}

// SCROLLING CONTENT FROM ONE POSITION TO ANOTHER
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    let touch = touches.first
    self.localTouchPosition = touch?.preciseLocation(in: self)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    let touch = touches.first
    guard let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{
        return
    }

    self.frame.origin = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)
    print(self.frame)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    self.localTouchPosition = nil
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesCancelled(touches, with: event)
    self.localTouchPosition = nil
}

// ROTATION CODE
@objc func rotatedView(_ sender: UIRotationGestureRecognizer) {
    var originalRotation = CGFloat()
    if sender.state == .began {
        sender.rotation = lastRotation
        originalRotation = sender.rotation
    } else if sender.state == .changed {
        let newRotation = sender.rotation + originalRotation
        sender.view?.transform = CGAffineTransform(rotationAngle: newRotation)
        } else if sender.state == .ended {
            lastRotation = sender.rotation
        }
    }
}

编辑 1

我上传了问题视频。

编辑 2

如果我分别使用 UIButton 旋转和移动代码那么它可以工作但是当我写这两个代码时它会产生这个问题。

我认为问题在于旋转是围绕视图的中心进行的,因此当您应用旋转时,框架尺寸会增加,从而偏离相对于框架原点的距离。

我能够通过相对于其中心移动视图来解决此问题:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    let touch = touches.first
    guard let location = touch?.location(in: self.superview) else { return }

    // Store localTouchPosition relative to center
    self.localTouchPosition = CGPoint(x: location.x - self.center.x, y: location.y - self.center.y)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    let touch = touches.first
    guard let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{
        return
    }

    self.center = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)
    print(self.frame)
}