[英]What is a good way to reset a UIPanGestureRecognizer's view to its original frame once the pan gesture is done?
Say you have a pinch gesture recognizer and pan gesture recognizer on my image view:假设您在我的图像视图上有一个捏合手势识别器和平移手势识别器:
@IBAction func pinchPiece(_ pinchGestureRecognizer: UIPinchGestureRecognizer) {
guard pinchGestureRecognizer.state == .began || pinchGestureRecognizer.state == .changed,
let piece = pinchGestureRecognizer.view else {
// After pinch releases, zoom back out.
if pinchGestureRecognizer.state == .ended {
UIView.animate(withDuration: 0.3, animations: {
pinchGestureRecognizer.view?.transform = CGAffineTransform.identity
})
}
return
}
adjustAnchor(for: pinchGestureRecognizer)
let scale = pinchGestureRecognizer.scale
piece.transform = piece.transform.scaledBy(x: scale, y: scale)
pinchGestureRecognizer.scale = 1 // Clear scale so that it is the right delta next time.
}
@IBAction func panPiece(_ panGestureRecognizer: UIPanGestureRecognizer) {
guard panGestureRecognizer.state == .began || panGestureRecognizer.state == .changed,
let piece = panGestureRecognizer.view else {
return
}
let translation = panGestureRecognizer.translation(in: piece.superview)
piece.center = CGPoint(x: piece.center.x + translation.x, y: piece.center.y + translation.y)
panGestureRecognizer.setTranslation(.zero, in: piece.superview)
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
true
}
The pinch gesture's view resets to its original state after the gesture is done, which occurs in its else clause.手势完成后,捏合手势的视图将重置为其原始 state,这发生在其 else 子句中。 What would be a good way to do the same for the pan gesture recognizer?对平移手势识别器执行相同操作的好方法是什么? Ideally I'd like the gesture recognizers to be in an extension of UIImageView
, which would also mean that I can't add a store property to the extension for tracking the initial state of the image view.理想情况下,我希望手势识别器位于UIImageView
的扩展中,这也意味着我无法将存储属性添加到扩展中以跟踪图像视图的初始 state。
Since UIImageView is Hashable , you can store a STATIC dictionary, using the image view itself as a key and some struct as the value, with whatever data you need to "remember" in order to restore the view to its original state.由于 UIImageView 是Hashable ,您可以存储一个 STATIC 字典,使用图像视图本身作为键和一些结构作为值,您需要“记住”任何数据以将视图恢复到其原始 Z9ED39E2EA931586B5732.985A69E Note that static properties can be stored from extensions (as opposed to stored properties).请注意, static属性可以从扩展存储(与存储属性相反)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.