第二个触摸animation
试图抓住Xcode,似乎在过去几周取得了一些进展。
有没有人知道一种方法,一个自定义button可以做一个不同的一组animation的第二次点击。
所以说,我有一个自定义button和它的马里奥,当我点击它,他从屏幕中间运行,并从屏幕的右侧,然后从屏幕左侧跑回到中间,他也制造噪音。
我已经实现了这个使用这个代码:
- (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]; }
我怎么能让他做下一个点击animation的第二套? 例如,不是从左到右跑,如果我第二次点击他,他会跳起来和下去?
使用button的选定状态决定要执行哪个animation
-(void)buttonClicked:(UIButton*)button { if(button.selected) { [self doAnimation1]; } else { [self doAnimation2]; } button.selected = !button.selected; }
这可能是矫枉过正,但这是一个很酷的方式来做你想做的事情:
做一个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
然后无论你在哪里创build你的button,你逐一添加每一步:
DDDMarioButton *button = [[DDDMarioButton alloc] initWithFrame:CGRectZero]; [button addAnimationBlockToQueue:^(UIButton *button) { // perform some animation }]; [button addAnimationBlockToQueue:^(UIButton *button) { // perform another animation }];
这应该做到这一点。 我没有testing过这个,你可能需要一些configuration,但是这非常的想法。