UIView子类没有收到touchesBegan,touchesEnded

时间:2015-10-21 14:53:06

标签: 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子类中没有调用上述函数。好奇,我开始了一个空白的项目,这个项目很短,我只是在这里发布代码。

main.m:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

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

这是AppDelegate。动态创建窗口和视图。

#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类。只是有触摸全部接触到了。

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.userInteractionEnabled = YES,我甚至试图给出视图第一响应者状态。似乎没有任何好处。

敏锐的观察者也会看到我在窗口上设置这些值:

window.userInteractionEnabled = YES;
window.multipleTouchEnabled = YES;

我有没有尝试过。没有骰子。我试过打电话

[super touchesBegan:touches withEvent:event]

[super touchesEnded:touches withEvent:event]

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

1 个答案:

答案 0 :(得分:4)

在应用程序运行时查看您的视图层次结构,使用recursiveDescription或使用Xcode 7的“调试视图层次结构”按钮。

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

子视图列表中稍后的视图“更接近”屏幕,因此首先接收触摸。通用UIViewController创建的视图填充窗口,位于SubView前面。因此,SubView无法接收到触摸。

Apple在五年前将rootViewController属性添加到了iOS 4.0中的UIWindow。通过战斗,你只会为自己制造麻烦。只需让rootViewController.view成为您窗口中的单个顶级视图,然后在其下添加子视图即可。没有人强迫您将任何其他视图控制器添加到您的应用程序。