swift 单指旋转图片,超简单(已封存,开箱即用)

Posted ihoudf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 单指旋转图片,超简单(已封存,开箱即用)相关的知识,希望对你有一定的参考价值。

工具类:

//  Created by hdf on 2022/3/31.

import UIKit

class UIOneFingerRotationGes: UIGestureRecognizer 

    var rotation: CGFloat = 0.0
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) 
        if (event.touches(for: self)?.count ?? 0) > 1 
            self.state = .failed
        
    
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) 
        if self.state == .possible 
            self.state = .began
         else 
            self.state = .changed
        
        
        guard let touch = touches.first, let view = self.view else  return 
        let center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
        let currentTouchPoint = touch.location(in: view)
        let previousTouchPoint = touch.previousLocation(in: view)
        
        let f1 = Float(currentTouchPoint.y - center.y)
        let f2 = Float(currentTouchPoint.x - center.x)
        let f3 = Float(previousTouchPoint.y - center.y)
        let f4 = Float(previousTouchPoint.x - center.x)
        let angle = atan2f(f1, f2) - atan2f(f3, f4)
        self.rotation = CGFloat(angle)
    
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) 
        if state == .changed 
            state = .ended
         else 
            state = .failed
        
    
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) 
        state = .cancelled
    
    

使用方法:


let oneRotate = UIOneFingerRotationGes(target: self, action: #selector(oneFingerRotateAction(ges:)))        
compassImgV.addGestureRecognizer(oneRotate)
   @objc private func oneFingerRotateAction(ges: UIOneFingerRotationGes) 
        compassImgV.transform = compassImgV.transform.rotated(by: ges.rotation)
    

以上是关于swift 单指旋转图片,超简单(已封存,开箱即用)的主要内容,如果未能解决你的问题,请参考以下文章

swift 单指旋转图片,超简单(已封存,开箱即用)

讲解一下iOS图片单指旋转缩放实现方法

自主封装hooks+echarts(超详细介绍)开箱即用框架

Magicodes.Pay,打造开箱即用的统一支付库,已提供ABP模块封装

Delphi 后端随机生成前端验证码图片程序源代码,开箱即用

vue - 音乐播放器示例源码开箱即用(无任何第三方插件依赖,代码注释详细)