ios UIButton的點擊與松開事件處理


ios UIButton的點擊與松開事件處理

按鈕點擊打印字符串也可以處理其他事件

按鈕松開也打印字符串也可以處理其他事件

相關代碼如下


    #import "ViewController.h"  
      
    @interface ViewController ()  
      
    @property (nonatomic, strong) UIButton *testbtn;  
      
    @end  
      
    @implementation ViewController  
    @synthesize testbtn;  
      
    - (void)viewDidLoad  
    {  
        [super viewDidLoad];  
    self.testbtn.frame = CGRectMake(10, 10, 50, 50);
        self.testbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];   
      
        [self.testbtn setTitle:@"點擊" forState:UIControlStateNormal];  
        [self.testbtn setTitle:@"松開" forState:UIControlEventTouchDown];  
    //處理按鈕點擊事件
        [self.testbtn addTarget:self action:@selector(TouchDown)forControlEvents: UIControlEventTouchDownInside];  
    //處理按鈕松開狀態
        [self.testbtn addTarget:self action:@selector(TouchUp)forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];  
          
        [self.view addSubview:self.testbtn];  
    }  
      
      
    - (void)TouchDown  
    {  
    //這里可以處理其他點擊事件
        NSLog(@"按鈕點擊了");
    }  
      
    - (void)TouchUp
    {  
    //這里可以處理其他松開事件
        NSlog(@"按鈕松開了");  
    }  
      
    @end 


免責聲明!

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



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