使用故事板为iOS 5中的屏幕底部的按钮设置自定义子视图动画

ios*_*oob 0 uiviewcontroller ios subviews ios5

我试图模拟键盘出现动画,只使用自定义子视图,将显示用户三个按钮.有没有办法用故事板来完成这个任务(即无需以编程方式创建子视图)?

Ped*_*ori 5

快速回答

是的,虽然您将以编程方式设置一些子视图属性.你想要做的是让你的UIViewController调用:

[UIView animateWithDuration:animations:completion:]
Run Code Online (Sandbox Code Playgroud)

详细示例

在任何方法的一面应该调出键盘尝试以下:

CGFloat windowWidth = self.mainView.frame.size.width;
CGFloat windowHeight = self.mainView.frame.size.height;

// center myCustomSubview along the x direction, and put myCustomSubview just below the screen when UIViewController initially gets onto the screen
CGPoint offScreenBelow = CGPointMake(windowWidth/2, windowHeight + (myCustomView.frame.size.y/2));
CGPoint onScreen = CGPointMake(windowWidth/2,windowHeight/2); 
// change the second argument of the CGPointMake function to alter the final height of myCustomSubview

// start myCustomSubview offscreen
myCustomSubview.center = offScreenBelow;
// make sure to add myCustomSubview to the UIViewController's view's subviews
[self.view addSubview:myCustomSubview];
float duration = 1.0; // change this value to make your animation slower or faster. (units in seconds)

// animate myCustomSubview onto the screen
[UIView animateWithDuration:duration
                 animations:^{
                     myCustomSubview.center = onScreen;
                 }
                 completion:^(BOOL finished){
                     // add anything you want to be done as soon as the animation is finished here
                 }];
Run Code Online (Sandbox Code Playgroud)

确保在'viewDidAppear:'之后或在其中调用方法.如果你想让myCustomSubview动画回到屏幕上,请确保在你的UIViewController中执行以下操作:

// set offscreen position same way as above
CGFloat windowWidth = self.mainView.frame.size.width;
CGFloat windowHeight = self.mainView.frame.size.height;

CGPoint offScreenBelow = CGPointMake(windowWidth/2, windowHeight + (myCustomView.frame.size.y/2));

// myCustomSubview is on screen already. time to animate it off screen
[UIView animateWithDuration:duration // remember you can change this for animation speed
                 animations:^{
                     myCustomSubview.center = offScreenBelow;
                 }
                 completion:^(BOOL finished){
                     [myCustomSubview removeFromSuperView];
                 }];
Run Code Online (Sandbox Code Playgroud)

如果您的子视图未显示

与处理子视图时一样,确保框架设置正确,子视图已添加到超级视图中addSubview:,子视图不是nil(并且已正确初始化),并且子视图的alpha和opacity属性都没有设置为0.