gpt4 book ai didi

iphone - UIControl 未接收到触摸

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

我有一个 UIControl,它实现了 touches begind 方法,如下所示:

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

UIControl 的这个子类在 View Controller 中实例化,然后作为 subview 添加到该 View Controller 。我在 UIControl 的 touches 开始方法处有一个断点,并且该方法永远不会被调用。我一直在做一些阅读,似乎 View Controller 有一些逻辑来决定是否将触摸事件传递给它的 subview 。奇怪的是,我在同一个 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

.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 的所有 subview 设置为 setUserInteractionEnabled:NO。 UIImageViews 默认设置为 NO

原帖

我最近发现的一件事是,如果 UIControl 的最顶层 subview 具有 setUserInteractionEnabled:NO,它会有所帮助。我之所以这样做是因为我有一个带有 UIImageView 的 UIControl 子类,因为它只是 subview 并且工作正常。 UIImageView 默认将 userInteractionEnabled 设置为 NO

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

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

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

关于iphone - UIControl 未接收到触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9350041/

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