【问题标题】:touchesBegan caught by AppDelegate instead of subclass of UIViewtouchesBegan 被 AppDelegate 捕获,而不是 UIView 的子类
【发布时间】:2016-12-16 23:47:44
【问题描述】:

我正在阅读 The Big Nerd Ranch Guide 的 iOS 编程第 5 章(第 4 版,想要 Objective C),我按照关于子类化 UIView 类的说明,并在 AppDelegate 中添加了子视图,这件事是子视图没有捕捉到touchesBegan 事件, AppDelegate 捕捉到了信号。

AppDelegate 中的didFinishLaunchingWithOptions 方法:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:[UIViewController alloc]];

CGRect firstFrame = self.window.bounds;

HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
[self.window addSubview:firstView];
[firstView becomeFirstResponder];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

return YES;

HypnosisView 的两个初始化方法,UIView 的子类定义如下:

#import "HypnosisView.h"

@interface HypnosisView ()

@property (strong, nonatomic) UIColor *circleColor;

@end

@implementation HypnosisView

// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    CGRect bounds = self.bounds;
    CGRect frame = self.frame;

    // Figure out the center of the bounds rectangle
    CGPoint center;
     center.x = frame.origin.x + frame.size.width / 2.0;
    center.y = frame.origin.y + frame.size.height / 2.0;

    // The largest circle will circumscribe the view
    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
                 clockwise:YES];
    }

    // Configure line with to 10 points
    path.lineWidth = 10;

    // Configure the drawing color to light gray
    [self.circleColor setStroke];

    // Draw the line!
    [path stroke];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // All HypnosisViews start with a clear background color
        self.backgroundColor = [UIColor clearColor];
        self.circleColor = [UIColor lightGrayColor];

        self.userInteractionEnabled = YES;
    }
    return self;
}

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

【问题讨论】:

  • 如果代码来自书中,你不应该在这里发布。这是他们的知识产权。

标签: ios objective-c uiview appdelegate touchesbegan


【解决方案1】:

在你的Appdelegate.m中,你应该首先makeKeyAndVisible你的windowmakeKeyAndVisible会将window设置为keyWindow,并将window带到你所有@987654328的前面@。

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

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    CGRect firstFrame = self.window.bounds;

    HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
    [self.window addSubview:firstView];
    [firstView becomeFirstResponder];



    return YES;
}

【讨论】:

  • 谢谢!那行得通!但是为什么设置背景颜色的顺序会有所不同呢?
  • @TPWang 不是setting background color,而是[self.window addSubview:firstView];,请参阅我编辑的答案。
【解决方案2】:

UIView 对象通常不会对触摸事件做出反应。您是否将视图的 userInteractionEnabled 标志设置为 true?

没有被调用的 touchesBegan 方法在哪里?

【讨论】:

  • 我已经添加了应该调用但没有调用的 touchesBegan 函数,HypnosisView 没有获取触摸事件但在 AppDelegate 中调用了相同的函数,这是我不明白的。而且我确实将 userInteractionEnabled 设置为 YES,顺便说一句,现有的答案都没有帮助,我是初学者,但在我看来,窗口是如何在 AppDelegate 中启动的,你能看看吗?
  • 你没有显示这些东西(你的 touchesBegan 方法,或者将 userInteractionEnabled 设置为 true 的代码。)
  • 您的代码还将应用程序窗口的根视图控制器设置为您分配但从未初始化的视图控制器,这是错误的。你为什么要尝试直接在应用程序窗口中安装视图,并搞乱根视图控制器的设置方式?
  • 请向下滚动代码的第二部分。很抱歉把它放在那里。
  • 我一直在关注 Big Nerd Rand 的书,因为我不知道事情是如何正确完成的,但我很惊讶默认的 ViewController 没有参与其中
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多