首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用平移手势缩放UITextView

使用平移手势缩放UITextView
EN

Stack Overflow用户
提问于 2019-07-23 18:06:55
回答 1查看 593关注 0票数 1

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

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

对不起,我没有足够的名气来添加图片

文本贴纸视图结构- https://imgur.com/lDz47fV

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

代码语言:javascript
运行
复制
@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的比例:

代码语言:javascript
运行
复制
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()
    }
EN

Stack Overflow用户

发布于 2019-07-26 16:21:12

好的,如果其他人也在寻找类似的问题,我会发布我的解决方案

我在我的类的顶部添加了scale的变量:

代码语言:javascript
运行
复制
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

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

代码语言:javascript
运行
复制
@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
        }
    }
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57161634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档