简体   繁体   English

动画后如何将UIImage保持在新位置?

[英]How can I keep UIImage in new position after Animation?

I am moving UIImages across the screen, they are cards. 我正在屏幕上移动UIImage,它们是卡片。 When they move across the screen, the images are generic card backs. 当它们在屏幕上移动时,图像就是普通的卡片背面。 When they land in the correct positions, they are supposed to stay in that position however they revert back to the original points. 当它们降落在正确的位置时,它们应该停留在该位置,但是它们会返回到原始点。 The original points of playerCardOne , playerCardTwo , and playerCardThree are (60,28 , 90,28 , 120,28) respectively. playerCardOneplayerCardTwoplayerCardThree的原始点playerCardThree(60,28 , 90,28 , 120,28)

Here is the code: 这是代码:

[playerCardOne setImage:[UIImage imageNamed: @"cardBack.png"] forState:UIControlStateNormal];
[playerCardTwo setImage:[UIImage imageNamed: @"cardBack.png"] forState:UIControlStateNormal];
[playerCardThree setImage:[UIImage imageNamed: @"cardBack.png"] forState:UIControlStateNormal];

then animation: 然后是动画:

[UIView animateWithDuration:1.0f
                 animations:^(void){
                     playerCardOne.center = CGPointMake(120.0f, 28.0f);
                     playerCardTwo.center = CGPointMake(90.0f, 28.0f);
                     playerCardThree.center = CGPointMake(60.0f, 28.0f);
                         } 
                 completion:^(BOOL finished) { 
                     if (finished) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                         [playerCardOne setImage:[UIImage imageNamed:playerCardOneTitle] forState:UIControlStateNormal];
                         [playerCardTwo setImage:[UIImage imageNamed:playerCardTwoTitle] forState:UIControlStateNormal];
                         [playerCardThree setImage:[UIImage imageNamed:playerCardThreeTitle] forState:UIControlStateNormal];
                     }
   }];

When the animation completes the front card images pop up and return to their original positions (ie playerCardOne should be at (120.0, 28.0) but reverts back to (60.0, 28.0) . How can I lock them in their new positions? 动画完成后,会弹出前卡图像并返回其原始位置(即playerCardOne应该位于(120.0, 28.0) playerCardOne (120.0, 28.0)但又还原为(60.0, 28.0) playerCardOne (60.0, 28.0) 。如何将它们锁定在新位置?

If you are using AutoLayout you should animate the constraints instead of the frames of items. 如果使用的是自动版式,则应设置约束动画,而不是项目框架动画。

There is some information on this at objc.io under Animation. objc.io的 “动画”下对此有一些信息。

Basically you modify a constraint and then relayout over a time period. 基本上,您可以修改约束,然后在一段时间内重新布局。

[UIView animateWithDuration:1.0f animations:^{
    [myView layoutIfNeeded];
}];

If you turn-off autolayout , then the button should stay at the new position. 如果关闭自动autolayout ,则按钮应保持在新位置。 The reason is because autolayout will use the current constraints to calculate the position when you update the views. 原因是因为在更新视图时,自动autolayout将使用当前约束来计算位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM