【问题标题】:How to correctly create animation with the moving label and disable any UI interractions while it is in progress如何使用移动标签正确创建动画并在动画进行时禁用任何 UI 交互
【发布时间】:2014-07-18 19:52:26
【问题描述】:

我需要在 iOS 的 Objective-C 中创建带有移动标签的动画。当用户点击某个按钮时,动画应该会出现。我还需要在动画进行时禁用任何 UI 交互。这就是我为实现这种行为所做的:

1.在视图控制器的viewDidLoad方法中创建UILabel对象,将所有需要的字段设置为合适的值并隐藏:

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.然后我在按钮按下的动作上调用 animateWithDuration:animation:completion 方法,如下所示:

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.在每个 IBAction 方法中添加以下检查:

if (uiInteractionsDisabled == YES) {
    return;
}

这个方法有问题吗?也许有更好的方法来实现这种行为?

提前致谢。

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:

    您的解决方案没有任何问题。 如果你想要一些特别的东西,你可以继承 CABasicAnimation 并覆盖其中的两个方法:

    /* 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;
    

    此外,您还可以向子类添加两个块属性:

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

    并在覆盖的方法中执行块:

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

    之后,您可以像这样使用自定义动画对象:

        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"];
    

    我希望你学到了一些新东西:)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2010-11-25
    相关资源
    最近更新 更多