【发布时间】:2015-04-15 18:55:45
【问题描述】:
我试图在我的应用中显示车辆在道路上的运动。为此,我让我的车辆保持静止,并不断地从上到下移动道路上的车道以创造效果。车道作为子视图添加到 roadView,我正在尝试为 roadView 中的所有子视图设置动画,如下面的代码所示。
我面临的问题是最接近底部的车道与开始时顶部的车道相比以更快的速度移动,尽管我是根据恒定速度计算持续时间。
-(void)animateLanes {
for (UIView *laneView in [self.roadView subviews]) {
if (laneView.tag < 10) {
float distance = self.view.frame.size.height - CGRectGetMaxY(laneView.frame);
float duration = distance/10;
[UIView animateWithDuration:duration animations:^{
laneView.frame = CGRectMake(laneView.frame.origin.x, self.view.frame.size.height+laneView.frame.size.height, laneView.frame.size.width, laneView.frame.size.height);
} completion:^(BOOL finished) {
laneView.frame = CGRectMake(self.roadView.frame.size.width/2 - 10, -laneView.frame.size.height, 20, 40);
[self nextStep:laneView];
}];
}
}
}
-(void) nextStep:(UIView *) laneView{
float distance = self.view.frame.size.height - CGRectGetMaxY(laneView.frame);
float duration = distance/10;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionRepeat animations:^{
laneView.frame = CGRectMake(laneView.frame.origin.x, self.view.frame.size.height+laneView.frame.size.height, laneView.frame.size.width, laneView.frame.size.height);
} completion:^(BOOL finished) {
laneView.frame = CGRectMake(self.roadView.frame.size.width/2 - 10, -laneView.frame.size.height, 20, 40);
}];
}
【问题讨论】:
-
这听起来不太适合视图动画。考虑学习游戏编程技术。
标签: ios objective-c animation uiview uiviewanimation