Core Animation for UIView.frame

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 03:30:21

问题


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"];

回答1:


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];



回答2:


[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];


来源:https://stackoverflow.com/questions/6728434/core-animation-for-uiview-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!