如果将UIView添加为子视图,则UIButton目标无法正常工作 [英] UIButton targets not working if a UIView is added as a subview
问题描述
我有一个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
.
两个子视图都是普通的UIViews
,带有frame
和backgroundColor
.
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.
这是按钮的代码.
@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目标无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!