简体   繁体   English

动画宽度标签快速

[英]Animated width label with swift

I want to change the width of my label since my swift code. 自我的快速代码以来,我想更改标签的宽度。 If possible, do it in a annimation prograssif for a change. 如果可能,请在动画效果中进行更改。 Is it possible ? 可能吗 ? When I do: 当我做:

self.lblChoice1.frame.size.width += 50

My exchange label width not ... Why? 我的交换标签宽度不...为什么? Thank you in advance. 先感谢您。

EDIT : The first response is not working. 编辑:第一个响应不起作用。

print(self.lblChoice1.frame.size.width)
self.lblChoice1.frame.size.width += 150
self.lblChoice1.contentMode = UIViewContentMode.Redraw
self.lblChoice1.setNeedsDisplay()
print(self.lblChoice1.frame.size.width)

This code displays me well in my console: 这段代码可以很好地在控制台中显示我:

150.0
300.0

But my label size does not change the display ... 但是我的标签尺寸并没有改变显示...

You are changing the frame, but you aren't telling the view that it needs to redraw itself. 您正在更改框架,但是并没有告诉视图它需要重绘自身。 After changing the frame you should then call 更改框架后,您应该致电

self.lblchoice1.setNeedsDisplay()

Although you may need to change the label's content mode to UIViewContentModeRedraw to make sure it redraws. 尽管您可能需要将标签的内容模式更改为UIViewContentModeRedraw ,以确保重新绘制。

Although it would be better to use UIView block animation methods to do this. 尽管最好使用UIView块动画方法来执行此操作。

I dont think you can edit the UILabel's frame directly. 我不认为您可以直接编辑UILabel的框架。 You should change entire frame instead. 您应该改为更改整个框架。

var lblChoice1Frame = self.lblChoice1.frame
lblChoice1Frame.size.width += 150

UIView.animateWithDuration(0.3) { () -> Void in
    self.lblChoice1.frame =  lblChoice1Frame
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM