在没有CAAnimations但使用“Fling / Flick”的多个CGPoints(Array)上移动UIImageView

时间:2012-08-29 14:33:55

标签: ios uiimageview uipangesturerecognizer

我有一些图像,所有这些图像都放在他们自己的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");
        }
    }

}

我想要实现的是:UIImageViews从左到右或后退(取决于用户触摸)在SINUS-WAVE上移动,左边的图像将被替换为数组中的下一个图像对,也向后..

所以:[图像F] [图像A] [图像B] [图像C] [图像D] 用户将手指向右移动...... 并且:[图像G] [图像F] [图像A] [图像B] [图像C] 随着它的飞速发展它会向前迈进一步,但仍然可以随时显示所有图像......

希望有人可以提供帮助......

1 个答案:

答案 0 :(得分:0)

UIScrollView有一个属性“pagingEnabled”,它将滚动视图的大小作为页面大小,滚动时,它将转到内容的下一个“页面”

IE:你的滚动视图大小是100x100,你的contentSize是400x100,从contentOffset 0x0开始,当你向左滑动时,你的contentOffset将变为100x0,然后是200x0,依此类推,停在正确的数字

但是,如果您想在自定义尺寸的自定义位置实现此效果,则需要手动编码,因为没有任何东西可以实现您的目标

我希望我有所帮助,祝你好运!