Ios 同一对象上的2个动画,仅最后显示。如何显示没有嵌套完成块的第一个?
我正在同一个UIImageView上使用块制作两个动画。动画不是背靠背的,但几乎是;两者之间有某种逻辑Ios 同一对象上的2个动画,仅最后显示。如何显示没有嵌套完成块的第一个?,ios,animation,block,cakeyframeanimation,completion-block,Ios,Animation,Block,Cakeyframeanimation,Completion Block,我正在同一个UIImageView上使用块制作两个动画。动画不是背靠背的,但几乎是;两者之间有某种逻辑 将UIImageView从视图上的一个位置设置为动画 执行确定是否允许映像停留在那里的逻辑 如果不允许,请撤消动画(UIImageView弹回到原始位置) 如果我如上所述实现,则只显示第二个动画(据我所知,这是正常行为)。如果我将逻辑和第二个动画块嵌套在第一个动画块的完成块中,我会看到两个动画,但是有相当多的代码要插入到完成块中,这看起来很难看也不合适 在非嵌套配置中,为什么iOS要缩短前
- 将UIImageView从视图上的一个位置设置为动画
- 执行确定是否允许映像停留在那里的逻辑
- 如果不允许,请撤消动画(UIImageView弹回到原始位置)
// first animation
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{movingColor.center = newCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(@"forward animation done");}];
if (/* ...color is allowed at that location */) {
// do some stuff
} else {
// undo the animation
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{movingColor.center = origCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(@"back animation done");}];
}
正确的方法是,正如你所说的,在第一个的基础上设置第二个,这是正确的方法 您还可以在另一个的开始添加延迟,这可能有效,也可能无效。这取决于许多变量 第二个动画的延迟为.5(第一个动画完成的时间)
正确的方法是,正如你所说的,在第一个的基础上设置第二个,这是正确的方法 您还可以在另一个的开始添加延迟,这可能有效,也可能无效。这取决于许多变量 第二个动画的延迟为.5(第一个动画完成的时间)
animateWithDuration
异步执行,并立即返回。也就是说,在不等待动画完成的情况下执行下一行代码。如果希望在动画完成后执行代码,可以将代码放入completion
块,或者接受立即启动第二个动画(从而取消第一个动画)
如果您希望通过启动动画而不是完成动画来向用户指示有问题,则可以延迟执行第二个动画。如果第二个动画的延迟时间小于第一个动画的持续时间,请记住也设置UIViewAnimationOptionBeginFromCurrentState
选项:
[UIView animateWithDuration:0.5
delay:0.25 //or some number
options: (UIViewAnimationCurveLinear | UIViewAnimationOptionBeginFromCurrentState)
animations:^{movingColor.center = origCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(@"back animation done");}];
animateWithDuration
异步执行,并立即返回。也就是说,在不等待动画完成的情况下执行下一行代码。如果希望在动画完成后执行代码,可以将代码放入completion
块,或者接受立即启动第二个动画(从而取消第一个动画)
如果您希望通过启动动画而不是完成动画来向用户指示有问题,则可以延迟执行第二个动画。如果第二个动画的延迟时间小于第一个动画的持续时间,请记住也设置UIViewAnimationOptionBeginFromCurrentState
选项:
[UIView animateWithDuration:0.5
delay:0.25 //or some number
options: (UIViewAnimationCurveLinear | UIViewAnimationOptionBeginFromCurrentState)
animations:^{movingColor.center = origCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(@"back animation done");}];
第二个动画应该在第一个动画的完成块内有条件地完成。将其放入函数将使其更具可读性
- (void)moveColor:(UIView *)view to:(CGPoint)center delay:(NSTimeInterval)delay completion:(void (^)(BOOL finished))complete {
[UIView animateWithDuration:0.5 delay:delay options:UIViewAnimationCurveLinear animations:^{
view.center = center;
view.transform = scaleTransform; // probably don't need this
} completion:completion];
}
这样,您的逻辑可以与动画代码分离
- (void)someMethod {
[self moveColor:movingColor to:newCenter delay:0.0 completion:^(BOOL finished) {
if (/*it can stay*/) {
// stuff
} else {
[self moveColor:movingColor to:origCenter delay:2.0 completion:^(BOOL finished) {}];
}
}];
}
第二个动画应该在第一个动画的完成块内有条件地完成。将其放入函数将使其更具可读性
- (void)moveColor:(UIView *)view to:(CGPoint)center delay:(NSTimeInterval)delay completion:(void (^)(BOOL finished))complete {
[UIView animateWithDuration:0.5 delay:delay options:UIViewAnimationCurveLinear animations:^{
view.center = center;
view.transform = scaleTransform; // probably don't need this
} completion:completion];
}
这样,您的逻辑可以与动画代码分离
- (void)someMethod {
[self moveColor:movingColor to:newCenter delay:0.0 completion:^(BOOL finished) {
if (/*it can stay*/) {
// stuff
} else {
[self moveColor:movingColor to:origCenter delay:2.0 completion:^(BOOL finished) {}];
}
}];
}
关于“丑陋和不合适”的笑话很有趣。我也经常这样做:根据代码在页面上的外观判断代码。块语法(尤其是嵌套语法)有点笨拙,但在这里它是强大而合适的。对计时大惊小怪是不精确的(而且不太可读……想象一下稍后再来,试图通过读取这些时间常数来弄清楚发生了什么)。完成块正是完成时要做的事情。也许有点难看,但正确。看看我的答案,想办法把它做好,变得更漂亮一点。有趣的是“丑陋和不合适”。我也经常这样做:根据代码在页面上的外观判断代码。块语法(尤其是嵌套语法)有点笨拙,但在这里它是强大而合适的。对计时大惊小怪是不精确的(而且不太可读……想象一下稍后再来,试图通过读取这些时间常数来弄清楚发生了什么)。完成块正是完成时要做的事情。也许有点难看,但正确。看看我的答案,想办法把它做好,再漂亮一点。