本文介紹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è),謝謝!
荊軻刺秦王!