簡體   English   中英

動畫寬度標簽快速

[英]Animated width label with swift

自我的快速代碼以來,我想更改標簽的寬度。 如果可能,請在動畫效果中進行更改。 可能嗎 ? 當我做:

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

我的交換標簽寬度不...為什么? 先感謝您。

編輯:第一個響應不起作用。

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)

這段代碼可以很好地在控制台中顯示我:

150.0
300.0

但是我的標簽尺寸並沒有改變顯示...

您正在更改框架,但是並沒有告訴視圖它需要重繪自身。 更改框架后,您應該致電

self.lblchoice1.setNeedsDisplay()

盡管您可能需要將標簽的內容模式更改為UIViewContentModeRedraw ,以確保重新繪制。

盡管最好使用UIView塊動畫方法來執行此操作。

我不認為您可以直接編輯UILabel的框架。 您應該改為更改整個框架。

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