简体   繁体   English

动画完成后,iOS停止UIbutton取代自己的位置

[英]IOS stop UIbutton to take its own place after animation completed

I have set the animation like seen in below image. 我已经设置了动画,如下图所示。 In which UIbutton move from left to right and then top to bottom. UIbutton在其中从左到右然后从上到下移动。 Animation work correctly but after completion of animation UIButton comes to its original place before the segue perform. 动画可以正常工作,但是动画完成后,UIButton会在segue执行之前返回其原始位置。 so, it's not look good. 所以,看起来不太好。 I want to set that after the completion of animation UIButton can't come to it's own place before segue . 我想设置为动画UIButton完成后不能在segue之前到达它自己的位置。

Here is my try with Image. 这是我对Image的尝试。

   //Move button Left to Right
- (IBAction)btnEasy:(id)sender {
Easy=YES;
NSLog(@"your x is: %f ", self.btnEasy.frame.origin.x);
NSLog(@"your y is: %f ", self.btnEasy.frame.origin.y);

x1=self.btnEasy.frame.origin.x;
y1=self.btnEasy.frame.origin.y;

CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView animateWithDuration:1.150 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.btnEasy.frame = CGRectMake(screenRect.size.width/1.80, self.btnEasy.frame.origin.y, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height);

    [self performSelector:@selector(btneasyanimation) withObject:self afterDelay:1.160 ];}
                      completion:^(BOOL finished) {

                      }];

  //Move Button top to Bottom

    if (Easy==YES) {
        if (isiPad2 || isiPadAir || isiPadRatina) {
          //[self.view layoutIfNeeded];
                [UIView beginAnimations:nil context:NULL];
                [UIView setAnimationBeginsFromCurrentState:YES];
                [UIView setAnimationDuration:1.0];
                [_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+290, self.view.frame.origin.y+900, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
                [UIView commitAnimations];


        }
     else   if (isiPhone4s) {

            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationBeginsFromCurrentState:YES];
            [UIView setAnimationDuration:1.0];
            [_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+92, self.view.frame.origin.y+428, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
            [UIView commitAnimations];


        }
    }
  [self performSelector:@selector(segueeMethod) withObject:self afterDelay:1.160 ];

Image :- 图片 :-

在此处输入图片说明

If you are not placing directly the button given it the frame, thus using autolayout (autoresize will end with the same effect). 如果您没有直接放置按钮给它框架,则使用自动布局(自动调整大小将具有相同的效果)。 You need to explicitly use autolayout, retain a reference to your constraints and update them (you can search here how to do that) and then set the UIView animation block [button layoutIfNeeded] 您需要显式使用自动布局,保留对约束的引用并进行更新(可以在此处搜索操作方法),然后设置UIView动画块[button layoutIfNeeded]

You can see a detailed answer about it in this answer 您可以在此答案中看到有关它的详细答案

There is an easy and quick way that will work with your current implementation. 有一种简单快捷的方法可以与您当前的实现一起使用。 Provided you know exactly where you want the view to be once the animation is done, you can do the following: 只要您知道动画制作完成后视图的确切位置,就可以执行以下操作:

in UIView animateWithDuration: ... assign the final transform (position & orientation) of the view in the completion block, ie: UIView animateWithDuration: ...在完成块中分配视图的最终变换(位置和方向),即:

completion:^(BOOL finished) {
// Assign views transform and appearance here, for when the animation completes
}

Hope that helps. 希望能有所帮助。

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

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