【问题标题】:Scale UITextView with pan gesture使用平移手势缩放 UITextView
【发布时间】:2019-07-23 10:06:55
【问题描述】:

我正在制作一个功能,用户可以通过拖动右下角使用平移手势添加文本贴纸、缩放和旋转它。

我创建了一个继承自 UIView 的自定义类,我将 UITextView 放在上面,按钮的角上有 3 个视图(关闭、编辑和缩放/旋转),顶部有透明的 UIView 用于手势。

抱歉,我没有足够的声誉来添加图片

Text Sticker 视图结构 - https://imgur.com/lDz47fV

我的尝试是在平移手势上使用 CGAffineTransform。平移手势位于旋转按钮视图(左下角)

@IBAction func resizeGestureRecognizer(_ sender: UIPanGestureRecognizer) {
        let touchLocation = sender.location(in: self.superview)
        let center = self.center

        switch sender.state {
        case .began:
            self.deltaAngle = CGFloat(atan2f(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))) - CGAffineTransformGetAngle(self.transform)
            self.initialDistance = CGPointGetDistance(point1: center, point2: touchLocation)
            //self.initialBounds = self.bounds
        case .changed:
            let angle = atan2f(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))
            let angleDiff = Float(self.deltaAngle) - angle
            let scale = CGPointGetDistance(point1: center, point2: touchLocation) / self.initialDistance

            // downscale buttons to ramain same size and rotation
            for button in buttonViews {
                var t = CGAffineTransform.identity
                t = t.rotated(by: CGFloat(angleDiff))
                t = t.scaledBy(x: 1/scale, y: 1/scale)
                button.transform = t
            }

            var t = CGAffineTransform.identity
            t = t.rotated(by: CGFloat(-angleDiff))
            t = t.scaledBy(x: scale, y: scale)
            self.transform = t

//            textView.layer.shouldRasterize = true
//            textView.layer.rasterizationScale = 20
        case .ended, .cancelled:
            print("ended")
        default:
            break
        }
    }

此代码有效,但每次我尝试调整贴纸大小或旋转贴纸时,它都会缩小到原始比例。我错过了什么?

结果 - https://imgur.com/QHPlI93

另外,作为缩放字体质量的一种解决方法,我使用巨大的字体大小(100)和 0.5 的比例用于贴纸:

override func awakeFromNib() {
        textView.textContainerInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        frameView.layer.cornerRadius = 13
        showEditingHandlers = true
        textView.becomeFirstResponder()

        let scale: CGFloat = 0.5

        for button in buttonViews {
            var t = CGAffineTransform.identity
            t = t.scaledBy(x: 1/scale, y: 1/scale)
            button.transform = t
        }


        var t = CGAffineTransform.identity
        t = t.scaledBy(x: scale, y: scale)
        self.transform = t

        textView.delegate = self
        update()
    }

【问题讨论】:

    标签: swift rotation uitextview scale cgaffinetransform


    【解决方案1】:

    好的,如果其他人会搜索类似的问题,我会发布我的解决方案

    我在班级顶部添加了规模变量:

    var scale: CGFloat = 0
    var savedScale: CGFloat = 0.5 // 0.5 becuase I use double font size and scaled my sticker by 0.5 to handle font blur
    

    接下来,在我的手势句柄函数中,我只保存最后一个比例,如下所示:

    @IBAction func resizeGestureRecognizer(_ sender: UIPanGestureRecognizer) {
            let touchLocation = sender.location(in: self.superview)
            let center = self.center
    
    
    
            switch sender.state {
            case .began:
    
                print("saved scale = \(savedScale)")
                self.deltaAngle = CGFloat(atan2f(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))) - CGAffineTransformGetAngle(self.transform)
                self.initialDistance = CGPointGetDistance(point1: center, point2: touchLocation)
            case .changed:
                let angle = atan2f(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))
                let angleDiff = Float(self.deltaAngle) - angle
                scale = CGPointGetDistance(point1: center, point2: touchLocation) / self.initialDistance
    
                print("scale: \(scale)")
                scale = scale * savedScale // <- added this
    
                // downscale buttons to ramain same size and rotation
                for button in buttonViews {
                    var t = CGAffineTransform.identity
                    //t = t.rotated(by: CGFloat(angleDiff))
                    t = t.scaledBy(x: 1/scale, y: 1/scale)
                    button.transform = t
                }
    
                var t = CGAffineTransform.identity
                t = t.rotated(by: CGFloat(-angleDiff))
                t = t.scaledBy(x: scale, y: scale)
                self.transform = t
            case .ended, .cancelled:
                print("ended")
                savedScale = 1 * scale // <- and this
            default:
                break
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      相关资源
      最近更新 更多