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

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

我在UIButton有1个StoryBoard,就像屏幕下方一样,我按照这个UIButtonAnswer从一个位置移动到另一个位置。

编辑

enter image description here

另一件事,我想旋转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

我上传了问题视频。 enter image description here

编辑2

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

ios swift uibutton rotation
1个回答
3
投票

我认为问题在于旋转是围绕视图的中心,因此当您应用旋转时,帧大小会增加,从而抛出相对于帧原点的距离。

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

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.