【问题标题】:UIButton Frame changed After Move from one Position To Another从一个位置移动到另一个位置后 UIButton 框架发生了变化
【发布时间】:2018-07-14 09:53:47
【问题描述】:

我在屏幕下方的StoryBoard 中有 1 个UIButton,我按照这个AnswerUIButton 从一个位置移动到另一个位置。

编辑

我想旋转UIButton 的另一件事,为此我尝试了下面的代码,它工作正常,但在旋转UIButton 后,当我尝试将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 Rotation 和 Movement 代码,那么它可以工作,但是当我编写这两个代码时,它会产生这个问题。

【问题讨论】:

  • 你能贴出你的按钮图片吗?我们想看看您的按钮的外观

标签: ios swift uibutton rotation


【解决方案1】:

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

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

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)
}

【讨论】:

  • @Kuldeep 感谢您参考我的答案,直到现在我才能阅读您的问题,但我发现已经回答并解决了您的问题,所以我也对答案投了赞成票,最好的问候
  • 对不起,我对我的项目的这一部分非常陌生,我将代码复制到我的项目中,现在它有这个错误Value of type 'MyViewController' has no member 'superview'你能帮忙吗?
  • 此代码应添加到UIViewUIView 的子类,如UIButtonUILabel。看来您将代码添加到不正确的ViewControllertouchesBegan()touchesMoved() 将在 UIView 上调用。
猜你喜欢
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
相关资源
最近更新 更多