Crystal
Crystal

Reputation: 29448

Core Animation for UIView.frame

I'm trying to do a simple animation of moving the frame of two views. Basically hiding the Ad until it is loaded, and then move the frame up from the bottom, along with the view that starts at the bottom, and then will move up also when the Ad pushes it up. The start and end positions are correct, but I don't see it being animated. Is this correct? Thanks.

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
    animation.duration = 1.0;

    CGRect adFrame = CGRectMake(self.adBanner.frame.origin.x, self.adBanner.frame.origin.y - self.adBanner.frame.size.height, self.adBanner.frame.size.width, self.adBanner.frame.size.height);
    self.adBanner.frame = adFrame;
    [self.adBanner.layer addAnimation:animation forKey:@"frame"];

    CGRect buttonViewFrame = CGRectMake(self.ButtonView.frame.origin.x, self.adBanner.frame.origin.y - self.adBanner.frame.size.height, self.ButtonView.frame.size.width, self.ButtonView.frame.size.height);
    self.ButtonView.frame = buttonViewFrame;
    [self.ButtonView.layer addAnimation:animation forKey:@"frame"];

Upvotes: 2

Views: 7504

Answers (2)

Satvik
Satvik

Reputation: 11

[UIView beginAnimations : @"Display notif" context:nil];

[UIView setAnimationDuration:1];

[UIView setAnimationBeginsFromCurrentState:FALSE];

CGRect frame = mainView.frame;

frame.size.height -= 40;

frame.origin.y += 40;

mainView.frame = frame;

[UIView commitAnimations];

Upvotes: 1

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

For something as simple as this, you don’t really need to use Core Animation directly—UIView’s built-in animation system should suffice.

[UIView animateWithDuration:1.0 animations:^{
    self.adBanner.frame = adFrame;
    self.ButtonView.frame = buttonViewFrame;
}];

or, if you’re targeting pre-4.0 iOS,

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
    self.adBanner.frame = adFrame;
    self.ButtonView.frame = buttonViewFrame;
[UIView commitAnimations];

Upvotes: 12

Related Questions