zoukankan      html  css  js  c++  java
  • 在UITableView中识别左右滑动,实现上下翻页的功能

    目前有三种方案:

    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");
    
    }
  • 相关阅读:
    PHP $_SERVER['HTTP_REFERER'] 获取前一页面的 URL 地址
    LAMP与LNMP架构的区别及其具体的选择说明
    LNMP 与 LAMP 架构的区别及配置解决方案
    LAMP和LNMP,你更愿意选择谁,为什么?
    Storm流计算从入门到精通之技术篇(高并发策略、批处理事务、Trident精解、运维监控、企业场景)
    Zookeeper从入门到精通(开发详解,案例实战,Web界面监控)
    基于Greenplum Hadoop分布式平台的大数据解决方案及商业应用案例剖析
    深入浅出Hive企业级架构优化、Hive Sql优化、压缩和分布式缓存(企业Hadoop应用核心产品)
    深入浅出OpenStack云计算平台管理(nova-compute/network)
    玩转大数据:深入浅出大数据挖掘技术(Apriori算法、Tanagra工具、决策树)
  • 原文地址:https://www.cnblogs.com/JuneWang/p/3764373.html
Copyright © 2011-2022 走看看