我正在制作一个功能,用户可以添加文本贴纸,缩放和旋转,通过拖动右下角使用平移手势。
我做了一个继承自UIView的自定义类,我在上面放了UITextView,三个角上的视图用于按钮(关闭,编辑和缩放/旋转),透明的UIView在顶部用于手势。
对不起,我没有足够的名气来添加图片
文本贴纸视图结构- 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
}
}这段代码可以工作,但每次我尝试调整大小或旋转贴纸时,它都会缩回到原来的比例。我遗漏了什么?
此外,作为缩放字体质量的变通方法,我对贴纸使用了巨大的字体大小(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()
}发布于 2019-07-26 16:21:12
好的,如果其他人也在寻找类似的问题,我会发布我的解决方案
我在我的类的顶部添加了scale的变量:
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
}
}https://stackoverflow.com/questions/57161634
复制相似问题