我正在尝试模拟键盘出现的动画,只使用自定义子视图,它会向用户显示三个按钮。有什么方法可以用故事板来完成这个(即无需以编程方式创建子视图)?
答案 0 :(得分:5)
是的,虽然您将以编程方式设置一些子视图属性。你想要做的是你的UIViewController调用:
[UIView animateWithDuration:animations:completion:]
在任何方法的一侧应该调出键盘尝试以下内容:
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
}];
确保在'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];
}];
与处理子视图时一样,确保框架设置正确,子视图已添加到具有addSubview:
的超级视图,子视图不是nil(并且已正确初始化),并且子视图的alpha或opacity属性设置为0。