我有一些图像,所有这些图像都放在自己的 UIImageView 上。我有一个 NSArray,其中包含我希望图像移动的多个点。所有图像必须相互跟随,并且 10 个图像中可能只有 5 个同时在该轨道上(用户设置)。仍然这些图像必须均匀地分布在路径上。
我的问题是我可以让图像移动,但我不能像 UIScrollView 和 UITableView 那样进行“轻弹或轻弹”移动。(通过 UIPanGesture 移动并在释放时使用剩余速度来计算图像应该保持移动多长时间,同时放慢速度直到它们可以停止)。
我的 UIImageViews 跳到最后一个地方,而不是从一个点移动到另一个点。我已经尝试过使用默认动画,但是离开和进入屏幕的 UIImageViews 给我带来了问题。
这是我为测试这部分(运动)而创建的代码
- (void) move: (int) direction{
switch (direction) {
case MOVEMENT_LEFT:
{
if ((currentLocation -1) <= startpoint){
currentLocation = endpoint;
} else {
currentLocation--;
}
}
break;
case MOVEMENT_RIGHT:
{
if ((currentLocation +1) >= endpoint){
currentLocation = startpoint;
} else {
currentLocation++;
}
}
break;
default:
break;
}
[image setFrame:CGRectMake(currentLocation, 100, 125, 125)];
}
- (void) panHandler: (UIPanGestureRecognizer *) recognizer
{
CGPoint velocity = [recognizer velocityInView:[self view]];
//NSLog(@"Speed: x.%f y.%f", velocity.x, velocity.y);
if (recognizer.state == UIGestureRecognizerStateBegan){
} else if (recognizer.state == UIGestureRecognizerStateChanged){
if((velocity.x < 0 && velocity.y < 0) || (velocity.x < 0 && velocity.y > 0) || (velocity.x < 0 && velocity.y == 0) || (velocity.x == 0 && velocity.y < 0))
{
speed = floorf(sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y)));
gestureDirection = MOVEMENT_LEFT;
LOG_direction = @"Left";
[self move:MOVEMENT_LEFT];
} else if((velocity.x > 0 && velocity.y > 0) || (velocity.x > 0 && velocity.y < 0) || (velocity.x > 0 && velocity.y == 0) || (velocity.x == 0 && velocity.y > 0))
{
speed = floorf(sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y)));
gestureDirection = MOVEMENT_RIGHT;
LOG_direction = @"Right";
[self move:MOVEMENT_RIGHT];
//[image setFrame:CGRectMake(currentLocation, 100, 125, 125)];
}
} else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateFailed || recognizer.state == UIGestureRecognizerStateCancelled){
NSLog(@"Movement %@ with start speed: %i", LOG_direction, speed);
if (speed > 1250){
NSLog(@"Fling Movement Event");
//int sleeper = 0;
if (gestureDirection == MOVEMENT_LEFT){
for (int i = speed; i > 0; i--){
[image removeFromSuperview];
[self move:MOVEMENT_LEFT];
sleep(.05);
[[self view] addSubview:image];
//sleeper = sleeper + 100;
//sleep(sleeper);
}
} else {
for (int i = speed; i > 0; i--){
[image removeFromSuperview];
[self move:MOVEMENT_RIGHT];
sleep(.05);
[[self view] addSubview:image];
//sleeper = sleeper + 100;
//sleep(sleeper);
}
}
} else {
NSLog(@"Normal Movement Event");
}
}
}
我想要实现的是:在 SINUS-WAVE 上从左到右或向后移动的 UIImageViews(取决于用户的触摸),离开左边的图像将被右侧数组中的下一个图像替换,也向后..
所以:[图像 F] [图像 A] [图像 B] [图像 C] [图像 D] 用户将手指向右移动......并且: [图像 G] [图像 F] [图像 A] [图像 B] [图片 C] 并且随着一甩,它会向前迈进一步,但仍会在旅途中显示所有图像..
希望有人可以帮助..