我在第一个视图中使用标签创建了一组很酷的动画。
我不想重写此代码,因为我将来可能会改进此动画。
但是,我想在另一个视图/视图控制器上使用带有不同标签的精确动画。它们将具有相同的名称。
我如何做到这一点,有人建议/提供一个例子吗?
这是基于我的代码的动画数组块代码......
http://xibxor.com/2013/03/27/uiview-animation-without-nested-hell/
NSMutableArray* animationBlocks = [NSMutableArray new];
typedef void(^animationBlock)(BOOL);
// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{
animationBlock block = animationBlocks.count ? (animationBlock)[animationBlocks objectAtIndex:0] : nil;
if (block){
[animationBlocks removeObjectAtIndex:0];
return block;
}else{
return ^(BOOL finished){};
}
};
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
[UIView animateWithDuration:1.0 animations:^{
//...animation code...
} completion: getNextAnimation()];
}];
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
[UIView animateWithDuration:1.0 animations:^{
//...animation code...
} completion: getNextAnimation()];
}];
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
[UIView animateWithDuration:1.0 animations:^{
//...animation code...
} completion: getNextAnimation()];
}];
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
NSLog(@"Multi-step Animation Complete!");
}];
// execute the first block in the queue
getNextAnimation()(YES);
以下是我的代码
的示例//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
[UIView animateWithDuration:1.0 animations:^{
//...animation code...
lblLeft.transform = CGAffineTransformMakeScale(1.3,1.3);
lblMiddle.transform = CGAffineTransformMakeScale(1.3,1.3);
lblRight.transform = CGAffineTransformMakeScale(1.3,1.3);
} completion: getNextAnimation()];
}];
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
[UIView animateWithDuration:1.0 animations:^{
//...animation code...
lblLeft.transform = CGAffineTransformIdentity;
lblMiddle.transform = CGAffineTransformIdentity;
lblRight.transform = CGAffineTransformIdentity;
} completion: getNextAnimation()];
}];
我在不同的视图控制器中的不同视图上有这三个标签。
答案 0 :(得分:0)
您是否尝试使用类方法将这些包装在对象中,以便将动画方法保留在对象中但将视图传递给包装器方法?例如:
此方法位于FooAnimationUtilities类中:
+ (void)animateViewWithScale:(UIView *)myView {
[animationBlocks addObject:^(BOOL finished){;
[myView animateWithDuration:1.0 animations:^{
//...animation code...
lblLeft.transform = CGAffineTransformMakeScale(1.3,1.3);
lblMiddle.transform = CGAffineTransformMakeScale(1.3,1.3);
lblRight.transform = CGAffineTransformMakeScale(1.3,1.3);
} completion: getNextAnimation()];
}];
}
然后从你的其他课程中,想要使用这个课程......
在各种视图类中的实现上方的.m中:
#import "FooAnimationUtilities.h"
在需要动画的视图类方法(比如animateMe)中:
-(void)animateMe {
[FooAnimationUtilities animateViewWithScale:self];
}
因此,就传入标签而言,您可以考虑修改要采用的类方法(UILabel *)lblLeft,或者您可以更清楚一点,并传递包含标签对象的数组或字典。你甚至可以通过规模。所以类方法名称应该是这样的:
+ (void)animateView:(UIView *)myView andLabels:(NSArray *)labels withScale:(float)scale
答案 1 :(得分:0)
您可以使用类别。类别用于扩展现有类的功能。以下是类别:Link
的文档如果您的代码不依赖于UIController是您的“自我”,您可以将该代码放在NSObject中并创建它的实例,或者在需要动画时拥有它的静态实例。所以你可以调用[ObjectA animateMyAnimation];