试图掌握Xcode并且似乎在过去几周取得了一些进展。
有没有人知道自定义按钮可以在第二次点击时执行不同的动画集。
所以,我说我有一个自定义按钮和它的Mario,当我点击它时,他从屏幕中间运行并从屏幕的右侧运行,然后从左侧向后跑回到中间屏幕上,他也发出声响。
我使用此代码实现了这一目标:
- (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];
}
如何让他在下一次点击时制作第二套动画?例如,如果我第二次点击他上下跳跃,而不是从左到右跑步?
答案 0 :(得分:1)
使用按钮的选定状态来决定要执行的动画
-(void)buttonClicked:(UIButton*)button
{
if(button.selected)
{
[self doAnimation1];
}
else
{
[self doAnimation2];
}
button.selected = !button.selected;
}
答案 1 :(得分:0)
这可能有点矫枉过正,但这是一种很酷的方式来做你想做的事情:
创建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
}];
那应该这样做。我没有测试过这个,你可能需要一些配置,但这几乎就是这个想法。