如何使用坐标检测手指向上/向下到向上

时间:2013-06-20 11:42:05

标签: ios objective-c cocoa-touch

正如我CustomView我想要检测到Touch Event,就像用户执行Touch向下/向上和释放一样,反之亦然,如图中所示。

enter image description here

我尝试了什么

滑动但在其中我只获得用户释放手指的坐标。例如点按X = 100并在10上发布,只获得10

我在寻找什么

希望得到整个坐标,例如用户点按X = 100并在X = 80上发布,然后在手指移动时查找与100,99,98,97,96,95,94.......80.相同的每个坐标。

如果有人对此有任何疑问,请忘记做。 请复习。

3 个答案:

答案 0 :(得分:1)

将此更改为.h文件

@interface ViewController : UIViewController{
    CGFloat touchStartPoint;
    CGFloat touchOffsetPoint;
    CGFloat tempTouchOffsetPoint;
}

@end

这是你的.m文件

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    touchStartPoint=[touch locationInView:self.view].y;
    touchOffsetPoint = 0;
    tempTouchOffsetPoint = 0;
    //  NSLog(@"touchStartPoint = %f",touchStartPoint);   
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    touchOffsetPoint = [touch locationInView:self.view].y - touchStartPoint;


    if (touchOffsetPoint>tempTouchOffsetPoint) {
        NSLog(@"touch down");
    }else{
        NSLog(@"touch up");
    }

    tempTouchOffsetPoint = touchOffsetPoint;
}

答案 1 :(得分:1)

我是这样做的:

 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(handleSwipeGesture:)];
 swipeGesture.direction =    UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
 [self.gestureAreaView addGestureRecognizer:swipeGesture];
 [swipeGesture release];

  -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender 
 {
//Gesture detect - swipe up/down , can't be recognized direction
 }
 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]    initWithTarget:self action:@selector(handleSwipeGesture:)];
 swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
 [self.view addGestureRecognizer:swipeGesture];
 [swipeGesture release];

 UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
 swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
 [self.view addGestureRecognizer:swipeGesture2];
 [swipeGesture2 release];

 -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender 
 {
//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{
}
else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
{
}
}

希望这会有所帮助

答案 2 :(得分:0)

试试这个方法

// declared somewhere
@property (nonatomic, retain) NSMutableArray *touchPositions;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint loc = [touch locationInView:self];
    [self.touchPositions addObject:[NSValue valueWithCGPoint:loc]];
}