UIView does not change position in animateWithDuration

I have two UIView (my bad one is UIViewa UIButton) and I am animating at the same time. Initially, I had a look and a container that would liven up just fine and work like a charm.

Now only one of my UIViews will move / animate in animateWithDuration, even if through debugging the frame of another view it says that it is in a position in which it is not.

    CGRect rect = self.toggle.frame;
    CGRect tabRect = self.tabButton.frame;
    rect.origin.x = rect.origin.x - rect.size.width;
    NSLog(@"%f Before",tabRect.origin.x);
    tabRect.origin.x = tabRect.origin.x - rect.size.width;
    NSLog(@"%f After", tabRect.origin.x);
    [UIView animateWithDuration:0.3 animations:^{  // animate the following:
        self.toggle.frame = rect; // move to new location
        self.tabButton.frame = tabRect;
    }];
    NSLog(@"%f AfterAnimation", tabButton.frame.origin.x);

The switch view moves fine, but the view is tabButtonnot animated or moved. It is strange that both debug codes "After" and "AfterAnimation" return the same value, which suggests that the frame really moved. Is there a particular reason why this will not work if toggleis UIViewthat it will work as UIContainerView?

Please note that if I delete the line

self.toggle.frame = rect;

tabButtonwill animate correctly, but if I move toggle, tabButtonit won’t move regardless of whether it is the first in an animation block or a second.

Edit: I tried moving them into separate blocks and changing the center point, not the frame, but to no avail. It seems that if the view togglemoves, tabButtonit will not move.

2: . {

tabButton bg , bg - .

Initial position (<code> toggle </code> is off-screen) correct position

: (toggle )

The problem in question <code> toggle </code> is correct <code> tabButton </code> is not

: toggle tabButton

When <code> self.toggle.frame = rect </code> is commented out (<code> tabButton </code> correct, <code> toggle </code> not)

: self.toggle.frame = rect (tabButton , toggle ) }

3: , . {

, toggle , , tabButton . , tabButton toggle / , , .

}

4: {

tabButton tabButton.frame = CGRectMake(10,10,100,100), View , .

}

/TL;DR, .

  • toggle - ToggleDraw, UIView, .

  • tabButton UIButton, IB viewController

  • toggle tabButton self.view

  • - , ,

    ,
  • toggle tabButton

+4
4

UIView, IB ( ).

layoutIfNeeded() :

self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5) { () -> Void in
...
+3

, UIView. , tabButton. , (10, 10, 100, 100), .

0

, , . , :

UIView *toggle = [[UIView alloc] initWithFrame:CGRectMake(320, 64, 100, 100)];
[toggle setBackgroundColor:[UIColor redColor]];
[self.view addSubview:toggle];

UIButton *tabButton = [[UIButton alloc] initWithFrame:CGRectMake(220, 64, 100, 100)];
[tabButton setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:tabButton];

CGRect rect = toggle.frame;
CGRect tabRect = tabButton.frame;
rect.origin.x = rect.origin.x - rect.size.width;
NSLog(@"%f Before",tabRect.origin.x);
tabRect.origin.x = tabRect.origin.x - rect.size.width;
NSLog(@"%f After", tabRect.origin.x);
[UIView animateWithDuration:1 animations:^{  // animate the following:
    toggle.frame = rect; // move to new location
    tabButton.frame = tabRect;
}];

, , mainthread:

dispatch_async(dispatch_get_main_queue(), ^{
    [UIView animateWithDuration:0.3 animations:^{
        self.toggle.frame = rect; // move to new location
        self.tabButton.frame = tabRect;
    }];
});

, , , , , .

, , :

[UIView animateWithDuration:0.3 animations:^{
    self.toggle.frame = rect; // move to new location
    self.tabButton.frame = tabRect;
} completion:^(BOOL finished){
    NSLog(@"Finished animating!");
}];
0

. UIButton (tabButton), , .

, , , , . , .

- , , , , .

.

0

All Articles