2010-01-11 25 views
4

有沒有人能夠在UITableView上實現成功的滑動手勢?默認情況下,由於控件的滾動特性,這是不可能的。我已經嘗試了繼承UITextView並在實例方法中實現滑動功能,但沒有使用骰子。在UITextView中成功滑動?

我的UITextView已禁用滾動 - 除非有另一種方法來實現多行文本?

[編輯]我想說的是輸入多行文本[/編輯]

回答

3

這裏是一個UITextView,將檢測刷卡手勢的子類...

這是你在找什麼?

#import <UIKit/UIKit.h> 

#define kMinimumGestureLength 25 
#define kMaximumVariance  5 

@interface SwipeableTextView : UITextView { 
    CGPoint gestureStartPoint; 
} 

@end 

@implementation SwipeableTextView 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 

    UITouch *touch =[touches anyObject]; 
    gestureStartPoint = [touch locationInView:self]; 

} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesMoved:touches withEvent:event]; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPosition = [touch locationInView:self]; 

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); 
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); 

    if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) { 
     NSLog(@"Horizontal swipe detected"); 
    } 

} 

@end 
+0

謝謝 - 我會嘗試這個今天在某些時候,讓你知道如何去。 – mootymoots 2010-01-13 09:50:54

+0

恐怕這不起作用。我已經將它添加爲子類,在Interface Builder中將類更改爲新的子類,並且將主視圖控制器中的類更新爲子類,但未檢測到刷卡:-( – mootymoots 2010-01-14 16:21:28

1

通常你在UITableViewCell中實現觸摸響應事件,它可以識別滑動並執行某些操作。 UITableViews繼承自UIScrollView,它不喜歡觸摸事件被重寫。

+0

嗨,您能解釋更多爲什麼UIScrollView不喜歡讓它的觸摸事件被覆蓋? – Ross 2010-01-12 14:25:20

+1

除了觸摸事件做了很多複雜的工作之外,並不是很多,所以當它沒有得到所有的東西時,它無法弄清楚發生了什麼。 。抱歉,我不能添加更多細節,我只是從經驗中知道它不適用。 – 2010-01-12 17:09:29

0

您也可以使用雙指輕掃。我回到家時,我可以用一些代碼闡述,但是,這是我所使用

2

UITextView的頂部添加透明UIView,並用它來處理輕掃,併發送touchesBegan/Moved/Ended/Canceled:消息文本視圖保持正常相互作用。

2

我拿了羅斯的代碼,並添加了一些幫助我的東西。特別是,這段代碼在停止之前不會響應滑動。它還可以防止在中途翻轉方向。

#import <UIKit/UIKit.h> 

#define kMinimumGestureLength 25 
#define kMaximumVariance  5 

typedef enum swipeDirection { 
    kSwipeNone, 
    kSwipeLeft, 
    kSwipeRight 
} tSwipeDirection; 

@interface SwipeableTextView : UITextView { 
    CGPoint gestureStartPoint; 
    tSwipeDirection swipeDirection; 
} 

@end 

@implementation SwipeableTextView 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 

    swipeDirection = kSwipeNone; 
    UITouch *touch =[touches anyObject]; 
    gestureStartPoint = [touch locationInView:self]; 

} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesMoved:touches withEvent:event]; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPosition = [touch locationInView:self]; 

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); 
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); 

    // Check if we already started a swipe in a particular direction 
    // Don't let the user reverse once they get going 
    if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance && 
     swipeDirection == kSwipeNone) { 
     if (gestureStartPoint.x < currentPosition.x) { 
      swipeDirection = kSwipeRight; 
     } 
     else { 
      swipeDirection = kSwipeLeft; 
     } 
    } 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (swipeDirection == kSwipeRight) { 
     NSLog(@"Swipe right"); 
    } 
    else if (swipeDirection == kSwipeLeft) { 
     NSLog(@"Swipe left"); 
    } 
    [super touchesEnded:touches withEvent:event]; 
} 

@end 
3

一個更好的解決辦法,我已經找到了,就是簡單地通過觸摸回到上海華:

@interface SwipeableTextView : UITextView { 
} 

@end 

@implementation SwipeableTextView 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 

    [self.superview touchesBegan:touches withEvent:event]; 

} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesMoved:touches withEvent:event]; 

    [self.superview touchesMoved:touches withEvent:event]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesEnded:touches withEvent:event]; 

    [self.superview touchesEnded:touches withEvent:event]; 
} 

@end