2017-09-10 4 views
-1

私は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!"); 
       }]; 
} 
+0

制約を使用するときは、フレームを調整しないでください。あなたは、制約の 'constant'プロパティを変更する(または一つの制約を無効にし、別の制約を無効にする)べきです。可能な複製https://stackoverflow.com/a/28329399/1271826(tho、確かに迅速な答え、アイデアはObjective-Cと同じです)。または、制約のアニメーションを示すObjective-Cの回答があります:https://stackoverflow.com/a/14190042/1271826 – Rob

+0

ありがとうございました。 IBOutletを介して接続された制約を追加し、定数を使用してアニメーションを作成しましたが、UIViewをオフスクリーンで開始するにはどうすればよいですか? –

+0

leftConstraint.constant - 320.はオフスクリーンを開始する方法の例です。 – GeneCode

答えて

0

制約を使用して、あなたは調整のフレームではありません。

は、ここに私のコードです。制約のconstantプロパティを変更する(または、1つの制約を無効にし、別の制約を無効にする)必要があります。

ここでは、ビューtopViewの一番上の制約にtopConstraintと呼ばれる@IBOutletを追加したと仮定すると、トップからのトップビューをアニメーションの例です:

のObjective-Cでは:

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    self.topConstraint.constant = -self.topView.frame.size.height; // or whatever you want 
    [self.view layoutIfNeeded]; 
    [UIView animateWithDuration:0.5 delay:1 options:0 animations:^{ 
     self.topConstraint.constant = 0; 
     [self.topView layoutIfNeeded]; 
    } completion:nil]; 
} 

Swift:

override func viewDidAppear(_ animated: Bool) { 
    topConstraint.constant = -topView.frame.height // or whatever you want 
    view.layoutIfNeeded() 
    UIView.animate(withDuration: 0.5, delay: 1, animations: { 
     topConstraint.constant = 0 
     self.view.layoutIfNeeded() 
    }) 
} 
関連する問題