0

正如我CustomView想要检测Touch Event的那样,当用户执行Touch向下/向上和释放时,反之亦然,如本图所示。

在此处输入图像描述

我试过的

滑动但在其中我只获取用户释放手指的坐标。就像点击X = 100和释放一样,我只10得到10

我在找什么

想要像用户点击X = 100 和释放一样获得整个坐标,X = 80然后寻找每个坐标的变化,就像100,99,98,97,96,95,94.......80.手指移动一样。

请如果有人对此有任何想法或我忘记做的事情。请查阅。

4

3 回答 3

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;
}
于 2013-06-20T12:06:17.340 回答
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)
{
}
}

希望这会有所帮助

于 2013-06-20T12:11:21.903 回答
0

Try this method

// 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]];
}
于 2013-06-20T11:54:08.737 回答