ユーザーに3つのボタンを表示するカスタムサブビューのみを使用して、キーボードの表示アニメーションをシミュレートしようとしています。ストーリーボードでこれを達成する方法はありますか (つまり、プログラムでサブビューを作成する必要はありません)?
2399 次
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:」の後またはその中で呼び出されていることを確認してください。animate 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 でないこと (および適切に初期化されていること)、サブビューのアルファ プロパティも不透明度プロパティも設定されていないことを確認してください。 0 に設定されています。
于 2012-07-20T02:08:35.700 に答える