2012-07-12 73 views
1

我有一個UILabel和我想要做的是平滑地改變它的高度爲0動畫的UILabel的高度爲0

CGRect newFrame = self.label1.frame; 
newFrame.size.height = 0; 

[UIView animateWithDuration:1.0 animations:^{ 

    self.label1.frame = newFrame; 
}]; 

的問題是,動畫干擾做一些奇怪的事情。我猜這個標籤試圖調整它的大小並重新定位它的文本,這就是爲什麼它不起作用。我嘗試過所有可能的標籤屬性組合,但沒有成功。

+0

你想要/你期望的文本,而其高度變化? – 2012-07-12 19:00:46

+0

是你想要動畫的高度還是位置? – trumpetlicks 2012-07-12 19:00:48

回答

7

你可以附上UILabel到另一個UIView,封閉UIViewautoresizesSubviews「屬性NOclipToBounds設置爲YES,然後動畫封閉UIView的高度...

+0

好主意,thx。 – 2012-07-12 19:18:03

3

與變換嘗試。我爲你挖出這個代碼:

//[self setAnchorPoint:CGPointMake(0.5, 1.0) forView:self.label]; 
    [UIView animateWithDuration:0.3 
         delay:0 
        options:UIViewAnimationCurveEaseInOut 
       animations:^{ 
        //Scale the height to close to zero 
        //0.00001, because 0.0 behaves strange 
        self.label.transform = CGAffineTransformMakeScale(1, 0.00001); 
       } 
       completion:^(BOOL finished) { 
        self.label.hidden = YES; 
       }]; 

這會縮小到中間。如果您希望它縮小到頂部或底部,則需要設置錨點。我也有這樣的一些代碼:

- (void) setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view { 
CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y); 
CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y); 

newPoint = CGPointApplyAffineTransform(newPoint, view.transform); 
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform); 

CGPoint position = view.layer.position; 

position.x -= oldPoint.x; 
position.x += newPoint.x; 

position.y -= oldPoint.y; 
position.y += newPoint.y; 

view.layer.position = position; 
view.layer.anchorPoint = anchorPoint; 
}