2011-03-26 55 views
1

所有,實現滑動效果的一個UIView

我拼命寫,將動畫在一種「向上滑動」效應,增加一個子視圖的方法,其視圖滑到從屏幕特別點。我可以得到一個UIImageView工作,但我有一段時間應用到UIPickerView。會發生什麼是,UIPickerView只會移動併爲其高度的大約1/4進行動畫處理。 UIImageView確實以0的高度開始並向上滑動到屏幕上。

這是到目前爲止我的代碼:

 

+ (void) slideupView:(UIView*) view duration:(CGFloat) duration completion:(void (^)(BOOL 
            finished)) completion { 
    CGRect endBounds = view.bounds; 
    CGRect startBounds = endBounds; 

    startBounds.size.height = 0; 

    view.bounds = startBounds; 
    view.contentMode = UIViewContentModeTop; 
    view.clipsToBounds = YES; 
    view.layer.anchorPoint = CGPointMake(0.5, 1.0); 

    CGPoint pos = view.layer.position; 
    pos.y += (endBounds.size.height/2); 
    view.layer.position = pos; 

    [UIView animateWithDuration:duration delay:0.0   
       options:UIViewAnimationOptionAllowUserInteraction 
     animations:^{ 
      view.bounds = endBounds; 
     } 
     completion:completion]; 
} 
 

從我讀過的東西,它聽起來就像是鼓勵的方法是使用邊界矩形,而不是框架,因爲框架的做法是行不通的如果你修改了變換。

我也讀過,不能改變UIPickerView的高度,所以我想知道如果這是這個問題的原因。誠然,我還沒有嘗試過任何其他的UIView子類。

有什麼想法?有更好的方法嗎?我只想爲屏幕上的視圖添加動畫,類似於呈現模式視圖控制器的視圖,除了我不希望它覆蓋整個屏幕。我只想要一個幻燈片效果,就像鍵盤出現在屏幕上一樣。

謝謝! 賈斯汀

回答

2

如果你只是想滑動你的視圖,就像鍵盤出現,那麼你可以通過動畫改變視圖的y座標。

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f,460.0f,320.0f,260.0f)]; 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:[self view] cache:YES]; 
[view setFrame:CGRectMake(0.0f,200.0f,320.0f,260.0f)]; 
[UIView commitAnimations]; 
+0

夠簡單,但我不知道爲什麼不使用view.frame屬性? – tia 2011-03-26 04:03:23

+0

但是它不像動畫框一樣簡單,因爲我想從屏幕上的任意一點開始動畫,而不僅僅是從屏幕開始。 – 2011-03-26 19:55:51

+0

是的,從屏幕上的特定點添加視圖動畫不是一件容易的事情,可以通過一種技巧來使用圖像來隱藏動畫,但這取決於您的完整屏幕視圖。 – saadnib 2011-03-27 02:31:14

0

「邊界」是指視圖本身的內部座標系中,而「幀」和「中心」指的是在其父的座標系的視圖中的位置。因此,要將視圖滑動到屏幕上,您需要爲框架或中心設置動畫。