问题描述
我有一个 UIButton.它有两个子视图.然后我打电话:
I have a UIButton. It has two subviews. I then call:
[createButton addTarget:self action:@selector(openComposer) forControlEvents:UIControlEventTouchUpInside];
如果我点击 UIButton 中未被覆盖但它的子视图之一的部分,它可以正常工作.但是,如果我点击其中一个子视图,它不会触发操作.
If I tap the parts of the UIButton that aren't covered but one of its subviews, it works fine. However if I tap one of the subviews instead, it does not trigger the action.
在两个子视图上,我都将 .userInteractionEnabled 设置为 YES.
On both subviews, I has .userInteractionEnabled set to YES.
两个子视图都是带有 frame 和 backgroundColor 的普通 UIViews.
Both subviews are plain UIViewss with a frame and backgroundColor.
如何让点击通过"UIView 并进入 UIButton?
How can I get the tap to "pass through" the UIView and onto the UIButton?
谢谢
我需要使用 UIControlEvents,因为我需要 UIControlEventTouchDown.
I need to use UIControlEvents because I need UIControlEventTouchDown.
编辑 2:
这是按钮的代码.
@interface CreateButton ()
@property (nonatomic) Theme *theme;
@property (nonatomic) UIView *verticle;
@property (nonatomic) UIView *horizontal;
@property (nonatomic) BOOL mini;
@end
@implementation CreateButton
#pragma mark - Accessors
- (UIView *)verticle {
if (! _verticle) {
_verticle = [[UIView alloc] init];
_verticle.backgroundColor = [self.theme colorForKey:@"createButtonPlusBackgroundColor"];
_verticle.userInteractionEnabled = NO;
}
return _verticle;
}
- (UIView *)horizontal {
if (! _horizontal) {
_horizontal = [[UIView alloc] init];
_horizontal.backgroundColor = [self.theme colorForKey:@"createButtonPlusBackgroundColor"];
_verticle.userInteractionEnabled = NO;
}
return _horizontal;
}
#pragma mark - Init
- (instancetype)initWithTheme:(Theme *)theme frame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_theme = theme;
self.layer.cornerRadius = roundf(frame.size.width / 2);
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = .1f;
self.layer.shadowOffset = CGSizeZero;
self.layer.shadowRadius = 15.f;
self.backgroundColor = [self.theme colorForKey:@"createButtonBackgroundColor"];
[self addSubview:self.verticle];
[self addSubview:self.horizontal];
[self addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpOutside];
}
return self;
}
#pragma mark - Actions
- (void)animate {
[UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:8 options:kNilOptions animations:^{
if (self.mini) {
self.transform = CGAffineTransformMakeScale(1.0, 1.0);
self.mini = NO;
} else {
self.transform = CGAffineTransformMakeScale(0.90, 0.90);
self.mini = YES;
}
} completion:nil];
}
#pragma mark - UIView
- (void)layoutSubviews {
CGSize size = self.bounds.size;
NSInteger width = 3;
NSInteger verticleInset = 12;
self.verticle.frame = CGRectMake((size.width - width) / 2, verticleInset, width, size.height - (verticleInset * 2));
self.horizontal.frame = CGRectMake(verticleInset, (size.height - width) / 2, size.width - (verticleInset * 2), width);
}
@end
推荐答案
将您的子视图的 userInteractionEnabled 设置为 NO 以让触摸通过它们并到达您的按钮.
Set userInteractionEnabled to NO for your subviews to let touches pass through them and onto your button.
还要确保将延迟加载器中 horizontal 属性的 _verticle.userInteractionEnabled = NO; 更改为 _horizontal.userInteractionEnabled = NO; 因为我认为这是一个错字.
Also make sure to change _verticle.userInteractionEnabled = NO; in your lazy-loader for your horizontal property to _horizontal.userInteractionEnabled = NO; as I believe that's a typo.
这篇关于如果将 UIView 添加为子视图,则 UIButton 目标不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)