Swift CGAffineTransform 按比例缩放,而不是按比例缩放

Swift CGAffineTransformScale to a scale, not by a scale

假设我使用 CGAffineTransformScale 缩放 UILabel,如下所示:

let scale = 0.5
text = UILabel(frame: CGRectMake(100, 100, 100, 100))
text.text = "Test"

UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
    self.text.transform = CGAffineTransformScale(self.text.transform, scale, scale)
}, completion: {(value : Bool) in
    print("Animation finished")
})

当我想将 UILabel 缩放一半时,这非常有用。但是,如果我再次调用相同的代码,它将以 0.25 的比例结束,因为它再次缩小了一半。

是否可以使用 CGAffineTransformScale 始终缩放到原始 UILabel 框架的一半大小,而不是累积缩放?

您正在缩放现有变换。只需创建一个新转换:

self.text.transform = CGAffineTransformMakeScale(scale, scale)

Swift 3:

text.transform = CGAffineTransform.identity
UIView.animate(withDuration: 0.25, animations: {
   self.text.transform = CGAffineTransform(scaleX: scale, y: scale)
})

Swift版本:

self.text.transform = CGAffineTransform.init(scaleX: scaleFactorX, y: scaleFactorY)