Sententia
Sententia

Reputation: 625

UIView not moving when animating

I'm trying to move a simple uiview 30 points up. However, no matter what I do I can't move it.

I'm developing in iOS 7.1

Now, I finally found out that I have auto layout on, however I don't have any constrains for this view. It was a simple drag from the object library and place in a view controller. I've tried various things to get it to move. I can animate properties, such as alpha and what have you but I can't for the life of me move it.

Below is what I've tried:

[UIView animateWithDuration:1.0f animations:^{
    CGREct oldFrame = self.viewWantingToMove.frame;
    self.viewWantingToMove.frame = CGRectMake(self.viewWantingToMove.frame.origin.x, self.viewWantingToMove.origin.y, self.viewWantingToMove.size.width, self.viewWantingToMove.width.height);
});

This doesn't work

[UIView animateWithDuration:1.0f animation:^{
    self.viewWantingToMove.center = CGPoint(self.viewWantingToMove.frame.origin.x, self.viewWantingToMove.frame.origin.y-30
}];

Again nadda...

After I figured it might have something with auto layout, mind you I can't turn it off because I have to use it, I then tried this.

[self.viewWantingToMove layoutIfNeeded];
[UIView animateWithDuration:1.0f animation:^{
    self.viewWantingToMove.center = CGPoint(self.viewWantingToMove.frame.origin.x, self.viewWantingToMove.frame.origin.y-30
    [self.viewWantingToMove layoutIfNeeded];
}];

This gives me animation...... BUT backwards. it starts off 30 points up and then moves back to it's original position. Can anyone help me with this? Its getting to one of those moments where you feel like biting the monitor.

Upvotes: 0

Views: 959

Answers (1)

rdelmar
rdelmar

Reputation: 104082

When you're using auto layout, you shouldn't be setting any frames. To move or resize views, you should modify the constraints instead. The fact that you didn't add any constraints does mean they're not there. The system adds default constraints for you. You should set the constraints you want, so you know what they are (try ones to the top of the view, to the left side, and ones for height and width). Make an IBOutlet to the constraint to the top of the view (lets call it topCon), then you can modify that one in code to animate it,

[UIView animateWithDuration:1.0f animation:^{
    self.topCon.constant -= 30;
    [self.view layoutIfNeeded];
}];

Upvotes: 3

Related Questions