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]

---恢復內容結束---



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM