UIControl未接收触摸 [英] UIControl Not Receiving Touches

查看:146
本文介绍了UIControl未接收触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIControl实现了触摸开始方法,如下所示:

I have a UIControl which implements the touches began method like so:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    //More code goes here

UIControl的这个子类在视图控制器中实例化,然后将其作为子视图添加到该视图控制器。我在UIControl的touches begin方法中有一个断点,并且该方法永远不会被调用。我一直在做一些阅读,看起来View Controller有一些逻辑决定是否将触摸事件传递给它的子视图。奇怪的是,我在同一个视图控制器中有一个不同的UIControl子类,触摸事件在用户触摸时传递给它!
以下是完整代码:

This subclass of UIControl is instantiated in a view controller, it is then added as a subview to that view controller. I have a breakpoint at the touches began method of the UIControl, and the method never gets called. I've been doing some reading and it seems that the View Controller has some logic that decides whether to pass on touch events to its subviews. The strange thing is that I have a different subclass of UIControl in the same view controller, and the touch events get passed down to it when the user touches it! Here is the full code:

.h

#import <UIKit/UIKit.h>

@interface CustomSegment : UIView
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, assign) NSInteger segments;
@property (nonatomic, strong) NSArray *touchDownImages;
@property (nonatomic, readonly, assign) NSInteger selectedIndex;
@property (nonatomic, weak) id delegate;


- (id)initWithPoint:(CGPoint)point numberOfSegments:(NSInteger)_segments andTouchDownImages:(NSArray *)_touchDownImages;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end

.m

#import "CustomSegment.h"

@implementation CustomSegment
@synthesize bgImageView, segments, touchDownImages, selectedIndex, delegate;

- (id)initWithPoint:(CGPoint)point
   numberOfSegments:(NSInteger)_segments
           andTouchDownImages:(NSArray *)_touchDownImages  
{  
    self = [super initWithFrame:CGRectMake(point.x, point.y, [[_touchDownImages     objectAtIndex:0] size].width, [[touchDownImages objectAtIndex:0] size].height)];
if (self)
{
    touchDownImages = _touchDownImages;
    segments = _segments;
    bgImageView = [[UIImageView alloc] initWithImage:[touchDownImages objectAtIndex:0]];
    [self addSubview:bgImageView];
}
return self;
}

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    return YES;  
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //[super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    float widthOfSegment = [self frame].size.width / segments;
    float bottomPoint = 0;
    float topPoint = widthOfSegment;
    for (int i = 0; i < segments; i++)
    {
        if ([touch locationInView:self].x > bottomPoint && [touch locationInView:self].x < topPoint)
        {
            [bgImageView setImage:[touchDownImages objectAtIndex:i]];
            selectedIndex = i;
            return;
        }
        else
        {
            bottomPoint = topPoint;
            topPoint += topPoint;
        }
    }
}
@end


推荐答案

tl; dr将UIControl的所有子视图设置为 setUserInteractionEnabled:NO 。 UIImageView默认设置为true。

tl;dr Set all subviews of the UIControl to setUserInteractionEnabled:NO. UIImageView's by default have it set to true.

我最近发现的一件事是它有帮助如果UIControl的最顶层子视图具有 setUserInteractionEnabled:NO 。我到达这里是因为我有一个带有UIImageView的UIControl子类,因为它是唯一的子视图,并且工作正常。默认情况下,UIImageView的 userInteractionEnabled 设置为

One thing I found recently is that it helps if the top-most subview of the UIControl has setUserInteractionEnabled:NO. I arrived at this because I had a UIControl subclass with a UIImageView as it's only subview and it worked fine. UIImageView has userInteractionEnabled set to NO by default.

我还有另一个带有UIView的UIControl,因为它是最顶级的子视图(技术上在不同的状态下是相同的UIControl)。我相信UIView默认为 userInteractionEnabled == YES ,这排除了UIControl处理的事件。将UIView的 userInteractionEnabled 设置为解决了我的问题。

I also had another UIControl with a UIView as it's top most subview (technically the same UIControl in a different state). I believe UIView defaults to userInteractionEnabled == YES, which precluded the events being handled by the UIControl. Settings the UIView's userInteractionEnabled to NO solved my issue.

我不知道这是否是同一个问题,但也许这会有所帮助?

I don't know if it's the same issue here, but maybe that will help?

-
编辑:当我说最顶层的视图时......可能将UIControl的所有子视图设置为 setUserInteractionEnabled:NO

这篇关于UIControl未接收触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆