#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