如何制作功能正常的动画按钮?

How can I make a functioning animated button?

我在这样的场景中设置了一个按钮:

self.playButton = [SKSpriteNode spriteWithImageNamed:@"playbutton.png"];
self.playButton.size = CGSizeMake(100,100);
self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), 100);
[self addChild:self.playButton];

然后在touchesBegan:

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

if([self.playButton containsPoint:location]){
    self.playButton.texture = [SKTexture textureWithImageNamed:@"playbuttonpressed.png"];
    [self.playButton runAction: [self touchButtonAction]];
}

然后在touchesEnded:

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

if([self.playButton containsPoint:location]){
    self.playButton.texture = [SKTexture textureWithImageNamed:@"playbutton.png"];
    [self.view presentScene:self.nextScene transition:self.sceneTransition];

}

然后在touchButtonAction:

SKAction *toSmall = [SKAction scaleBy:0.8 duration:0.1];
return toSmall;

问题是: 当我点击按钮时,touchesEnded 在操作完成之前被调用,这导致动画不完整,看起来不太好(看起来很慢) ).如何制作一个在过渡前完成动画的按钮?

有几种方法可以完成您想要的。其中之一是使用积木。

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

if([self.playButton containsPoint:location]){

    SKAction *block0 = [SKAction runBlock:^{
        self.playButton.texture = [SKTexture textureWithImageNamed:@"playbuttonpressed.png"];
        [self.playButton runAction: [self touchButtonAction]];
    }];

    SKAction *wait0 = [SKAction waitForDuration:1.0]; // <- however long it takes for the animation to complete

    SKAction *block1 = [SKAction runBlock:^{
        self.playButton.texture = [SKTexture textureWithImageNamed:@"playbutton.png"];
        [self.view presentScene:self.nextScene transaction:self.sceneTransaction];
    }];

    [self runAction:[SKAction sequence:@[block0, wait0, block1]]];
}

有关使用块的更多信息,您可以阅读 Apple Blocks Programming Guide