gpt4 book ai didi

swift - 使用平移手势缩放 UITextView

转载 作者:行者123 更新时间:2023-11-30 10:39:32 25 4
gpt4 key购买 nike

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

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

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

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

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

@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()
}

最佳答案

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

我在类(class)顶部添加了比例变量:

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
}
}

关于swift - 使用平移手势缩放 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57161634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com