问题
I have a pan gesture on a button that moves the button in the direction of the pan. When the translation is "far enough" the button gets rotated. On this rotation the button visible goes to its initial position or close and then back where it should be. Cannot figure out what is going on. This is my code:
@IBAction func didPanButton(_ sender: UIPanGestureRecognizer) {
print("Did pan button", sender.translation(in: sender.view).x)
answerButton.center.x = view.center.x + sender.translation(in: sender.view).x
switch buttonPosition {
case .left:
print("Rotate button left")
UIView.animate(withDuration: 0.5, animations: {
self.rotateButtonLeft()
})
case .right:
print("Rotate button right")
UIView.animate(withDuration: 0.5, animations: {
self.rotateButtonRight()
})
case .middle:
print("Remove button rotation")
UIView.animate(withDuration: 0.5, animations: {
self.removeButtonRotation()
})
}
}
var buttonPosition : ButtonPosition {
let leftWidth = (view.frame.size.width - answerButton.frame.size.width) / 2
if answerButton.frame.origin.x < leftWidth / 2 {
return .left
}
if answerButton.frame.origin.x > leftWidth * 3 / 2 {
return .right
}
return .middle
}
func rotateButtonLeft() {
let degrees : CGFloat = -10; //the value in degrees
answerButton.transform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi/180);
let image = UIImage(named: "item_no")
answerButton.setImage(image, for: UIControlState.normal)
}
func rotateButtonRight() {
let degrees : CGFloat = 10; //the value in degrees
answerButton.transform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi/180);
let image = UIImage(named: "item_yes")
answerButton.setImage(image, for: UIControlState.normal)
}
func removeButtonRotation() {
let degrees : CGFloat = 0; //the value in degrees
answerButton.transform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi/180);
let image = UIImage(named: "item_neutral")
answerButton.setImage(image, for: UIControlState.normal)
}
回答1:
Your button has constraints to set its position. When auto layout runs, it resets the button's center
according to those constraints. Setting the button's image is triggering auto layout.
You have three options to fix this:
- Change the button's position by updating the constraints, OR
- Change the button's position by setting its
transform
instead of by changing itscenter
, OR - Don't use constraints to position the button.
来源:https://stackoverflow.com/questions/49280086/view-moves-weirdly-on-rotation-through-pan-gesture