所以我正在嘗試創建一個應用程序,根據您滑動的方向更改標籤。但是,我不希望在屏幕上的任何位置滑動,而只是在特定的視圖上滑動。我在.xib文件上添加了一個新視圖,並將該視圖用於觸摸的位置並將其命名爲leftView,但它仍然使用整個屏幕。我正在使用touchesBegan和touchesMoved方法,所以我可以使用對角線。 這裏有點我使用的代碼:多次滑動視圖
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:leftView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:leftView];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if ((deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) && currentPosition.x < gestureStartPoint.x){
label.text = @"Left Horizontal swipe detected";
}
else if((deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) && currentPosition.x > gestureStartPoint.x){
label.text = @"Right Horizontal swipe detected";
}
else if ((deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) && currentPosition.y < gestureStartPoint.y){
label.text = @"Up Vertical swipe detected";
}
else if ((deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) && currentPosition.y > gestureStartPoint.y){
label.text = @"Down Vertical swipe detected";
}