简体   繁体   English

UIView没有得到touchesbegan的称呼

[英]UIView not getting touchesbegan called

There are a number of other issues for this but none of the solutions seem to be working for me. 还有其他一些问题,但没有一个解决方案似乎对我有用。 I'm very new to iOS - currently working on a problem from the Big Nerd Ranch iOS Programming book. 我是iOS的新手 - 目前正在研究Big Nerd Ranch iOS编程书中的一个问题。

Most of the SOs I found said the issue ended up being userInteractionEnabled = YES was missing. 我发现的大多数SO都表示问题最终导致userInteractionEnabled = YES丢失。 Or the view's background was set to transparent . 或者视图的背景设置为transparent But removing the transparent background and setting userInteractionEnabled = YES did not result in the event firing. 但是删除透明背景并设置userInteractionEnabled = YES不会导致事件触发。 Any ideas what I'm missing? 我缺少什么想法?

AppDelegate.m: 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: 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;
}

Screenshot of app: 应用截图: 在此输入图像描述

Screenshot of view debugger 视图调试器的屏幕截图 在此输入图像描述

You're adding your BNRHypnosisView as a subview of the window. 您将BNRHypnosisView添加为窗口的子视图。 Later, when the window is about to appear on screen, it adds its root view controller's view as another subview, in front of your hypnosis view. 稍后,当窗口即将出现在屏幕上时,它会在催眠视图前面将其根视图控制器的视图添加为另一个子视图。 You can see this in the view hierarchy, where it shows a plain UIView after your BNRHypnosisView . 您可以在视图层次结构中看到这一点,它在BNRHypnosisView之后显示一个简单的UIView A view later in the list is “on top of” or “closer to the screen than” a view earlier in the list. 列表中稍后的视图是“在屏幕之上”或“靠近屏幕而不是”列表中较早的视图。

Try this: 试试这个:

- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM