简体   繁体   English

完成平移手势后,将 UIPanGestureRecognizer 的视图重置为其原始帧的好方法是什么?

[英]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.

相关问题 使用摇动手势重置UIView的框架而无需对开始值进行硬编码 - Reset UIView's frame with a pan gesture without hardcoding start values 手势识别器重置 - Pan Gesture Recognizer reset 平移手势后迅速将视图返回到原始位置 - Returning view to original position after pan gesture, swift UITableViewCell上的UIPanGestureRecognizer会覆盖UITableView的滚动视图手势识别器 - UIPanGestureRecognizer on UITableViewCell overrides UITableView's scroll view gesture recognizer 捏合时,即使平移到其他位置,图像视图也会移回到原始帧的原点 - on pinch, image view shifts back to original frame's origin even after pan to different location 为什么我的平移手势只会轻推它的观点并停止? - Why does my pan gesture only nudge its view and stop? 什么是原型视图 controller 的好方法? - What's a good way to prototype a view controller? 平移手势识别器:松开平移手势后,触摸位置会崩溃 - Pan Gesture Recognizer: Location of touch crashes once the pan gesture is released UIPanGestureRecognizer 的translation(in:) func 的观点参数是什么? - What's the point of view parameter of translation(in:) func of UIPanGestureRecognizer? 限制平移手势移动到屏幕框架左右两侧之外 - Restrict pan gesture from moving outside of the left and right sides of the screen’s frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM