@interface ViewController ()
@property(nonatomic,strong)UIButton * button;
-(void)btnshort; //短按回调方法
-(void)btnlong:(UILongPressGestureRecognizer*)gestureRecognizer;//长按回调方法
@end
@implementation ViewController
-(void)viewDidLoad{
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(40, 100, 60, 60);
self.button.backgroundColor = [UIColor redColor];
//button点击时间
[self.button addTarget:self action:@selector(btnshort)forControlEvents:UIControlEventTouchUpInside];
//button长按事件
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:selfaction:@selector(btnlong:)];
longPress.minimumPressDuration = 0.8;//定义按的时间
[self.button addGestureRecognizer:longPress];
[self.view addSubview:self.button];
}
-(void)btnshort
{
NSLog(@"短按事件");
}
-(void)btnlong:(UILongPressGestureRecognizer*)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
{
NSLog(@"长按事件");
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"消息"message:@"确定删除图片吗?" delegate:self cancelButtonTitle:@"确 定"otherButtonTitles: nil];
[alert show];
}
}
转载于:https://www.cnblogs.com/ylg-----/p/4753012.html
相关资源:ios-布局UIButton中TitleLabel和ImageView位置.zip