UIView没有触发touchesbegan方法

3

有许多其他问题,但是似乎没有任何解决方案适用于我。我非常陌生iOS - 目前正在解决Big Nerd Ranch iOS编程书中的一个问题。

我找到的大部分SO表示问题最终是由于userInteractionEnabled = YES缺失引起的。或视图的背景设置为transparent。但是删除透明背景并将userInteractionEnabled = YES设置为不会导致事件触发。你们有什么想法我可能漏掉了什么?

AppDelegate.m:

#import "AppDelegate.h"
#import "BNRHypnosisView.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    CGRect firstFrame = self.window.bounds;

    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    firstView.userInteractionEnabled = YES;

    ViewController *controller = [[ViewController alloc] init];
    [self.window setRootViewController:controller];
    [self.window addSubview:firstView];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;
}

BNRHypnosisView.m:

#import "BNRHypnosisView.h"

@interface BNRHypnosisView()

@property (strong, nonatomic) UIColor *circleColor;

@end

@implementation BNRHypnosisView
-(void)drawRect:(CGRect)rect {
    CGRect bounds = self.bounds;

    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;

    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
    UIBezierPath *path = [[UIBezierPath alloc] init];

    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -=20) {
        [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];

        [path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
    }

    path.lineWidth = 10;

    [self.circleColor setStroke];

    [path stroke];
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.circleColor = [UIColor lightGrayColor];
    }
    return self;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%@ was touched", self);

    float red = (arc4random() % 100) / 100.0;
    float green = (arc4random() % 100) / 100.0;
    float blue = (arc4random() % 100) / 100.0;

    UIColor *randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

    self.circleColor = randomColor;
}

应用程序截图: 在此输入图片描述

视图调试器截图在此输入图片描述


你有没有其他的视图涵盖催眠视图?能否通过查看视图调试器来确认它! - Teja Nandamuri
嗯,这是我第一次使用视图调试器,我刚才发布的截图中的UIView是否实际上在催眠视图的前面? - Tom Hammond
这可能与幕后仍然有故事板有关吗? - Tom Hammond
我看到BNRHypnosisView在一个UIView下面?这是什么? - tuledev
BNRHypnosisView 是我正在尝试创建/接收 touchesBegan 事件的视图。它是绘制圆圈的视图,就像屏幕截图一样。我不知道那个 UIView 是从哪里来的。 - Tom Hammond
显示剩余2条评论
1个回答

6
你正在将BNRHypnosisView作为窗口的子视图添加。稍后,当窗口即将出现在屏幕上时,它会添加其根视图控制器的视图作为另一个子视图,在你的催眠视图前面。你可以在视图层次结构中看到这一点,在你的BNRHypnosisView之后显示了一个普通的UIView。列表中后面的视图比前面的视图“更靠近屏幕”或“更接近屏幕”。
尝试这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    ViewController *controller = [[ViewController alloc] init];
    [self.window setRootViewController:controller];

    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:controller.view.bounds];
    firstView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    firstView.userInteractionEnabled = YES;
    [controller.view addSubview:firstView];

    [self.window makeKeyAndVisible];
    return YES;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接