2015-08-29 81 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]; 
}