在UITableView中識別作用滑動,實現上下翻頁的功能

來源:互聯網
上載者:User

目前有三種方案:

1.

UIScrollView + UITableView。

實現方法,在UIScrollView中,加入UITableView即可

設定UIScrollView的代理和方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    int currentPostion = scrollView.contentOffset.x;    if (currentPostion - 0 > 50) {        NSLog(@"Scroll right now ");    }    else if (0 - currentPostion > 50)    {        NSLog(@"Scroll left now");    }}

 

2.利用UISwipeGestureRecognizer 

原文地址:http://www.2cto.com/kf/201312/265158.html

-(void)viewDidLoad{UISwipeGestureRecognizer *recognizer;recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];[[self view] addGestureRecognizer:recognizer];recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];[[self view] addGestureRecognizer:recognizer];recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];[[self view] addGestureRecognizer:recognizer];recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];[[self view] addGestureRecognizer:recognizer];}-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{if(recognizer.direction==UISwipeGestureRecognizerDirectionDown) {NSLog(@"swipe down");//執行程式}if(recognizer.direction==UISwipeGestureRecognizerDirectionUp) {NSLog(@"swipe up");//執行程式}if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {NSLog(@"swipe left");//執行程式}if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {NSLog(@"swipe right");//執行程式}}

 

3.

原文地址:http://www.cppblog.com/Khan/archive/2013/02/27/198100.html

UITableView 屏蔽了左右滑動事件.  通過重載的方式可以注入事件touch事件, 供開發人員使用..

 #import <UIKit/UIKit.h> @protocol TouchTableViewDelegate <NSObject> @optional - (void)tableView:(UITableView *)tableView touchesBegin:(NSSet *)touches withEvent:(UIEvent *)event; - (void)tableView:(UITableView *)tableView touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; - (void)tableView:(UITableView *)tableView touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)tableView:(UITableView *)tableView touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; @end

 #import "TouchTableView.h"  @implementation TouchTableView  @synthesize touchDelegate = _touchDelegate;  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {     [super touchesBegan:touches withEvent:event];          if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&         [_touchDelegate respondsToSelector:@selector(tableView:touchesBegin:withEvent:)])     {         [_touchDelegate tableView:self touchesBegin:touches withEvent:event];     } }  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {     [super touchesCancelled:touches withEvent:event];          if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&         [_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)])     {         [_touchDelegate tableView:self touchesCancelled:touches withEvent:event];     } }  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {     [super touchesEnded:touches withEvent:event];          if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&         [_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)])     {         [_touchDelegate tableView:self touchesEnded:touches withEvent:event];     } }  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {     [super touchesMoved:touches withEvent:event];          if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] &&         [_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)])     {         [_touchDelegate tableView:self touchesMoved:touches withEvent:event];     } }  @end
 調用方法 :1. 標頭檔中加入delegate
@interface MoneyViewCtl : UIViewController<UITableViewDataSource, UITableViewDelegate, SDWebDataDownloaderDelegate, EGORefreshTableHeaderDelegate, TouchTableViewDelegate>{        IBOutlet UISegmentedControl *_sigTime;    IBOutlet TouchTableView *_tableview; }@end

 2. .m檔案中設定好delegate

_tableview.touchDelegate = self;

 3. .m檔案中實現如下事件 

#pragma mark - TouchTableViewDelegate lifecycle- (void)tableView:(UITableView *)tableView touchesBegin:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"touchesBegin");}- (void)tableView:(UITableView *)tableView touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"touchesCancelled");} - (void)tableView:(UITableView *)tableView touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"touchesEnded");}- (void)tableView:(UITableView *)tableView touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"touchesMoved");}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.