问题描述
我在 StoryBoard
中有 1 个 UIButton
如下图所示,我按照这个
我想旋转 UIButton
的另一件事,为此我尝试了下面的代码,它工作正常,但在旋转 UIButton
后,当我尝试移动 UIButton
位置从 1 个位置到另一个位置,然后 UIButton
框架被更改.
整个UIButton
代码.
class DraggableButton: UIButton {var localTouchPosition : CGPoint?var lastRotation: CGFloat = 0需要初始化?(编码器aDecoder:NSCoder){super.init(编码器:aDecoder)//旋转代码让旋转 = UIRotationGestureRecognizer(target: self, action:#selector(rotatedView(_:)))self.addGestureRecognizer(旋转)}//将内容从一个位置滚动到另一个位置覆盖 func touchesBegan(_ touches: Set, with event: UIEvent?) {super.touchesBegan(touches, with: event)让触摸 = touches.firstself.localTouchPosition = touch?.preciseLocation(in: self)}覆盖 func touchesMoved(_ touches: Set, with event: UIEvent?) {super.touchesMoved(touches, with: event)让触摸 = touches.first守卫 let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{返回}self.frame.origin = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)打印(self.frame)}覆盖 func touchesEnded(_ touches: Set, with event: UIEvent?) {super.touchesEnded(touches, with: event)self.localTouchPosition = nil}覆盖 func touchesCancelled(_ touches: Set, with event: UIEvent?) {super.touchesCancelled(touches, with: event)self.localTouchPosition = nil}//旋转代码@objc func 旋转视图(_ 发送者:UIRotationGestureRecognizer){var originalRotation = CGFloat()如果 sender.state == .began {sender.rotation = lastRotationoriginalRotation = sender.rotation} else if sender.state == .changed {让 newRotation = sender.rotation + originalRotationsender.view?.transform = CGAffineTransform(rotationAngle: newRotation)} else if sender.state == .ended {lastRotation = sender.rotation}}}
编辑 1
我上传了问题视频.
编辑 2
如果我分别使用 UIButton
旋转和移动代码,那么它可以工作,但是当我编写这两个代码时,它会产生这个问题.
我认为问题在于旋转是围绕视图的中心进行的,因此当您应用旋转时,框架大小会增加,从而导致相对于视图的距离增加框架的起源.
我能够通过相对于其中心移动视图来解决这个问题:
override func touchesBegan(_ touches: Set, with event: UIEvent?) {super.touchesBegan(touches, with: event)让触摸 = touches.first守卫让位置=触摸?.位置(在:self.superview)其他{返回}//存储相对于中心的 localTouchPositionself.localTouchPosition = CGPoint(x: location.x - self.center.x, y: location.y - self.center.y)}覆盖 func touchesMoved(_ touches: Set, with event: UIEvent?) {super.touchesMoved(touches, with: event)让触摸 = touches.first守卫 let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{返回}self.center = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)打印(self.frame)}
I have 1 UIButton
in StoryBoard
like below screen and I move UIButton
from one position to another position by following this Answer.
Edit
Another thing that I want to rotate UIButton
and for that I have tried below code and it works fine but after rotating UIButton
when I try to move UIButton
position from 1 place to another then UIButton
frame is changed.
Entire UIButton
code.
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
}
}
}
Edit 1
I uploaded issue video.
Edit 2
If I used UIButton
Rotation and Movement Code separately then it works but when I write both code it generate this issue.
I believe the issue is that the rotation is about the center of the view, so when you apply the rotation the frame size increases which throws off the distance relative to the origin of the frame.
I was able to fix this by moving the view relative to its center:
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)
}
这篇关于UIButton Frame从一个位置移动到另一个位置后发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!