添加长按事件
1 - (
void)viewDidLoad
2 {
3 [super viewDidLoad];
4 // Do any additional setup after loading the view, typically from a nib.
5
6 UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
7 [aBtn setFrame:CGRectMake( 0, 10, 60, 60)];
8 [aBtn setBackgroundColor:[UIColor redColor]];
9 // button点击事件
10 [aBtn addTarget:self action:@selector(btnShort) forControlEvents:UIControlEventTouchUpInside];
11 // button长按事件
12 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
13 longPress.minimumPressDuration = 0.5; // 定义按的时间
14 [aBtn addGestureRecognizer:longPress];
15
16 [self.view addSubview:aBtn];
17 }
18 -( void)btnShort
19 {
20 NSLog( @" de ");
21 }
22 -( void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
23 if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
24 NSLog( @" 长按事件 ");
25 UIAlertView *alert=[[UIAlertView alloc] initWithTitle: @" 消息 " message: @" 确定删除该模式吗? " delegate:self cancelButtonTitle: @" 取消 " otherButtonTitles: @" 删除 ", nil];
26 [alert show];
27 }
28 }
2 {
3 [super viewDidLoad];
4 // Do any additional setup after loading the view, typically from a nib.
5
6 UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
7 [aBtn setFrame:CGRectMake( 0, 10, 60, 60)];
8 [aBtn setBackgroundColor:[UIColor redColor]];
9 // button点击事件
10 [aBtn addTarget:self action:@selector(btnShort) forControlEvents:UIControlEventTouchUpInside];
11 // button长按事件
12 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
13 longPress.minimumPressDuration = 0.5; // 定义按的时间
14 [aBtn addGestureRecognizer:longPress];
15
16 [self.view addSubview:aBtn];
17 }
18 -( void)btnShort
19 {
20 NSLog( @" de ");
21 }
22 -( void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
23 if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
24 NSLog( @" 长按事件 ");
25 UIAlertView *alert=[[UIAlertView alloc] initWithTitle: @" 消息 " message: @" 确定删除该模式吗? " delegate:self cancelButtonTitle: @" 取消 " otherButtonTitles: @" 删除 ", nil];
26 [alert show];
27 }
28 }
更多介绍
//
加个 longPressGesture ,设置如下:
UILongPressGestureRecognizer *pahGestureRecognizer=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];
pahGestureRecognizer. delegate = self; // 指定委托
pahGestureRecognizer.minimumPressDuration = 0.3; // 最少按压响应时间
[scrollView addGestureRecognizer:pahGestureRecognizer]; // 指定对象为scrollView
// [pahGestureRecognizer release];
// 实现委托方法:判断手势状态 动作开始、移动变化、结束
- ( void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer
{
switch (gestureRecognizer.state)
{
case UIGestureRecognizerStateBegan:
{
}
case UIGestureRecognizerStateChanged:
{
}
case UIGestureRecognizerStateEnded:
{
}
}
}
UILongPressGestureRecognizer *pahGestureRecognizer=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];
pahGestureRecognizer. delegate = self; // 指定委托
pahGestureRecognizer.minimumPressDuration = 0.3; // 最少按压响应时间
[scrollView addGestureRecognizer:pahGestureRecognizer]; // 指定对象为scrollView
// [pahGestureRecognizer release];
// 实现委托方法:判断手势状态 动作开始、移动变化、结束
- ( void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer
{
switch (gestureRecognizer.state)
{
case UIGestureRecognizerStateBegan:
{
}
case UIGestureRecognizerStateChanged:
{
}
case UIGestureRecognizerStateEnded:
{
}
}
}
---恢复内容开始---
//如果你打开横向或纵向的滚动条,这里可以设置滚动条的风格// UIScrollViewIndicatorStyleDefault, 默认风格
// UIScrollViewIndicatorStyleBlack, 黑色风格
// UIScrollViewIndicatorStyleWhite 白色风格
//[_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack]
---恢复内容结束---