iPhone:通过滑动连续移动UIImageView

时间:2010-08-17 13:48:52

标签: iphone objective-c

我想用手势连续移动图像(cBlock)。滑动手势的方向决定了移动的方向。我能够移动,但不能持续移动。虽然手指被按下,但运动开始/停止。谁能明白为什么?

我相信touchesMoved会被连续调用,因为日志语句似乎是连续启动的。此外,这个逻辑似乎并没有考虑到手指被按下而方向变为正,然后是负面(例如,用户平移右边,然后平移而没有抬起手指。)这种情况是否有经过验证的解决方案?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:gridView];
    prevGesturePoint = gestureStartPoint;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:gridView];   

    CGFloat deltaX = currentPosition.x - prevGesturePoint.x;
    CGFloat deltaY = currentPosition.y - prevGesturePoint.y;

    if(running == YES){
        if(fromTop || fromBottom){ 
            if((currentPosition.x - prevGesturePoint.x) > 0) {
                NSLog(@"swipe right.");
                [cBlock movXSinglePixel:10]; // move 10px
            } else if((currentPosition.x - prevGesturePoint.x) < 0) {
                NSLog(@"swipe left.");
                [cBlock movXSinglePixel:-10]; //move -10px
            }
        }
    }

    [container setNeedsDisplay];

    prevGesturePoint = currentPosition; 
}


- (void)movXSinglePixel:(int)dir{

    [UIView beginAnimations:@"center" context:nil];
    [UIView setAnimationDuration:0.01];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    if(tX + dir > 8 || tX + dir < 0){
        // Attempt to move out of grid bounds.
        return;
    }
    CGPoint center = [self center];
    center.x += (dir * 1.0f);
    [self setCenter:center];
    tX += dir;


    [UIView commitAnimations];
}

0 个答案:

没有答案