UIButton框架在从一个位置移动到另一个位置后发生了变化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIButton框架在从一个位置移动到另一个位置后发生了变化相关的知识,希望对你有一定的参考价值。
我在UIButton
有1个StoryBoard
,就像屏幕下方一样,我按照这个UIButton
将Answer从一个位置移动到另一个位置。
编辑
另一件事,我想旋转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)
}
以上是关于UIButton框架在从一个位置移动到另一个位置后发生了变化的主要内容,如果未能解决你的问题,请参考以下文章
是否可以使用淡入淡出将 ui 元素从一个位置移动到另一个位置?
我的 UIButton 从随机位置开始,而不是从中心开始并移动