ios - UIView 子类没有接收到 touchesBegan、touchesEnded

标签 ios objective-c iphone cocoa-touch uiview

好的-所以我有一个子类 UIView 的应用程序,并依赖于 touchesBegan 和 touchesEnded。我当然指的是这些回调:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

就在前几天,我将我的 iOS 从 7 更新到了 9(我知道,我很懒),发现触摸不起作用。原来上面的函数没有在我的 UIView 子类中被调用。好奇,我开始了一个空白项目,它足够短,我将在这里发布代码。

主.m:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

这是 AppDelegate。动态创建窗口和 View 。
#import "AppDelegate.h"
#import "SubView.h"

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //create a window and give it a frame. makeKeyAndVisible
    CGRect rect = [[UIScreen mainScreen] bounds];
    self.window = [[UIWindow alloc] initWithFrame:rect];
    self.window.frame = [[UIScreen mainScreen] bounds];
    [self.window makeKeyAndVisible];

    //Create a subview. Make it a red square. make sure it has a background.
    //set the userInteractionEnabled and multipleTouchEnabled and for
    //good meausure call becomeFirstResponder.
    SubView *view = [[SubView alloc] initWithFrame:CGRectMake(100,100,100,100)];
    view.backgroundColor = [UIColor redColor];
    [self.window addSubview:view];
    view.userInteractionEnabled = YES;
    view.multipleTouchEnabled = YES;
    [view becomeFirstResponder];


    //Create default UIViewControllers because Objective-C fails if the windows don't have
    //root view controllers. I don't like being muscled into MVC but what are you gonna do
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for(UIWindow *window in windows) {
        if(window.rootViewController == nil){
            UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
            window.rootViewController = vc;
            window.userInteractionEnabled = YES;
            window.multipleTouchEnabled = YES;
        }
    }

return YES;
}

@end

这是 SubView 类。只是有touchesBegan touchesEnded。

subview .h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SubView : UIView {
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

@end

subview .m
#import "SubView.h"

@implementation SubView

//delegate method for touches began
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    //[super touchesBegan:touches withEvent:event];
    NSLog(@"touchesBegan!");

}


//delegate method for touches ending.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    //[super touchesEnded:touches withEvent:event];
    NSLog(@"touchesEnded!");
}

//just for good measure.
- (BOOL) canBecomeFirstResponder {
    return YES;
}

@end

很简单。创建窗口,创建SubView,将SubView添加到窗口,设置userInteractionEnabled。然而,它不起作用。我看过以下 S.O.帖子:

subviews are not receiving touches

iPhone SDK - Forwarding touches from a UIViewController to a subview

touchesBegan not responding

touchesBegan in UIView not being called

touchesBegan not called in UIView subclass

touchesBegan not called in a UIView subclass

他们都说 userInteractionEnabled = YES 应该修复它,或者可能有 View 覆盖另一个 View ,或者 View 可能具有透明背景。这些都不适用这里。我设置了 view.userInteractionEnabled = YES,我什至尝试给 View 提供第一响应者状态。这些似乎都没有任何好处。

精明的观察者也会看到我在窗口本身上设置了这些值:
window.userInteractionEnabled = YES;
window.multipleTouchEnabled = YES;

我试过有无。没有骰子。我试过打电话
[super touchesBegan:touches withEvent:event]


[super touchesEnded:touches withEvent:event]

在 SubView 上的那些回调中。没有骰子。您能给我的任何帮助将不胜感激。谢谢!

最佳答案

在应用程序运行时查看您的 View 层次结构,或者使用 recursiveDescription或者使用 Xcode 7 的“Debug View Hierarchy”按钮。

(lldb) po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]
<UIWindow: 0x7fc1f9d17b20; frame = (0 0; 414 736); gestureRecognizers = <NSArray: 0x7fc1f9d18910>; layer = <UIWindowLayer: 0x7fc1f9d16560>>
   | <SubView: 0x7fc1f9c04350; frame = (100 100; 100 100); layer = <CALayer: 0x7fc1f9c04730>>
   | <UIView: 0x7fc1f9c07500; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x7fc1f9c07fc0>>

subview 列表中后面的 View “更接近”屏幕,因此首先接收触摸。由通用 UIViewController 创建的 View 填满 window ,在你的SubView前面.因此,您的 SubView无法接收触摸。

苹果添加了rootViewController属性(property)给 UIWindow在 iOS 4.0 中——五年多以前。与它抗争,你只会给自己找麻烦。让rootViewController.view成为您窗口中的单个顶级 View ,并在其下添加您的 subview 。没有人会强制您将任何其他 View Controller 添加到您的应用程序中。

关于ios - UIView 子类没有接收到 touchesBegan、touchesEnded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262596/

相关文章:

iphone - Itunes Connect 的问题和需要澄清

iOS 应用程序外部主机

iOS 应用程序在后台执行任务

ios - 如何根据需要调整背景图像的大小

ios - 放大时使用 MKMapView 在 setUserTrackingMode 上崩溃

objective-c - 查找 nsindexset 中的第 n 个条目

iphone - iOS 6 多个 UIWebViews : Javascript only runs in one

ios - 自定义 UIView 不显示

ios - 为特征改变用户定义的运行时属性

iphone - 使用 AFJSONRequestOperation