使用 UIPanGestureRecognizer- Swift 3 旋转 ImageView

Posted

技术标签:

【中文标题】使用 UIPanGestureRecognizer- Swift 3 旋转 ImageView【英文标题】:Rotating ImageView using UIPanGestureRecognizer- Swift 3 【发布时间】:2017-03-21 18:53:51 【问题描述】:

我正在尝试根据 X 坐标旋转我拥有的 ImageView。基本上,我希望它在 x = 300 时旋转 0º,在 x = 190 时旋转 180º。

我必须以编程方式对 UIPanGestureRecognizer 进行编程。这是我目前拥有的代码:

    @objc func personDrag(recognizer: UIPanGestureRecognizer) 

    let rotationSub: CGFloat = 1

    let translation = recognizer.translation(in: rView)
    if let view = recognizer.view 
        view.center = CGPoint(x:view.center.x +  translation.x, y:view.center.y + translation.y)
        view.transform = view.transform.rotated(by: CGFloat.pi - rotationSub)
    
    recognizer.setTranslation(CGPoint.zero, in: rView)


每次他们平移时,我都会尝试将旋转度数更改 1,但这并没有真正起作用/没有意义。任何帮助,将不胜感激。非常感谢!

干杯,西奥

【问题讨论】:

你能解释一下'它不起作用'是什么意思吗? @dmorrow 当我在屏幕上移动图像视图而不是根据 x 坐标更改旋转度数时,它只是旋转得非常快。这澄清了吗?谢谢! 【参考方案1】:

您可以在此基础上构建您的实现:

import UIKit

class ViewController: UIViewController 

    @IBOutlet weak var imageview: UIImageView!

    private var currentRotation: Rotation = .none

    /* Certain rotation points (rotation of 0º when x = 300 and a rotation of 180º when x = 190) */
    enum Rotation 
        case none, xPoint190, xPoint300
    

    override func viewDidLoad() 
        super.viewDidLoad()

        let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
        imageview.addGestureRecognizer(gestureRecognizer)
        imageview.isUserInteractionEnabled = true
    

    @IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) 
        guard gestureRecognizer.state == .began || gestureRecognizer.state == .changed else 
            return
        

        guard let imgView = gestureRecognizer.view else 
            return
        

        let translation = gestureRecognizer.translation(in: self.view)
        imgView.center = CGPoint(x: imgView.center.x + translation.x, y: imgView.center.y + translation.y)
        gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)

        let angle: CGFloat = self.degreesToRadians(180.0)

        /* After reaching x point case - rotating and setting rotation occured to prohibit further rotation */

        if imgView.layer.frame.origin.x <= 190, currentRotation != .xPoint190 

        imgView.transform = imgView.transform.rotated(by: angle)
        currentRotation = .xPoint190

     else if imgView.layer.frame.origin.x >= 300, currentRotation != .xPoint300 

        imgView.transform = imgView.transform.rotated(by: angle)
        currentRotation = .xPoint300
    


    private func degreesToRadians(_ deg: CGFloat) -> CGFloat 
        return deg * CGFloat.pi / 180
    

【讨论】:

我觉得你很困惑。我希望图像在屏幕上拖动时旋转。 @TheoStrauss 编辑了我的答案。实现了拖动,当图像视图到达某些 x 点时,它会旋转。希望这是你想要的;] 天哪,这太棒了。我需要一些时间来消化和实施,我会和你核对一下。我现在给你打勾! 如果您有任何问题,请随时询问;) 嗯。它给了我它无法识别手势识别器的错误。那bc有守卫吗?你能帮我解决这个问题吗?【参考方案2】:

希望对你有所帮助。

 @objc func rotateViewPanGesture(_ recognizer: UIPanGestureRecognizer) 
            touchLocation = recognizer.location(in: superview)
            let center = CGRectGetCenter(frame)

            switch recognizer.state 
            case .began:
                deltaAngle = atan2(touchLocation!.y - center.y, touchLocation!.x - center.x) - CGAffineTrasformGetAngle(transform)
                initialBounds = bounds
                initialDistance = CGpointGetDistance(center, point2: touchLocation!)

            case .changed:

                let ang = atan2(touchLocation!.y - center.y, touchLocation!.x - center.x)
                let angleDiff = deltaAngle! - ang

                let a = transform.a
                let b = transform.b
                let c = transform.c
                let d = transform.d

                let sx = sqrt(a * a + b * b)
                let sy = sqrt(c * c + d * d)

                let currentScale = CGPoint(x: sx, y: sy)
                let scale = CGAffineTransform(scaleX: currentScale.x, y: currentScale.y)
                self.transform = scale.rotated(by: -angleDiff)

                layoutIfNeeded()
            case .ended:

              print("end gesture status")
            default:break

            
        

【讨论】:

以上是关于使用 UIPanGestureRecognizer- Swift 3 旋转 ImageView的主要内容,如果未能解决你的问题,请参考以下文章

为啥使用 UIPanGestureRecognizer 移动对象时会有延迟?

UIPanGestureRecognizer ***视图未获取事件,子视图使用它们

在 UITableView 上滑动删除以使用 UIPanGestureRecognizer

在 UIPanGestureRecognizer 中使用velocityInView

使用 UIPanGestureRecognizer 拖动 + 旋转触摸下车

使用 UIPanGestureRecognizer 移动和缩放 UIView