ios UIButton的點選與鬆開事件處理
阿新 • • 發佈:2019-01-23
#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
@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