Note that there are some explanatory texts on larger screens.

plurals
  1. POAnimating an UIImageView (View) from Point A to Point B with constraints programmatically
    primarykey
    data
    text
    <p>I have an image view that I have lined up on the center right of my screen. All is fine when adding the image view by itself with constraints. I need to animate the image view from that original position(Point A) to the bottom portion(Point B) of the screen; the problem is when I try to animate from Point A to Point B, the image view starts off in the upper left hand corner of the screen, like it was simply added with no constraints (as opposed to where I want it starting off in the center) .</p> <p>Here is the code and some comments:</p> <pre><code>#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0) //defined above - (void)viewDidLoad { UIImage *cardFaceDownImage = [UIImage imageNamed:@"b-top@2x.png"]; UIImageView *dealCardDown = [[UIImageView alloc] initWithImage:cardFaceDownImage]; dealCardDown.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:1]; NSLayoutConstraint *dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:130]; [self.view addConstraint:dealCardConstraintY]; [self.view addConstraint:dealCardConstraintX]; [self.view addSubview:dealCardDown]; //IF I comment out the below code the UIImageView lines up where I want it (Point A) //IF I don't coment it out, the imageview starts in the upper left hand corner of the screen //where it normally would as if just adding an image view programatically without //constraints would, but it does end up where I want it to (Point B) [self.view removeConstraint:dealCardConstraintX]; [self.view removeConstraint:dealCardConstraintY]; dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:1]; dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:130]; [self.view addConstraint:dealCardConstraintY]; [self.view addConstraint:dealCardConstraintX]; [self.view setNeedsUpdateConstraints]; [UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationCurveEaseIn animations:^(void) { dealCardDown.transform = CGAffineTransformRotate(dealCardDown.transform, DEGREES_TO_RADIANS(90)); [self.view layoutIfNeeded]; } completion:^(BOOL finished) { } ]; } </code></pre> <p>..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload