UIView.frame的核心动画

时间:2011-07-18 03:40:33

标签: iphone core-animation

我正在尝试移动两个视图的帧的简单动画。基本上隐藏广告直到加载,然后从底部向上移动框架,以及从底部开始的视图,然后在广告向上推动时向上移动。开始和结束位置是正确的,但我不认为它是动画的。它是否正确?感谢。

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

2 个答案:

答案 0 :(得分:12)

对于像这样简单的事情,你并不需要直接使用Core Animation - UIView的内置动画系统就足够了。

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

或者,如果您的目标是4.0之前的iOS,

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

答案 1 :(得分:1)

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