我试图将包含众多自动生成按钮的ScrollView拖动到主视图上。
以下代码可以满足我的需求(我可能仍然可以清理一下), 除了拖动完成后,我希望它能够快速回到滚动条上的原始位置。
下面的代码将替换相同位置的按钮,但基本上位于整个视图的顶部,而不是滚动条上。
有什么建议可以在返回时将其返回到子视图吗?
感谢您的帮助! 约翰
@IBAction func handleScrollPan(recognizer:UIPanGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.began {
scrollView.superview?.addSubview(recognizer.view!)
let currentPoint = CGPoint(x:recognizer.view!.center.x, y:recognizer.view!.center.y)
let newPoint = scrollView.convert(currentPoint, to: self.view)
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:newPoint.x + translation.x,
y:newPoint.y + translation.y)
}
recognizer.setTranslation(CGPoint(x:0, y:0), in: self.view)
firstLocation = CGPoint(x:scrollView.center.x, y:scrollView.center.y)
} else if (recognizer.state == UIGestureRecognizerState.ended) {
UIView.animate(withDuration: 0,
delay: 0,
options: UIViewAnimationOptions.curveEaseIn,
animations: {recognizer.view!.center = firstLocation},
completion: nil)
} else {
scrollView.superview?.addSubview(recognizer.view!)
let currentPoint = CGPoint(x:recognizer.view!.center.x, y:recognizer.view!.center.y)
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint(x:0, y:0), in: self.view)
}
}