OC开发资料收集区

iOS 手势

2017-07-20  本文已影响4人  oc123

本文介绍iOS开发中手势的运用,话不多说,先上代码;
给tableView添加长按手势代码如下:

UILongPressGestureRecognizer *longPressG = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(presentAlertSheetEvent:)];
longPressG.minimumPressDuration = 1.0;//长按最短时间
[tableView addGestureRecognizer:longPressG];//给tableView对象添加长按手势

给cell添加长按手势代码:

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //给cell添加长按手势 - wsx注释
    UILongPressGestureRecognizer *longPressG = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(presentAlertSheetEvent:)];
    longPressG.minimumPressDuration = 1.0;
    [cell addGestureRecognizer:longPressG];
    return cell;
}

手势触发执行方法代码如下:

//表单警告框代码
-(void)presentAlertSheetEvent:(UILongPressGestureRecognizer *)gesture {
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        CGPoint point = [gesture locationInView:tableView];//获取按点在tableView上的point
        NSIndexPath * indexPath = [tableView indexPathForRowAtPoint:point];
        if(indexPath == nil) return ;
        //add your code here
        //选项提示 - wsx注释
        {
            NSLog(@"row = %ld",(long)indexPath.row);
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警  告" message:@"这里写警告的内容!" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *centain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:centain];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
}

作者将持续继续更新,如果觉得有帮助,请关注一个,谢谢!
荆轲刺秦王!

上一篇下一篇

猜你喜欢

热点阅读