【问题标题】:Second touch animation第二次触摸动画
【发布时间】:2013-12-09 18:11:30
【问题描述】:

尝试掌握 Xcode 并在过去几周似乎取得了一些进展。

有谁知道自定义按钮可以在第二次点击时执行一组不同的动画。

假设我有一个自定义按钮及其马里奥,当我点击它时,他从屏幕中间跑出屏幕右侧,然后从左侧跑回中间屏幕上,他也会发出声音。

我使用以下代码实现了这一点:

- (IBAction)marioRunning_clicked:(id)sender
{

    [UIView animateWithDuration:1.50 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        marioRunning.center = CGPointMake(350.5, 456.0);
    } completion:^(BOOL finished) {
        if (finished) {
            [UIView animateWithDuration:0.00 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                marioRunning.center = CGPointMake(-30.5, 456.0);
            } completion:^(BOOL finished) {
                if (finished) {
                    [UIView animateWithDuration:1.50 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                        marioRunning.center = CGPointMake(160.0, 456.0);
                    } completion:^(BOOL finished) {
                        if(finished)  // NSLog ( @"Finished !!!!!" );
                        marioRunning.center = CGPointMake(160.0, 456.0);
                    }];
                }
            }];
        }
    }];

    marioRunning.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"mario-running2"],[UIImage imageNamed:@"mario-running3"],nil];

    marioRunning.imageView.animationDuration = 0.15;
    marioRunning.imageView.animationRepeatCount = 19;

    [marioRunning.imageView startAnimating];
}

我怎样才能让他在下次点击时制作第二组动画?例如,如果我第二次点击他,而不是从左到右运行,他会上下跳跃?

【问题讨论】:

    标签: ios animation uiview uibutton


    【解决方案1】:

    使用按钮的选中状态来决定执行哪个动画

    -(void)buttonClicked:(UIButton*)button
    {
         if(button.selected)
         {
              [self doAnimation1];
         }
         else
         {
              [self doAnimation2];
         }
    
         button.selected = !button.selected;
    }
    

    【讨论】:

    • 这太棒了,一旦我将代码放入其中,它就可以工作了,非常感谢。你知道是否有办法播放第三个动画,或者至少让第一个动画播放两次然后播放第二个动画?
    【解决方案2】:

    这可能有点矫枉过正,但这是一种很酷的方式来做你想做的事:

    创建UIButton 的子类,我们称之为DDDMarioButton

    typedef void (^DDDAnimationBlock)(UIButton *button);
    
    @interface DDDMarioButton : UIButton
    
    - (void)addAnimationBlockToQueue:(DDDAnimationBlock)block;
    
    @end
    

    然后在DDDMarioButton.m:

    @interface DDDMarioButton ()
    
    @property (nonatomic, strong) NSMutableArray *animationQueue;
    
    @end
    
    @implementation DDDMarioButton
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    
    - (void)buttonPressed:(id)button
    {
        DDDAnimationBlock block = self.animationQueue[0];
        block(self);
        [self.animationQueue removeObjectAtIndex:0];
    }
    
    - (void)addAnimationBlockToQueue:(DDDAnimationBlock)block
    {
        if(!self.animationQueue)
        {
            self.animationQueue = [NSMutableArray new];
        }
    
        [self.animationQueue addObject:block];
    }
    
    @end
    

    然后,无论您在哪里创建按钮,都可以一步一步添加:

    DDDMarioButton *button = [[DDDMarioButton alloc] initWithFrame:CGRectZero];
    
    [button addAnimationBlockToQueue:^(UIButton *button) {
       // perform some animation
    }];
    
    [button addAnimationBlockToQueue:^(UIButton *button) {
        // perform another animation
    }];
    

    应该这样做。我还没有测试过这个,你可能需要一些配置,但这差不多就是这个想法。

    【讨论】:

    • 嘿,moby,并不是说我对您对这个问题的回答不满意,我可能会恢复使用此选项来执行多个动画,但我希望得到一个更简短的答案,以便我在学习时能更好地理解所以另一个答案更多的是我的街道。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多