1
我有一个用户在某处拖动UIView的场景;我想将它返回到它开始的位置,并且我希望它沿着相同的轨道返回,并且速度与用户拖动的速度相同。我想出了一个递归解决方案,看起来像这样:UIView动画以“倒带”平移手势:递归?
@property (...) NSMutableArray *dragTrack; // recorded during pan gesture
- (void)goHome
{
[self backtrack: [self.dragTrack count]
withDelay: 1.0/[self.dragTrack count]];
// interim version of delay; will implement accurate timing later
}
- (void)backtrack: (int)idx withDelay: (double)delay
{
if (idx > 0){
[UIView animateWithDuration:0 delay:delay options: 0
animations: ^{
self.center = [[self.dragTrack objectAtIndex:idx - 1] CGPointValue];
}
completion:^(BOOL finished){
[self backtrack: idx - 1 withDelay: delay];
}];
} else {
// do cleanup stuff
}
}
这工作,和递归深度似乎并不成为一个问题 - 我拖轨道通常只有几百几个百分点长。 (我假设递归调用回调获得tail调用优化的机会相当渺茫?)。但我仍然想知道:这是一个合理的/正常的/安全的解决方案,我试图达到什么目的?还是有更简单的方法将时间戳和状态集合传递给动画并说“播放这些”?