IOS/Objective-c:动画中的代码缩减
我有一个简单的动画,在几个标签中淡出,一次一个。但我想知道是否有可能用这种逻辑来减少代码IOS/Objective-c:动画中的代码缩减,ios,objective-c,animation,Ios,Objective C,Animation,我有一个简单的动画,在几个标签中淡出,一次一个。但我想知道是否有可能用这种逻辑来减少代码 [UIView animateWithDuration:0.50f animations:^{ [_latLabel setAlpha:0.9f]; [_firstLat setAlpha:0.9f]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.50f animations:^{ [_l
[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];
}];
}];
}];
}];
}];
考虑到在执行动画块的任何单个部分时需要对2个标签对象执行操作,创建一个
字典
结构,其中节索引
定义要执行的节动画的时间顺序&每个节索引包含标签对象的数组。
在这个结构上进行一个简单的迭代循环,在这里命名为dictionaryofsectionlabels
,可以帮助实现所需的备用动画实现
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];
}
NSDictionary*dictionaryOfSectionedLabels=@{@0:@[\u latLabel,\u firstLat],
@1:@[_lonLabel,_firstLon],
@2:@[\u speedLabel,\u firstSpeed],
@3:@[\u realNorthLabel,\u firstReal],
@4:@[\u磁性标签,\u第一磁性标签]
};
对于(i=0;i
考虑到在执行动画块的任何单个部分时需要对2个标签对象执行操作,创建一个字典
结构,其中节索引
定义要执行的节动画的时间顺序&每个节索引包含标签对象的数组。
在这个结构上进行一个简单的迭代循环,在这里命名为dictionaryofsectionlabels
,可以帮助实现所需的备用动画实现
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];
}
NSDictionary*dictionaryOfSectionedLabels=@{@0:@[\u latLabel,\u firstLat],
@1:@[_lonLabel,_firstLon],
@2:@[\u speedLabel,\u firstSpeed],
@3:@[\u realNorthLabel,\u firstReal],
@4:@[\u磁性标签,\u第一磁性标签]
};
对于(i=0;i
创建标签的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];
为标签创建一个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];
使用带有延迟开始的函数我会尝试你的建议,但是我得到了很多错误…所以你用延迟开始参数构建了一个函数,你得到了一个错误,你是怎么做到的?你的意思是使用这个?[UIView animateWithDuration:delay:options:animations:completion:]我是这样做的,但结果不同:[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut动画:^{[u latLabel setAlpha:0.9f];[u firstLon setAlpha:0.9f];[u speedLabel setAlpha:0.9f];[U firstSpeed setAlpha:0.9f];[U realNorthLabel setAlpha:0.9f];[U firstReal setAlpha:0.9f];[U magnetics setAlpha:0.9f];[U firstMagnetic setAlpha:0.9f];}完成:^(布尔完成){nil;}];使用带有延迟开始的函数我会尝试你的建议,但我得到了很多错误…因此,你使用延迟开始参数构建了一个函数,并且得到了一个错误。你是如何使用它的?你的意思是什么?[UIView animateWithDuration:delay:options:animations:completion:]我这样做,但结果不一样:[UIView animateWithDuration:1.0延迟:1.0选项:UIViewAnimationOptionCurveEaseOut动画:^{[[u latLabel setAlpha:0.9f];[u firstLon setAlpha:0.9f];[u firstSpeed setAlpha:0.9f];[u Real NorthLabel setAlpha:0.9f];[u firstReal setAlpha:0.9f];[_magnetic-northlabel-setAlpha:0.9f];[_-firstMagnetic-setAlpha:0.9f];}完成:^(BOOL-finished){nil;};很高兴帮助@FuManchu:)很高兴帮助@FuManchu:)