使用 UIPanGestureRecognizer - swift - 以编程方式获取视图翻译
Getting Translation of view with UIPanGestureRecognizer - swift - programmatically
我正在使用 UIPanGestureRecognizer 方法翻译视图:
@objc fileprivate func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
makeDisappearButtons()
deletebox.isHidden = false
let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
if gestureRecognizer.view!.frame.intersects(deletebox.frame) {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
gestureRecognizer.view!.transform = .init(scaleX: 0.6, y: 0.6)
self.deletebox.transform = .init(scaleX: 1.2, y: 1.2)
}, completion: nil)
} else {
gestureRecognizer.view!.setNeedsDisplay()
gestureRecognizer.setTranslation(.zero, in: self.view)
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.deletebox.transform = .identity
gestureRecognizer.view!.transform = .identity
}, completion: nil)
}
}
if gestureRecognizer.state == .ended {
makeButtonsReAppear()
if gestureRecognizer.view!.frame.intersects(deletebox.frame) {
gestureRecognizer.view!.isHidden = true
gestureRecognizer.view?.removeFromSuperview()
} else {
gestureRecognizer.view!.setNeedsDisplay()
gestureRecognizer.setTranslation(.zero, in: self.view)
gestureRecognizer.view!.transform = .identity
}
print(gestureRecognizer.translation(in: self.view))
deletebox.isHidden = true
}
}
我想知道 gestureRecognizer.state == .ended 中视图的翻译,但是当我尝试将其打印出来时,它是 x:0、y:0.
这个方法是从哪里调用的,可以放在viewDidAppear中试试。否则调用 setNeedsDisplay 和 LayoutIfNeeded 方法。
每次手势处理程序调用 changed
时,您都将翻译设置回 .zero
。
gestureRecognizer.setTranslation(.zero, in: self.view)
但是,当手指抬起时,状态变为 ended
,此时翻译不再改变。
documentation 表示:
When the user’s fingers lift from the screen, the gesture recognizer enters the UIGestureRecognizer.State.ended state.
因此翻译与上次 changed
调用 和 ended
中的 相同 -- 以及因为您已经在 changed
内设置了 gestureRecognizer.view
的框架,所以您不需要在 ended
.
内做任何事情
我正在使用 UIPanGestureRecognizer 方法翻译视图:
@objc fileprivate func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
makeDisappearButtons()
deletebox.isHidden = false
let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
if gestureRecognizer.view!.frame.intersects(deletebox.frame) {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
gestureRecognizer.view!.transform = .init(scaleX: 0.6, y: 0.6)
self.deletebox.transform = .init(scaleX: 1.2, y: 1.2)
}, completion: nil)
} else {
gestureRecognizer.view!.setNeedsDisplay()
gestureRecognizer.setTranslation(.zero, in: self.view)
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.deletebox.transform = .identity
gestureRecognizer.view!.transform = .identity
}, completion: nil)
}
}
if gestureRecognizer.state == .ended {
makeButtonsReAppear()
if gestureRecognizer.view!.frame.intersects(deletebox.frame) {
gestureRecognizer.view!.isHidden = true
gestureRecognizer.view?.removeFromSuperview()
} else {
gestureRecognizer.view!.setNeedsDisplay()
gestureRecognizer.setTranslation(.zero, in: self.view)
gestureRecognizer.view!.transform = .identity
}
print(gestureRecognizer.translation(in: self.view))
deletebox.isHidden = true
}
}
我想知道 gestureRecognizer.state == .ended 中视图的翻译,但是当我尝试将其打印出来时,它是 x:0、y:0.
这个方法是从哪里调用的,可以放在viewDidAppear中试试。否则调用 setNeedsDisplay 和 LayoutIfNeeded 方法。
每次手势处理程序调用 changed
时,您都将翻译设置回 .zero
。
gestureRecognizer.setTranslation(.zero, in: self.view)
但是,当手指抬起时,状态变为 ended
,此时翻译不再改变。
documentation 表示:
When the user’s fingers lift from the screen, the gesture recognizer enters the UIGestureRecognizer.State.ended state.
因此翻译与上次 changed
调用 和 ended
中的 相同 -- 以及因为您已经在 changed
内设置了 gestureRecognizer.view
的框架,所以您不需要在 ended
.