UILabel使用UIPinchGestureRecognizer模糊文本

时间:2016-04-28 15:19:35

标签: ios objective-c uipinchgesturerecognizer

我有一个UILabel;当我使用UIPinchGestureRecognizer放大它时,文本变得模糊。 before

after

我使用CGAffineTransformScale我的代码

self.myLabel.transform = CGAffineTransformScale(self.myLabel.transform, pinchRecognizer.scale, pinchRecognizer.scale);

如何解决?

2 个答案:

答案 0 :(得分:1)

经过大量的尝试和失败。我找到了解决方案。

  @IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
    var pinchScale = recognizer.scale
    signatureLabel.layer.contentsScale = UIScreen.main.scale + pinchScale;
    signatureLabel.transform = signatureLabel.transform.scaledBy(x: pinchScale, y:pinchScale)
    pinchScale = round(pinchScale * 1000) / 1000.0

    if recognizer.state == .changed {
        signatureLabel.font = UIFont(name: signatureLabel.font.fontName, size: signatureLabel.font.pointSize * pinchScale)
        pinchScale = recognizer.scale
    }
    recognizer.scale = 1
}

虽然如果不更新该UILabel的约束,它将无法正常工作。因此,在故事板中,我添加了Vertical&对我的标签的水平中心约束。然后,在我班上创建出口。在我的handlePan方法中,我做了:

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: recognizer.view)
    self.signatureLabel.center = CGPoint(x:self.signatureLabel.center.x + translation.x,
                                         y:self.signatureLabel.center.y + translation.y)
    signatureLabelCenterConstraint.constant = signatureLabelCenterConstraint.constant + translation.y
    signatureLabelCenterXConstraint.constant = signatureLabelCenterXConstraint.constant + translation.x
    signatureLabel.setNeedsLayout()
    recognizer.setTranslation(.zero, in: recognizer.view)
}

我希望能帮助大家!

答案 1 :(得分:0)

我找到了如何解决这个问题,这很容易。

给出比例:

CGFloat scale =  self.myLable.transform.a *pinchRecognizer.scale *[UIScreen mainScreen].scale;

这个 - (self.myLable.transform.a)返回当前比例因子。

 self.myLable.transform = CGAffineTransformScale(self.myLable.transform, pinchRecognizer.scale, pinchRecognizer.scale);
[self.myLable setContentScaleFactor:scale];

完成, 快乐的编码!