我有一个 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 UIViews
s 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 目标不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!