有没有办法改变一个UILabel的textAlignmentanimation?

我正在使用iOS 7,我试图移动一个标签,它位于我的视图的左侧。 目前我正在尝试通过改变我的UILabel如何alignment来实现这一点,我正在尝试在这个过程中对它进行animation处理。 我目前正在呼叫以下内容:

[UIView animateWithDuration:0.5 animations:^{ self.monthLabel.textAlignment = NSTextAlignmentLeft; } completion:nil]; 

但是这只是将标签跳到新的路线。 有没有一种方法来animation,或更好的方式来调整标签,使之发生?

将UILabel框架大小设置为精确地包含文本并将UILabel居中在您的视图中。

 self.monthLabel.text = @"February"; [self.monthLabel sizeToFit]; self.monthLabel.center = parentView.center; // You may need to adjust the y position 

然后设置不会影响布局的alignment,因为没有多余的空间。

 self.monthLabel.textAlignment = NSTextAlignmentLeft; 

接下来,animationUILabel框架大小,以便滑过你想要的地方。

 [UIView animateWithDuration:0.5 animations:^{ CGRect frame = self.monthLabel.frame; frame.origin.x = 10; self.monthLabel.frame = frame; } completion:nil]; 

你的标签是多行吗? 像这样的animation: http : //img62.imageshack.us/img62/9693/t7hx.png ?

如果是这样,那么有几个select。

多线标签

选项1:

而不是传统的UIViewanimation,请尝试UIView转换。 文字不会滑到左边,而是会很好地淡入到新的位置。

 [UIView transitionWithView:self.monthLabel duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.monthLabel.textAlignment = NSTextAlignmentLeft; } completion:NO]; 

选项2:

您可以手动计算出新行的出现位置,然后为每行文本创build一个单独的UILabel,然后对框架进行animation处理。 这显然是更多的工作,但会给予期望的幻灯片animation。

单行标签

使用[self.monthLabel sizeToFit]使标签与包含的string的大小相同,然后手动计算出框架和居中,而不是使用textAlignmentanimation。 然后animation框架,就像多线标签上的选项2一样。

您可以为FRAME制作animation,而不是textAlignment。

如果你想摆脱框架上的任何“填充”,那么在你的标签上做一个[UILabel sizeToFit],然后你可以animation框中的animation块来移动它,

 CGRect frameLabel = self.monthLabel.frame; [UIView animateWithDuration:0.5 animations:^{ frameLabel.origin.x -= 100; // eg move it left by 100 self.monthLabel.frame = frameLabel; } completion:nil]; 

你只需要animation你的标签框架。 因为这里有完成块,所以你应该再次改变帧。 并使循环进行。

 [UIView animateWithDuration:0.5 animations:^{ // Change the frame } completion:^{[UIView animateWithDuration:0.5 animations:^{ // Change the frame } completion:^{ // repeat the proccess }] }]; 

这对我很好

 import Foundation import UIKit class AnimatableMultilineLabel: UIView { enum Alignment { case left case right } public var textAlignment: Alignment = .left { didSet { layout() } } public var font: UIFont? = nil { didSet { setNeedsLayout() } } public var textColor: UIColor? = nil { didSet { setNeedsLayout() } } public var lines: [String] = [] { didSet { for label in labels { label.removeFromSuperview() } labels = [] setNeedsLayout() } } private var labels: [UILabel] = [] override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() } private func setup() { isUserInteractionEnabled = false } override func layoutSubviews() { super.layoutSubviews() layout() } private func layout() { autocreateLabels() var yPosition: CGFloat = 0.0 for label in labels { let size = label.sizeThatFits(bounds.size) let minX = textAlignment == .left ? 0.0 : bounds.width - size.width let frame = CGRect(x: minX, y: yPosition, width: size.width, height: size.height) label.frame = frame yPosition = frame.maxY } } private func autocreateLabels() { if labels.count != lines.count { for text in lines { let label = UILabel() label.font = font label.textColor = textColor label.text = text addSubview(label) labels.append(label) } } } override func sizeThatFits(_ size: CGSize) -> CGSize { autocreateLabels() var height: CGFloat = 0.0 for label in labels { height = label.sizeThatFits(size).height } return CGSize(width: size.width, height: height) } } 

那你应该可以的

 UIView.animate(withDuration: 0.5, animations: { multilineLabel.textAlignment = .right })