Second touch animation

后端 未结 2 1392
情歌与酒
情歌与酒 2021-01-28 05:36

Trying to get to grips with Xcode and seem to be making some progress over the last few weeks.

Does anyone know a way that a custom button can do a different set of anim

相关标签:
2条回答
  • 2021-01-28 06:22

    Use the selected state of the button to decide which animation to do

    -(void)buttonClicked:(UIButton*)button
    {
         if(button.selected)
         {
              [self doAnimation1];
         }
         else
         {
              [self doAnimation2];
         }
    
         button.selected = !button.selected;
    }
    
    0 讨论(0)
  • 2021-01-28 06:26

    This might be overkill, but here's a cool way to do what you want:

    Make a subclass of UIButton, and let's call it DDDMarioButton:

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

    Then in 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
    

    and then wherever you create your buttons, you add each step one by one:

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

    And that should do it. I haven't tested this, you'll probably need some configuration, but that's pretty much the idea.

    0 讨论(0)
提交回复
热议问题