Ios UIview的起始中心错误。(假视觉)
视频说明: 我有一个UIView,您可以将其拖动到另一个视图的中心以执行操作。该视图应具有来自中心视图+24的前导约束 在较大的iphone(iPhone8)上打印的起始中心X值约为324 在iphone 5s和其他一些较小的手机上,视图似乎放在了正确的位置,但一旦我移动视图,它就会弹回到错误的位置。.ended PanGesture将使其捕捉到其起始中心X值 我在324处打印它的起始值,但一旦我移动它,它就会捕捉到屏幕右侧的324 这是什么原因造成的?为什么一开始它似乎位于正确的位置 Pangesture检查按钮如下:Ios UIview的起始中心错误。(假视觉),ios,swift,uiview,autolayout,uipangesturerecognizer,Ios,Swift,Uiview,Autolayout,Uipangesturerecognizer,视频说明: 我有一个UIView,您可以将其拖动到另一个视图的中心以执行操作。该视图应具有来自中心视图+24的前导约束 在较大的iphone(iPhone8)上打印的起始中心X值约为324 在iphone 5s和其他一些较小的手机上,视图似乎放在了正确的位置,但一旦我移动视图,它就会弹回到错误的位置。.ended PanGesture将使其捕捉到其起始中心X值 我在324处打印它的起始值,但一旦我移动它,它就会捕捉到屏幕右侧的324 这是什么原因造成的?为什么一开始它似乎位于正确的位置 Pan
@objc func checkButtonWasDragged(_ sender: UIPanGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began || sender.state == UIGestureRecognizerState.changed {
self.view.bringSubview(toFront: checkButton)
let translation = sender.translation(in: self.view)
checkButton.center = CGPoint(x: checkButton.center.x + translation.x, y: checkButton.center.y)
print(checkButton.center.x)
distanceCounter += translation.x
if distanceCounter > 0 {
distanceCounter = 0
}
if checkButton.center.x > startingPointCheckBtn {
checkButton.center.x = startingPointCheckBtn
} else if checkButton.center.x < largeCircle.center.x && distanceCounter < checkBtnDistance{
checkButton.center.x = largeCircle.center.x
distanceCounter = checkBtnDistance
} else {
sender.setTranslation(CGPoint.zero, in: self.view)
}
// hide the buttons after we drag over them
if distanceCounter < -60.0 && !rightCirclesHidden {
for circle in rightCircles {
circle.layer.removeAllAnimations()
circle.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
}
rightCirclesHidden = true
} else if distanceCounter > -60.0 && rightCirclesHidden {
for i in 0..<leftCircles.count {
leftCircles[i].layer.removeAllAnimations()
//rightCircles[i].backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
rightCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
leftCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
startAnimatingCircles()
rightCirclesHidden = false
}
} else if sender.state == UIGestureRecognizerState.ended {
if largeCircle.center.x - checkButton.center.x >= -35.0 {
checkButton.center = largeCircle.center
self.loadSignUpPage()
} else {
checkButton.center.x = startingPointCheckBtn
self.view.layoutIfNeeded()
print(startingPointCheckBtn)
if rightCirclesHidden {
for i in 0..<leftCircles.count {
leftCircles[i].layer.removeAllAnimations()
//rightCircles[i].backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
rightCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
leftCircles[i].backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
startAnimatingCircles()
rightCirclesHidden = false
}
}
distanceCounter = 0
}
}
setUpCheckButton代码:
func setUpCheckButton() {
startingPointCheckBtn = checkButton.center.x
print("check circle center: " + "\(startingPointCheckBtn)")
checkBtnDistance = largeCircle.center.x - startingPointCheckBtn
let checkGestureDrag = UIPanGestureRecognizer(target: self, action: #selector(AdViewPageVC.checkButtonWasDragged(_:)))
checkButton.addGestureRecognizer(checkGestureDrag)
}
您不应该在
viewDidLoad
中设置它,而应该在viewDidAppear
中设置它,以便在屏幕上显示复选标记时获得实际值。同时将您的手势结束块更改为此
UIView.animate(withDuration: 0.70, delay: 0, options: .curveEaseOut, animations: {
self.checkButton.center.x = self.startingPointCheckBtn
self.view.layoutIfNeeded()
}, completion: nil)
请显示视图的panGesture和te约束的代码。哇,这是很多检查。但是看看你需要什么。如果您将
checkButton
的起始位置存储在startingPointCheckButton
中,它应该可以正常工作。检查起始位置是否确实存储在该变量中。另外,我建议您使用自动布局约束进行动画。还可以使用UIView.animate
轻松地在复选标记中返回原始位置。请在viewdide中显示代码。我想你可能用了框架而不是边界什么的。你能加入聊天吗?
UIView.animate(withDuration: 0.70, delay: 0, options: .curveEaseOut, animations: {
self.checkButton.center.x = self.startingPointCheckBtn
self.view.layoutIfNeeded()
}, completion: nil)