0

我正在尝试模拟键盘出现动画,仅使用将向用户显示三个按钮的自定义子视图。有什么办法可以用情节提要完成这个(即无需以编程方式创建子视图)?

4

1 回答 1

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:,子视图不为零(并且已正确初始化),并且既不是 alpha 也不是 opacity 子视图的属性设置为 0。

于 2012-07-20T02:08:35.700 回答