2012-08-13 103 views
1

在我的應用程序中,我有一個滾動視圖,當我按下按鈕時,它被隱藏,當我再次按下它時出現。我想用scrollview.hidden = YES(或NO)來做。隱藏並顯示帶動畫的滾動視圖

但我想用動畫做。例如,它可能會通過移動並以相同的方式顯示,從屏幕底部消失。我怎樣才能做到這一點?

編輯:

[UIView beginAnimations:@"myAnimation" context:nil]; 
CGRect Frame = bottomScroller.frame; 
if(Frame.origin.y == 380){ 
    Frame.origin.y = 460; 
}else{ 
    Frame.origin.y = 380; 
} 
bottomScroller.frame = Frame; 
[UIView commitAnimations]; 

這解決了我的問題......

回答

5

您可以檢查的UIView animations。這很容易。例如動畫翻譯可能會使用類似:

[UIView beginAnimations:@"myAnimation" context:nil]; 
CGRect Frame = yourView.frame; 
Frame.origin.y = 0; 
yourView.frame = Frame; 
[UIView commitAnimations]; 

然後將其移回:

[UIView beginAnimations:@"myAnimation" context:nil]; 
CGRect Frame = yourView.frame; 
Frame.origin.y = 100; 
yourView.frame = Frame; 
[UIView commitAnimations]; 

變化框架,阿爾法和其他一些參數會自動動畫。

+0

謝謝。這正是我正在尋找的。 – death7eater 2012-08-13 13:47:26

+0

如果我在代碼中使用類似這樣的東西,這個解決方案對我來說不適用於Textfield:'[_nameOfSandwich setText:@「」];'。由於該行,textfield不會移動。任何想法如何解決它? – Segev 2012-12-12 20:20:06

+0

看起來就像你解釋你所看到的錯誤,或者在代碼中有一些愚蠢的錯誤,上面的錯誤可以肯定地工作。 – 2012-12-13 06:16:21

3

你可以用動畫像這樣: -

[UIView animateWithDuration:2.0 animations:^{ 
    [scrollview setframe:CGRectMake(xpos, ypos, width, height)]; 
}]; 

如果你想滾動視圖或關閉屏幕中將其y位置滑動 - 視圖的底部,你想隱藏它,正常y位置,如果你想顯示它。

2.0是動畫長度,這可以更改爲任何你需要它!

1

您可以使用簡單的視圖動畫來做到這一點。這裏有一個如何做到這一點的例子:

// myScrollView is your scroll view 

-(void)toggleShow{ 
    CGFloat targetAlpha = myScrollView.alpha == 1 ? 0 : 1; 
    CGFloat yPosition = targetAlpha == 1 ? 0 : self.view.frame.size.height; 

    [UIView animateWithDuration:1.0 animations:^{ 
     myScrollView.frame = CGRectMake(myScrollView.frame.origin.x, yPosition, myScrollView.frame.size.width, myScrollView.frame.size.height); 
     myScrollView.alpha = targetAlpha; 
    }]; 
} 

targetAlpha將始終是當前狀態的相反(1或0),如果是0,那麼y位置將被設置到父的底部視圖。使用帶有塊的新學校UIView動畫API,我們可以在1秒內執行滾動視圖的這些更改(在我的示例中)。

+0

它像一個魅力工作。謝謝,但唯一的想法,我沒有弄清楚例如我有一個水平滾動視圖在頁面底部(x,y,w,h)(0,380,320,80)當我運行它scrollview幻燈片關閉從屏幕的底部(很棒!),但是當我再次按下我的按鈕時,我希望它回到確切的位置,然後再將它移開。但是,在此代碼中,我的滾動視圖移至主視圖的頂部。你有什麼想法我該怎麼做? – death7eater 2012-08-13 13:27:42

1

ScrollView從屏幕的下方的動畫中提出。我認爲這個動畫就是你想要的。

// ScrollView appearing animation 
- (void)ScrollViewUpAnimation 
{ 
    // put the scroll view out of screen 
    CGRect frame = ScrollView.frame; 
    [self.view addSubview:ScrollView]; 
    ScrollView.frame = CGRectMake(0, 460, frame.size.width, frame.size.height); 

    // setting for animation 
    CGPoint fromPt = ScrollView.layer.position; 
    CGPoint toPt = CGPointMake(fromPt.x, fromPt.y - frame.size.height - 44); 
    CABasicAnimation* anime = 
    [CABasicAnimation animationWithKeyPath:@"position"]; 
    anime.duration = 0.2; 
    anime.fromValue = [NSValue valueWithCGPoint:fromPt]; 
    anime.toValue = [NSValue valueWithCGPoint:toPt]; 

    // change the position of Scroll View when animation start 
    [ScrollView.layer addAnimation:anime forKey:@"animatePosition"]; 
    ScrollView.layer.position = toPt; 
}