![](/img/trans.png)
[英]UIView resize and translate animation does not animate subviews correctly
[英]resize uiview with UILabel and animate it correctly
我有一個UIView作為帶有UILabel的容器,如下所示:
------------- ---------
| | | |
|Label text | |Label..|
| | --> | |
| | | |
| | | |
------------- ---------
現在,當我使用時:
UIView animateWithDuration:animations:
並嘗試設置較小的UIView(包含UILabel)的寬度,然后在動畫過程中UILabel突然替換為“ ...”,而沒有平滑過渡到它。 我將UILabel自動調整大小蒙版設置為UIViewAutoresizingFlexibleWidth,UIViewAutoresizingFlexibleRightMargin以使其保持在左側並將contentmode設置為left。
嘗試其他內容模式,例如:縮放以填充,縱橫比適合,縱橫比填充也不能解決我的問題,因為它們縮放文本然后看起來很奇怪。
有人知道如何為UILabel順利過渡嗎?
不久前,我以描述的方式對我的標簽進行了動畫處理,但您必須在其中花一些錢。 基本思想:-使用clipsToBounds:YES將標簽添加到容器視圖。 -不要為標簽框架設置動畫,而是為containerview的框架設置動畫,這樣您的標簽將具有平滑的過渡效果。 -使用單獨的標簽作為省略號(...),以便也可以為該部分設置動畫。
不幸的是,標簽無法縮放。 更改其框架會更改文本的放置位置,但是文本的大小不會以這種方式縮放。 因此基本動畫就消失了,另一種方法是觸發一個計時器,該計時器在每次觸發時將標簽的幀縮小預定量。
當然,您需要將標簽setAdjustsFontSizeToFitWidth
屬性設置為YES
。
注意:如果可能,請避免實際每5/1000秒觸發一次計時器。
- (void)fireTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.005 target:self selector:@selector(animate) userInfo:nil repeats:YES];
}
- (void)animate
{
[label setAdjustsFontSizeToFitWidth:YES];
if (label.frame.size.width > 100) {
[label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width-1, label.frame.size.height)];
}else{
[timer invalidate];
timer = nil;
}
}
在自動旋轉期間調整UILabel的大小時,我遇到了類似的問題,並通過使用CADisplayLink這樣解決了它。
首先,在willAnimateRotationToInterfaceOrientation方法中調用displayLinkTimer。
displayLinkTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(resizeTextLabel)];
[displayLinkTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
然后,通過以與我創建的不可見UIView相同的速率調整標簽的大小來調整標簽的大小,該方法稱為textView。 textView的框架與我希望UILabel的框架相同,並且其大小在willAnimateRotationToInterfaceOrientation方法中進行了更改。 通過訪問textView的presentationLayer,我能夠以相同的速度調整大小。
- (void)resizeTextLabel{
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
if (textLabel.frame.size.width < textView.frame.size.width -20) {
textLabel.frame = CGRectMake(0, 0, [[textView.layer presentationLayer] frame].size.width, [[textView.layer presentationLayer] frame].size.height);
}else{
[timer invalidate];
timer = nil;
}
}
else{
if (textLabel.frame.size.width > textView.frame.size.width -20) {
textLabel.frame = CGRectMake(0, 0, [[textView.layer presentationLayer] frame].size.width, [[textView.layer presentationLayer] frame].size.height);
}else{
[timer invalidate];
timer = nil;
}
}
}
希望這對某人有幫助。
老問題,但這對我有幫助:
override var frame: CGRect {
didSet {
self.layoutSubviews()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.