#iOS开发常用方法集锦#为UITableView添加UISwipeGestureRecognizer手势

?

本文永久地址为http://www.cnblogs.com/ChenYilong/p/4103039.html ,转载请注明出处。

印象笔记链接:https://app.yinxiang.com/shard/s22/sh/04150175-aac6-4981-b71d-d7246de3037b/a0f139b2619a4607

?

?

?

<UIGestureRecognizerDelegate>

-(void)viewDidLoad

{

? ? [superviewDidLoad];

? ? [selfsetupSwipeGestureRecognizer];

? ? //...

}

-(void)setupSwipeGestureRecognizer{

? ? UISwipeGestureRecognizer*swipeGestureUp=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeGesture:)];

? ? swipeGestureUp.direction=UISwipeGestureRecognizerDirectionUp;

? ? //默认为1,可删

? ? [swipeGestureUpsetNumberOfTouchesRequired:1];

? ? //默认为YES,可删

? ? [swipeGestureUpsetDelaysTouchesBegan:YES];

? ? //当self为UIScrollerView或其子类时,比如为UITableView添加手势时,必须添加此行代码,详见下方参考链接

? ? swipeGestureUp.delegate=self;

? ? [self.viewaddGestureRecognizer:swipeGestureUp];

?

? ? UISwipeGestureRecognizer*swipeGestureDown=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeGesture:)];

? ? swipeGestureDown.direction=UISwipeGestureRecognizerDirectionDown;

? ? //默认为1,可删

? ? [swipeGestureDownsetNumberOfTouchesRequired:1];

? ? //默认为YES,可删

? ? [swipeGestureDownsetDelaysTouchesBegan:YES];

? ? //当self为UIScrollerView或其子类时,比如为UITableView添加手势时,必须添加此行代码,详见下方参考链接

? ? swipeGestureDown.delegate=self;

? ? [self.viewaddGestureRecognizer:swipeGestureDown];

}

-(void)handleSwipeGesture:(UISwipeGestureRecognizer*)sender

{

? ? //Gesture detect - swipe up/down , can be recognized direction

? ? if(sender.direction==UISwipeGestureRecognizerDirectionUp)

? ? {

? ? ? ? MyLog(@"向上滑动");

? ? }

? ? elseif(sender.direction==UISwipeGestureRecognizerDirectionDown)

? ? {

? ? ? ? MyLog(@"向下滑动");

? ? }

}

//当self为UIScrollerView或其子类时,比如为UITableView添加手势时,必须添加此行代码,详见下方参考链接

-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{

? ? returnYES;

}

//左右手势时添加

// -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

// ? ? return UITableViewCellEditingStyleNone;

// }

//参考文献

//下面的第一个链接解释为何要添加gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法

http://stackoverflow.com/questions/2800016/how-do-you-add-gestures-to-a-uitableviewcontroller

//下边两个是为CELL添加手势

http://stackoverflow.com/questions/4454920/gesture-recognizers-and-tableview

http://stackoverflow.com/a/4604667/213987

//下面是替代#解决方案#,用touch的delegate事件来#解决方案#

http://stackoverflow.com/questions/8181774/how-to-recognize-swipe-in-all-4-directions/20734326#20734326

@end

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。