私は2つ持っていますUIViews
私は画面上でアニメーションしたいと思います。どちらも画面外に配置されます。上は下に、下は上に動く。制約がなければ、期待どおりに動作します。しかし、UIViews
はアプリの起動時に表示されます(わずかなアニメーションが表示されます)。彼らは画面をアニメーション化します。制約があるUIViewをアニメーション化する方法は?
トップUIView
の場合、先頭のスペース、後続のスペース、トップスペース、および高さがあります。
下部には、UIView
の場合、先頭のスペース、後続のスペース、底部スペース、および高さがあります。
希望するアニメーションを得るためにコードや制約を調整する方法を教えてください。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self animateViewsIn];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.topView.frame = CGRectMake(16.0, -self.topView.frame.size.height, self.topView.frame.size.width, self.topView.frame.size.height);
self.bottomView.frame = CGRectMake(16.0, self.view.bounds.size.height, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)animateViewsIn {
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.topView.frame = CGRectMake(16.0,
self.topView.frame.size.height/8.0, self.topView.frame.size.width,
self.topView.frame.size.height);
self.bottomView.frame = CGRectMake(16.0, (self.view.bounds.size.height-self.bottomView.frame.size.height)-20.0, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
- (IBAction)animateViewsOut:(id)sender {
CGRect basketTopFrame = self.topView.frame;
basketTopFrame.origin.y = -basketTopFrame.size.height;
CGRect basketBottomFrame = self.bottomView.frame;
basketBottomFrame.origin.y = self.view.bounds.size.height;
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
[self.view layoutIfNeeded];
self.topView.frame = basketTopFrame;
self.bottomView.frame = basketBottomFrame;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
制約を使用するときは、フレームを調整しないでください。あなたは、制約の 'constant'プロパティを変更する(または一つの制約を無効にし、別の制約を無効にする)べきです。可能な複製https://stackoverflow.com/a/28329399/1271826(tho、確かに迅速な答え、アイデアはObjective-Cと同じです)。または、制約のアニメーションを示すObjective-Cの回答があります:https://stackoverflow.com/a/14190042/1271826 – Rob
ありがとうございました。 IBOutletを介して接続された制約を追加し、定数を使用してアニメーションを作成しましたが、UIViewをオフスクリーンで開始するにはどうすればよいですか? –
leftConstraint.constant - 320.はオフスクリーンを開始する方法の例です。 – GeneCode