问题描述:

I need to create animation with the moving label in Objective-C for iOS. The animation should appear as the user taps the certain button. Also I need to disable any UI interractions while the animation is in progress. This is what I do to achieve this behavior:

1.Create UILabel object in the viewDidLoad method of the view controller, set all required fields to the appropriate values and make it hidden:

plusScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];

plusScoreLabel.text = @"+10";

plusScoreLabel.textColor = [UIColor colorWithRed:12.0/255.0 green:144.0/255.0 blue:51.0/255.0 alpha:1.0];

plusScoreLabel.hidden = YES;

plusScoreLabel.textAlignment = NSTextAlignmentCenter;

[self.view addSubview:plusScoreLabel];

uiInteractionsDisabled = NO;

2.Then I call animateWithDuration:animation:completion method on the button pressed action like this:

uiInteractionsDisabled = YES;

[plusScoreLabel setCenter:CGPointMake(buttonToChoose.center.x, buttonToChoose.center.y)];

plusScoreLabel.hidden = NO;

[UIView animateWithDuration:0.5f animations:^{

plusScoreLabel.center = CGPointMake(buttonToChoose.center.x, buttonToChoose.center.y - 50.0f);

} completion:^(BOOL finished) {

plusScoreLabel.hidden = YES;

}];

3.Add the following check to the each IBAction method:

if (uiInteractionsDisabled == YES) {

return;

}

Is there something wrong with this method? Maybe there is a better way to achieve this behavior?

Thanks in advance.

网友答案:

There is nothing wrong with your solution. If you want something special, you can subclass the CABasicAnimation and override two methods in it:

/* Called when the animation begins its active duration. */

- (void)animationDidStart:(CAAnimation *)anim;

/* Called when the animation either completes its active duration or
 * is removed from the object it is attached to (i.e. the layer). 'flag'
 * is true if the animation reached the end of its active duration
 * without being removed. */

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;

in addition you can add two block properties to the subclass:

@property (nonatomic, copy) void  (^startBlock)(void) ;
@property (nonatomic, copy) void  (^finishBlock)(void) ;

and execute the blocks in overriden methods:

- (void)animationDidStart:(CAAnimation *)anim{
    if (_startBlock){
        _startBlock();
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if (_finishBlock){
        _finishBlock();
    }
}

after that, you can use your custom animation object like that:

    MyCustomAnimation* anim = [MyCustomAnimation animationWithKeyPath:@"position"];
    anim.startBlock = ^{uiInteractionsDisabled = YES;};
    anim.finishBlock = ^{uiInteractionsDisabled = NO;};
    [anim setFromValue:[NSValue valueWithCGPoint:CGPointMake(buttonToChoose.center.x, buttonToChoose.center.y)]];
    [anim setToValue:[NSValue valueWithCGPoint:CGPointMake(buttonToChoose.center.x, buttonToChoose.center.y - 50.0f)]];
    [anim setDuration:0.5];
    [plusScoreLabel.layer addAnimation:anim forKey:@"translationAnimation"];

I hope you've learned something new : )

相关阅读:
Top