UIButton长按事件

 添加长按事件

 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( 0106060)];
 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:
        {
            
        }      
    }
}

 

 

---恢复内容开始---

//如果你打开横向或纵向的滚动条,这里可以设置滚动条的风格
    // UIScrollViewIndicatorStyleDefault, 默认风格
    // UIScrollViewIndicatorStyleBlack,   黑色风格
    // UIScrollViewIndicatorStyleWhite    白色风格
    //[_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack]

---恢复内容结束---


转载于:https://www.cnblogs.com/ioschen/p/3311672.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值