animationUILabel从中心resize?

我想从中心调整我的标签,并激活该过程。 但是,我可以使用的唯一选项是具有相应CALayer的默认锚点的CGAffineTransformMakeScale。

[UIView animateWithDuration:0.5 animations:^{ self.title.transform = CGAffineTransformMakeScale(2.0,1.0); }]; 

但它压缩了里面的文字。 我考虑过这个答案 ,看起来这个问题在Swift中解决了。

但是,我不能只修改frame size部分。 如何在Obj C中解决这个问题?

这将为你工作。 你只需要给宽度和高度的比例(默认1,这将不起作用)。 这将激活您的视图的框架,并将其设置回原来的。

 [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:(void (^)(void)) ^{ self.textLabel.transform=CGAffineTransformMakeScale(3, 1); } completion:^(BOOL finished){ self.textLabel.transform=CGAffineTransformIdentity; }]; 

如果你不想恢复效果比你能做到这一点

 [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:(void (^)(void)) ^{ self.textLabel.transform=CGAffineTransformMakeScale(3, 1); } completion:nil]; 

从我所了解的你想改变UILabel的大小,而不是通过animation的文字。 这可以通过抓取当前标签的框架的副本,然后修改它并将其设置回来。

 var frame = self.myLabel.frame // frame modification ... self.myLabel.frame = frame 

对于sizeTranformAction:你可以通过一个简单的button来连接它来testing行为。

 class SizeTransformViewController: UIViewController { var myLabel: UILabel! = nil // MARK: - View lifecycle override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.setupLabel() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Setups func setupLabel() { self.myLabel = UILabel() self.myLabel.center = self.view.center self.myLabel.text = "Text" self.myLabel.sizeToFit() self.myLabel.textAlignment = NSTextAlignment.Center self.myLabel.backgroundColor = UIColor.redColor() self.view.addSubview(self.myLabel) } // MARK: - Actions @IBAction func sizeTranformAction(sender: AnyObject) { var frame = self.myLabel.frame let size = frame.size frame.size = CGSize(width: 2*size.width, height: size.height) frame.origin.x = self.view.center.x - size.width UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { self.myLabel.frame = frame }, completion: nil) } } 

产量

在这里输入图像说明

我实际上结束了2个标签。 animation序列如下:

  • 创build一个没有文本的临时标签,像bg颜色,字体,字体大小等属性应该与主标签相同。

  • 将临时标签放在主标签上方。 将主标签的文本属性设置为零

  • 将主标签的框架animation到所需的值
  • 当animation完成时,将主标签的文本属性设置为原始值(大概在animateWithDuration:完成处理程序中animateWithDuration:
  • 隐藏临时标签/删除它

你可以改变你的常数来补偿缩放后的新宽度。 这会有帮助吗?

 layoutIfNeeded() let scale: CGFloat = 0.72 let consLeft = NSLayoutConstraint( item: titleLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: titleLabel.frameWidth * -(1-scale)/2 ) consLeft.isActive = true let animations = { self.titleLabel.transform = CGAffineTransform(scaleX: scale, y: scale) self.layoutIfNeeded() } UIView.animate(withDuration: 0.25, delay: 0, options: [.curveEaseOut], animations: animations, completion: nil)