• <bdo id='HFm7z'></bdo><ul id='HFm7z'></ul>
  • <tfoot id='HFm7z'></tfoot>
      <legend id='HFm7z'><style id='HFm7z'><dir id='HFm7z'><q id='HFm7z'></q></dir></style></legend>

        <i id='HFm7z'><tr id='HFm7z'><dt id='HFm7z'><q id='HFm7z'><span id='HFm7z'><b id='HFm7z'><form id='HFm7z'><ins id='HFm7z'></ins><ul id='HFm7z'></ul><sub id='HFm7z'></sub></form><legend id='HFm7z'></legend><bdo id='HFm7z'><pre id='HFm7z'><center id='HFm7z'></center></pre></bdo></b><th id='HFm7z'></th></span></q></dt></tr></i><div id='HFm7z'><tfoot id='HFm7z'></tfoot><dl id='HFm7z'><fieldset id='HFm7z'></fieldset></dl></div>
      1. <small id='HFm7z'></small><noframes id='HFm7z'>

      2. 如果将 UIView 添加为子视图,则 UIButton 目标不起作用

        时间:2023-07-30
          <legend id='lA7zb'><style id='lA7zb'><dir id='lA7zb'><q id='lA7zb'></q></dir></style></legend>
            • <small id='lA7zb'></small><noframes id='lA7zb'>

            • <i id='lA7zb'><tr id='lA7zb'><dt id='lA7zb'><q id='lA7zb'><span id='lA7zb'><b id='lA7zb'><form id='lA7zb'><ins id='lA7zb'></ins><ul id='lA7zb'></ul><sub id='lA7zb'></sub></form><legend id='lA7zb'></legend><bdo id='lA7zb'><pre id='lA7zb'><center id='lA7zb'></center></pre></bdo></b><th id='lA7zb'></th></span></q></dt></tr></i><div id='lA7zb'><tfoot id='lA7zb'></tfoot><dl id='lA7zb'><fieldset id='lA7zb'></fieldset></dl></div>

              <tfoot id='lA7zb'></tfoot>

                • <bdo id='lA7zb'></bdo><ul id='lA7zb'></ul>
                    <tbody id='lA7zb'></tbody>
                • 本文介绍了如果将 UIView 添加为子视图,则 UIButton 目标不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 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.

                  两个子视图都是带有 framebackgroundColor 的普通 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 目标不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:旋转 UIButton 360 度 下一篇:如何获取 Swift 3.0 中按钮的当前标题,ios 使用 sender.titleForState(.Normal)

                  相关文章

                  <small id='XlluM'></small><noframes id='XlluM'>

                  <legend id='XlluM'><style id='XlluM'><dir id='XlluM'><q id='XlluM'></q></dir></style></legend>

                • <i id='XlluM'><tr id='XlluM'><dt id='XlluM'><q id='XlluM'><span id='XlluM'><b id='XlluM'><form id='XlluM'><ins id='XlluM'></ins><ul id='XlluM'></ul><sub id='XlluM'></sub></form><legend id='XlluM'></legend><bdo id='XlluM'><pre id='XlluM'><center id='XlluM'></center></pre></bdo></b><th id='XlluM'></th></span></q></dt></tr></i><div id='XlluM'><tfoot id='XlluM'></tfoot><dl id='XlluM'><fieldset id='XlluM'></fieldset></dl></div>
                  • <bdo id='XlluM'></bdo><ul id='XlluM'></ul>
                • <tfoot id='XlluM'></tfoot>