2015-08-29 121 views
1

點擊按鈕後,我想要一個UIView向下滑動約50%的屏幕。幻燈片應該是動畫的。以下是我迄今爲止嘗試:用動畫滑動UIView

- (IBAction)moveDownButtonClicked:(id)sender { 

    _sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width 
                  , [[UIScreen mainScreen] bounds].size.height*0.5)]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    _sliderView.frame = CGRectMake(0,0,320,20); 
    [UIView commitAnimations]; 
} 
+0

當你運行該代碼時會發生什麼? – stone

+0

沒有任何反應。不動畫 – Illep

+2

我想你忘了添加[self.view添加子視圖:_sliderView] –

回答

2

首先,你必須插入_sliderView視圖層次結構:

[self.view addSubview: _sliderView]; 

[self.view.window addSubview: _sliderView]; 

然後進行變換。

也許這你想要什麼:

- (IBAction)moveDownButtonClicked:(id)sender { 

    _sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width 
                  , 0)]; 

    [self.view.window addSubview: _sliderView]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    _sliderView.frame = CGRectMake(0,0, 
            [[UIScreen mainScreen] bounds].size.width, 
            [[UIScreen mainScreen] bounds].size.height*0.5); 
    [UIView commitAnimations]; 
}