UIButton UIControlEventTouchUpInside does not fire

2019-04-08 06:11发布

I am implementing UIButton and my UIControlEventTouchUpInside event does not fire, even though UIControlEventTouchDown does fire.

UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *filePathImage = @"img.png";
NSString *filePathImageTap = @"img-tap.png";
UIImage *buttonImage = [UIImage imageWithContentsOfFile:filePathImage];
UIImage *buttonImageTap = [UIImage imageWithContentsOfFile:filePathImageTap];
[btnClose setImage:buttonImage forState:UIControlStateNormal];
[btnClose setImage:buttonImageTap forState:UIControlStateSelected];
[btnClose setImage:buttonImageTap forState:UIControlStateHighlighted];      
[btnClose addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];    
[self.view addSubview:btnClose];

1条回答
The star\"
2楼-- · 2019-04-08 06:55

I have an assumption why it doesn't take the UIControlEventTouchUpInside:

I had two "BannerView" (Advertisement) in two different ViewControllers. In one it just worked fine in the other I saw how the UIButton was touched and the second image (darker) appeared. But the selector was not hit. Then I realized, that the Up-Event was absorbt by a UISwipeGestureRecognizer from a UIImageView below. Some Recognizer compete against each other. I searched for an overview to see witch ones these are, but found nothing.

Another solution: If you see that the ButtonState doesn't change u have to look if you have a View over your Button.

查看更多
登录 后发表回答