当iOS编程,我经常发现自己面临着以下情况:iOS方法调用动画,完成时执行操作?
- (void)someMethod
{
[self performSomeAnimation];
//below is an action I want to perform, but I want to perform it AFTER the animation
[self someAction];
}
- (void)performSomeAnimation
{
[UIView animateWithDuration:.5 animations:^
{
//some animation here
}];
}
面对这种情况,我通常最终只是复制/粘贴我的动画代码,这样我就可以使用完成块处理,像这样:
- (void)someMethod
{
[self performSomeAnimation];
//copy pasted animation... bleh
[UIView animateWithDuration:.5 animations:^
{
//same animation here... code duplication, bad.
}
completion^(BOOL finished)
{
[self someAction];
}];
}
- (void)performSomeAnimation
{
[UIView animateWithDuration:.5 animations:^
{
//some animation here
}];
}
解决此问题的正确方法是什么?我是否应该将一块代码传递给我的-(void)performSomeAction
方法,如下所示,并在完成动画时执行该块?
- (void)someMethod
{
block_t animationCompletionBlock^{
[self someAction];
};
[self performSomeAnimation:animationCompletionBlock];
}
- (void)performSomeAnimation:(block_t)animationCompletionBlock
{
[UIView animateWithDuration:.5 animations:^
{
//some animation here
}
completion^(BOOL finished)
{
animationCompletionBlock();
}];
}
难道是解决这个问题的正确方法吗?我想我一直在避免它,因为我不熟悉块的使用(甚至不确定是否正确地声明了该块),并且它似乎是一个简单问题的复杂解决方案。
你说的动画总是相同的,但完成变化?如果是这样,你的解决方案对我来说看起来很不错。 –
我认为我的语法不正确。我只是看着它,我应该像这样声明我的块:void(^ myBlock)(void),但是我的方法声明看起来像什么? - (void)performSomeAnimation :(无效)(^ myBlock)(void)似乎没有工作(我敢肯定我正在屠杀语法哈哈) – MikeS