【问题标题】:IOS/Objective-c: code reduction within animationsIOS/Objective-c:动画中的代码缩减
【发布时间】:2017-02-21 20:49:21
【问题描述】:

我有一个简单的动画,它一次一个地淡入多个标签。但是不知道是不是可以用这个逻辑来减少代码呢?

[UIView animateWithDuration:0.50f animations:^{
    [_latLabel setAlpha:0.9f];
    [_firstLat setAlpha:0.9f];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.50f animations:^{
        [_lonLabel setAlpha:0.9f];
        [_firstLon setAlpha:0.9f];
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:0.50f animations:^{
            [_speedLabel setAlpha:0.9f];
            [_firstSpeed setAlpha:0.9f];
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:0.50f animations:^{
                [_realNorthLabel setAlpha:0.9f];
                [_firstReal setAlpha:0.9f];
            }completion:^(BOOL finished) {
                [UIView animateWithDuration:0.50f animations:^{
                    [_magneticNorthLabel setAlpha:0.9f];
                    [_firstMagnetic setAlpha:0.9f];
                }];
            }];
        }];

    }];

}];

【问题讨论】:

  • 使用延迟启动的函数
  • 嗯,我正在尝试你的建议,但我得到了很多错误......
  • 所以你构建了一个带有延迟启动参数的函数,但你得到了一个错误,你是如何做到的
  • 你的意思是用这个? [UIView animateWithDuration: 延迟: 选项: 动画: 完成:]
  • 我这样做但结果不一样:[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{[_latLabel setAlpha:0.9f]; [_firstLon setAlpha:0.9f]; [_speedLabel setAlpha:0.9f]; [_firstSpeed setAlpha:0.9f]; [_realNorthLabel setAlpha:0.9f]; [_firstReal setAlpha:0.9f]; [_magneticNorthLabel setAlpha:0.9f]; [_firstMagnetic setAlpha:0.9f]; } 完成:^(BOOL 完成){ nil; }];

标签: ios objective-c animation


【解决方案1】:

考虑到在执行动画块的任何单个部分时需要对 2 个标签对象执行操作,创建一个 dictionary 结构,其中 section indexes 定义部分动画的时间顺序顺序执行 & 每个部分索引包含标签对象的array
在此命名为dictionaryOfSectionedLabels 的结构上的简单迭代循环可以帮助实现所需的替代动画实现。

NSDictionary<NSNumber *, NSArray *> *dictionaryOfSectionedLabels = @{ @0: @[_latLabel, _firstLat],
                                                                      @1: @[_lonLabel, _firstLon],
                                                                      @2: @[_speedLabel, _firstSpeed],
                                                                      @3: @[_realNorthLabel, _firstReal],
                                                                      @4: @[_magneticNorthLabel, _firstMagnetic]
                                                                    };

for (NSUInteger i = 0; i < dictionaryOfSectionedLabels.allKeys.count; i++) {

    [UIView animateWithDuration:0.5 delay:0.5*i options:UIViewAnimationOptionLayoutSubviews animations:^{

        UILabel *firstLabel = [dictionaryOfSectionedLabels[@(i)] firstObject];
        UILabel *secondLabel = [dictionaryOfSectionedLabels[@(i)] lastObject];

        firstLabel.alpha = 0.9f;
        secondLabel.alpha = 0.9f;

    } completion:nil];
}

【讨论】:

  • 很高兴帮助@FuManchu :)
【解决方案2】:

为您的标签创建一个 NSDictionary 可能是一个繁琐且令人困惑的代码,以便将来阅读。我建议你只创建一个方法。

-(void)customFadeForLabel:(UILabel *)theLabel withDelay:(float)delayAmount {

    [UIView animateWithDuration:0.50f 
                          delay:delayAmount 
                        options:UIViewAnimationOptionCurveEaseInOut 
                     animations:^{
                         [theLabel setAlpha:0.9f];
                     }completion:nil];

}

然后根据需要延迟调用它

[self customFadeForLabel:_latLabel withDelay:0.00f];
[self customFadeForLabel:_firstLat withDelay:0.00f];

[self customFadeForLabel:_lonLabel withDelay:0.50f];
[self customFadeForLabel:_firstLon withDelay:0.50f];

[self customFadeForLabel:_speedLabel withDelay:1.00f];
[self customFadeForLabel:_firstSpeed withDelay:1.00f];

[self customFadeForLabel:_realNorthLabel withDelay:1.50f];
[self customFadeForLabel:_firstReal withDelay:1.50f];

[self customFadeForLabel:_magneticNorthLabel withDelay:2.00f];
[self customFadeForLabel:_firstMagnetic withDelay:2.00f];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多