Autolayout breaks UIView animation

Fighting for my app to work on both the iPhone 5 and the smaller iPhone. Would be glad to require iOS6 and use Autolayout, because it works great.

However, the application has 4 buttons that regularly change positions, so I assign each button a different location from the array, and then animate the movement with something like this:

[UIView animateWithDuration:0.5 animations:^{ button1.center = [[locations objectAtIndex:0] CGPointValue]; button2.center = [[locations objectAtIndex:1] CGPointValue]; button3.center = [[locations objectAtIndex:2] CGPointValue]; button4.center = [[locations objectAtIndex:3] CGPointValue]; }]; 

This does not work with Autolayout. Nothing changes. The best I got is that it will animate the position, and then immediately bounce back.

When I delete Autolayout, however, all buttons are compressed on smaller iPhones, and since I use dots set to the size of iPhone 5, they end below, placing the bottom line on the screen. The original had different CGPoint numbers that I received from Interface Builder, but they were "wrong", although I think they were "wrong" because they were numbers for using Autolayout. Array so you can see:

 buttonLocations = [NSArray arrayWithObjects: [NSValue valueWithCGPoint:CGPointMake(57, 523)], [NSValue valueWithCGPoint:CGPointMake(109, 523)], [NSValue valueWithCGPoint:CGPointMake(57, 471)], [NSValue valueWithCGPoint:CGPointMake(109, 471)], nil]; 

What should I do to fix this problem? Setting different points for each device size does not seem very effective.

+6
source share
3 answers

The solution is that you are animating the transform property of the elements, not its position.

The position will be set by autorun, and you do not want to fight it. The actual position on the screen, however, will be its “correct” (set auto layout) position, shifted by the conversion property.

So you can say button.transform = CGTransformMake (x, y) and it will display x and y from its center position.

There are many CGWh macros no matter how to make these conversions easily, and you can simply change them in the animation block to move elements around.

+12
source

One way is to use the automatic layout system and animate the “constant” value of the constraint. In the code example below, I have three buttons in a horizontal line, each of which has a leading edge restriction on the supervisor, and this is what I'm animating. side1, side2 and side3 are IBOutlets for these restrictions, and buttons 1, button2 and button3 are outputs for 3 buttons.

 -(void)animateButton { [self.view removeConstraint:self.side1]; [self.view removeConstraint:self.side2]; [self.view removeConstraint:self.side3]; NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.button1 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:114]; NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.button2 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:213]; NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.button3 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:20]; [self.view addConstraints:@[con1,con2,con3]]; [UIView animateWithDuration:2 animations:^{ [self.view layoutIfNeeded]; }]; } 
+2
source

You can fix this problem with very small changes in your code. Instead of changing the location of the buttons, replace their placement restrictions. You can use cocoapod SBP to replace layout restrictions by importing this category

  #import "UIViewController+SBP.h" 

Then using this method

  - (void)switchViewConst:(UIView*)firstView secondView:(UIView*)secondView durationInSeconds:(double)durationInSeconds 

Here is a video tutorial .

0
source

All Articles