在应用加载时,将UIButton从屏幕外的位置设置为动画

时间:2012-05-03 21:09:45

标签: iphone objective-c xcode ipad animation

我在iPad应用程序中遇到一些动画问题。当应用程序启动时,我在屏幕上有四个UIButtons。

我基本上希望加载应用程序,然后将四个按钮动画显示为从屏幕顶部到位的一个视图。

我有以下哪种动画,但我正在努力。

-(void)viewWillAppear:(BOOL)animated
{
    CGPoint newLeftCenter = CGPointMake( 15.0f + myCocoButton.frame.size.width / 2.0f, myCocoButton.center.y);
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:4.0f];
    myCocoButton.center = newLeftCenter;
    [UIView commitAnimations];

}

我当前的代码确实为按钮设置了动画,但没有按照我想要的方式设置动画。我无法理解如何将其准确放置在我想要的位置。

3 个答案:

答案 0 :(得分:2)

在故事板中,将按钮布置在最终位置。

viewWillAppear:中,保存每个按钮的位置并将按钮移出屏幕:

@implementation MyViewController {
    CGPoint _button0TrueCenter;
    CGPoint _button1TrueCenter;
    CGPoint _button2TrueCenter;
    CGPoint _button3TrueCenter;
}

static void moveButtonAndSaveCenter(UIButton *button, CGPoint offscreenCenter, CGPoint *trueCenter) {
    *trueCenter = button.center;
    button.center = offscreenCenter;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (animated) {
        moveButtonAndSaveCenter(self.button0, CGPointMake(-100, 100), &_button0TrueCenter);
        moveButtonAndSaveCenter(self.button1, CGPointMake(420, 100), &_button1TrueCenter);
        moveButtonAndSaveCenter(self.button2, CGPointMake(-100, 200), &_button2TrueCenter);
        moveButtonAndSaveCenter(self.button3, CGPointMake(420, 200), &_button3TrueCenter);
    }
}

然后在viewDidAppear:中,将它们设置回原始位置:

static void animateButton(UIButton *button, CGPoint newCenter, NSTimeInterval delay) {
    [UIView animateWithDuration:.25 delay:delay options:0 animations:^{
        button.center = newCenter;
    } completion:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (animated) {
        animateButton(self.button0, _button0TrueCenter, 0);
        animateButton(self.button1, _button1TrueCenter, 0.2);
        animateButton(self.button2, _button2TrueCenter, 0.4);
        animateButton(self.button3, _button3TrueCenter, 0.6);
    }
}

答案 1 :(得分:2)

假设你有一个数组中的按钮,你可以做这样的事情(你的项目应该在笔尖的正确结束位置)

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    CGAffineTransform transform = CGAffineTransformMakeTranslation(0.f, -200.f); 

    [self.buttons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL *stop) {

       button.transform = transform; // This translates the view's off the top of the screen

       [UIView animateWithDuration:0.25f
                             delay:0.25f * idx
                           options:UIViewAnimationCurveEaseOut            // No point doing easeIn as the objects are offscreen anyway
                        animations:^{
                            button.transform = CGAffineTransformIdentity;
                      } completion:nil];

       }];
}

每次都会动画。您可能需要添加一个ivar来阻止这种情况发生,并在运行一次之后将其设置为YES。我刚刚搞砸的时候发现的问题是,当第一次加载时viewDidAppear: animated调用NO时,我无法使用该BOOL作为检查

答案 2 :(得分:0)

我写了一篇关于这个主题的博文:http://www.roostersoftstudios.com/2011/07/01/iphone-dev-performing-sequential-uiview-animations/

我在那里有一个示例项目。

您可以将动画链接到一个完成时动画调用下一个动画的功能。您能否更具体地了解您所遇到的确切问题?