gpt4 book ai didi

ios - 如果将 UIView 添加为 subview ,则 UIButton 目标不起作用

转载 作者:IT王子 更新时间:2023-10-29 08:10:54 26 4
gpt4 key购买 nike

我有一个 UIButton。它有两个 subview 。然后我打电话:

[createButton addTarget:self action:@selector(openComposer) forControlEvents:UIControlEventTouchUpInside];

如果我点击 UIButton 中未被覆盖但它的 subview 之一的部分,它可以正常工作。但是,如果我改为点击其中一个 subview ,它不会触发操作。

在两个 subview 上,我都将 .userInteractionEnabled 设置为 YES

两个 subview 都是带有 framebackgroundColor 的普通 UIViews

我怎样才能让水龙头“穿过”UIView 并到达 UIButton

谢谢

编辑:我需要使用 UIControlEvents,因为我需要 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

最佳答案

为您的 subview 将 userInteractionEnabled 设置为 NO 以允许触摸通过它们并到达您的按钮。

还要确保将 _verticle.userInteractionEnabled = NO; 中的 horizo​​ntal 属性更改为 _horizo​​ntal.userInteractionEnabled = NO; 因为我认为这是一个打字错误。

关于ios - 如果将 UIView 添加为 subview ,则 UIButton 目标不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24814401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com