UIControl未接收到触摸

Kyl*_*uth 20 iphone uitouch uicontrol ios

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

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

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

.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
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

bto*_*omw 74

tl; dr将UIControl的所有子视图设置为setUserInteractionEnabled:NO.UIImageView默认情况下将其设置为true.

原帖

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

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

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

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

  • 已经超过3年了.凯尔应该接受这个答案! (7认同)