Ok- so I have an application which subclasses UIView, and relies on touchesBegan and touchesEnded. I refer of course to these callbacks:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
I updated my iOS from 7 to 9 just the other day (I know, I'm lazy) to find that the touches aren't working. Turns out the above functions aren't getting called in my UIView subclass. Curious, I started a blank project, which is short enough that I'll just post the code here.
main.m:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
Here's AppDelegate. Create a window and a view dynamically.
#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
And here's the SubView class. Does just has the 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
Pretty simple. Create a window, create a SubView, add SubView to the window, set userInteractionEnabled. And yet, it doesn't work. I've looked at the following S.O. posts:
subviews are not receiving touches
iPhone SDK - Forwarding touches from a UIViewController to a subview
touchesBegan in UIView not being called
touchesBegan not called in UIView subclass
touchesBegan not called in a UIView subclass
They all say userInteractionEnabled = YES should fix it, or there might be view covering another view, or the view might have a transparent background. None of that applies here. I've set view.userInteractionEnabled = YES, and I've even tried to give the view first-responder status. None of it seems to do any good.
Astute observers will also see that I'm setting these values on the window itself:
window.userInteractionEnabled = YES;
window.multipleTouchEnabled = YES;
I've tried it with and without. No dice. I've tried calling
[super touchesBegan:touches withEvent:event]
and
[super touchesEnded:touches withEvent:event]
in those callbacks on SubView. No dice. Any help you can give me would be greatly appreciated. Thanks!