UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [aBtn setFrame:CGRectMake(40, 100, 60, 60)]; [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"] forState:UIControlStateNormal];//button點擊事件 [aBtn addTarget:self action:@selector(btnShort:) forControlEvents:UIControlEventTouchUpInside]; //button長按事件 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)]; longPress.minimumPressDuration = 0.8; //定義按的時間 [aBtn addGestureRecognizer:longPress];-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{ if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) { NSLog(@"長按事件"); UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"訊息" message:@"確定刪除該模式嗎?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"刪除", nil]; [alert show]; }}
轉載地址:http://blog.sina.com.cn/s/blog_49b531af0102e3rt.html