我正在开发一个应用程序,其中两个视图在屏幕上的某处相遇。当他们相遇时,碰撞检测器会触发这种方法。它应该识别适当的基础,然后将两个视图都发送给它。它确实做到了,但它会立即发生,而不是在 4 秒内发生。我错过了什么?RADIUS 在此代码上方定义。它与箭头不是 UIView 有什么关系吗?spriteView 类是 UIView 的子类。
-(void)sendToBase:(spriteView *)arrow
{
int teamNumber = arrow.teamNumber;
// Find the location of the base.
for (UIView *scaledView in self.view.subviews) {
if (scaledView.tag == 100) {
for (UIView *base in scaledView.subviews) {
if (base.tag >= 1000) {
if (teamNumber + 1000 == base.tag) {
// We found the right base.
CGPoint newCenter;
newCenter.x = base.center.x + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
newCenter.y = base.center.y + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
[UIView animateWithDuration:4.0 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
[arrow setCenter:newCenter];
} completion:^(BOOL finished) {
// After walking back to base, remove and create new objects
[arrow removeFromSuperview];
[self addArrow:scaledView toTeam:teamNumber];
}];
}
}
}
}
}
}
如果类类型不匹配是问题所在,我因此修改了代码,结果相同。
-(void)sendToBase:(spriteView *)arrow
{
UIView *uiSpriteView = (UIView *)arrow;
int teamNumber = arrow.teamNumber;
// Find the location of the base.
for (UIView *scaledView in self.view.subviews) {
if (scaledView.tag == 100) {
for (UIView *base in scaledView.subviews) {
if (base.tag >= 1000) {
if (teamNumber + 1000 == base.tag) {
// We found the right base.
CGPoint newCenter;
newCenter.x = base.center.x + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
newCenter.y = base.center.y + arc4random() % (int) floor(RADIUS) - RADIUS/2.0;
[UIView animateWithDuration:4.0 delay:0.0 options:UIViewAnimationCurveLinear animations:^{
[uiSpriteView setCenter:newCenter];
} completion:^(BOOL finished) {
// After walking back to base, remove and create new objects
[arrow removeFromSuperview];
[self addArrow:scaledView toTeam:teamNumber];
}];
}
}
}
}
}
}