2011-07-19 34 views
0

所以我正在嘗試創建一個應用程序,根據您滑動的方向更改標籤。但是,我不希望在屏幕上的任何位置滑動,而只是在特定的視圖上滑動。我在.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"; 
} 

回答

2

你可以實現你以後通過增加兩個UIGestureRecognizer S(一個向右掃動,另一個用於向左掃)到UIView要接受滑動。

蘋果的SimpleGestureRecognizer sample project給出了一個很好的例子說明如何設置它。

0

檢查身份檢查器是否爲組織者中的UIView對象選擇了uiview類。我想你已經在UIView的子類中實現了touchesBegan方法。