代码之家  ›  专栏  ›  技术社区  ›  Kuldeep Jayesh Lathiya

从一个位置移动到另一个位置后更改了uiButton框架

  •  2
  • Kuldeep Jayesh Lathiya  · 技术社区  · 8 年前

    我有一个 UIButton 在里面 StoryBoard 就像在屏幕下面我移动 U按钮 从一个位置到另一个位置 Answer .

    编辑

    enter image description here

    我想旋转的另一件事 U按钮 为此,我尝试了下面的代码,它运行良好,但经过旋转 U按钮 当我试图移动 U按钮 从一个地方到另一个地方 U按钮 框架已更改。

    全部 U按钮 代码。

    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

    如果我用 U按钮 旋转和移动代码分开,然后它工作,但当我写这两个代码,它产生这个问题。

    1 回复  |  直到 8 年前
        1
  •  3
  •   vacawama    8 年前

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

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

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