iOS 手勢(shì)

本文介紹iOS開發(fā)中手勢(shì)的運(yùn)用,話不多說(shuō),先上代碼;
給tableView添加長(zhǎng)按手勢(shì)代碼如下:

UILongPressGestureRecognizer *longPressG = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(presentAlertSheetEvent:)];
longPressG.minimumPressDuration = 1.0;//長(zhǎng)按最短時(shí)間
[tableView addGestureRecognizer:longPressG];//給tableView對(duì)象添加長(zhǎng)按手勢(shì)

給cell添加長(zhǎng)按手勢(shì)代碼:

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //給cell添加長(zhǎng)按手勢(shì) - wsx注釋
    UILongPressGestureRecognizer *longPressG = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(presentAlertSheetEvent:)];
    longPressG.minimumPressDuration = 1.0;
    [cell addGestureRecognizer:longPressG];
    return cell;
}

手勢(shì)觸發(fā)執(zhí)行方法代碼如下:

//表單警告框代碼
-(void)presentAlertSheetEvent:(UILongPressGestureRecognizer *)gesture {
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        CGPoint point = [gesture locationInView:tableView];//獲取按點(diǎn)在tableView上的point
        NSIndexPath * indexPath = [tableView indexPathForRowAtPoint:point];
        if(indexPath == nil) return ;
        //add your code here
        //選項(xiàng)提示 - wsx注釋
        {
            NSLog(@"row = %ld",(long)indexPath.row);
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警  告" message:@"這里寫警告的內(nèi)容!" 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];
        }
    }
}

作者將持續(xù)繼續(xù)更新,如果覺得有幫助,請(qǐng)關(guān)注一個(gè),謝謝!
荊軻刺秦王!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容