I'm writing a game for tvOS that requires an object to be moved around the screen using the trackpad - rather as the mouse pointer moves around on the Mac. The problem I'm having is that, in my game, UIViewController isn't receiving touchesBegan or touchesMoved. Having read some drivel on the net about touchesBegan not working on tvOS I wrote a little program to test this theory
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
CGPoint cursorLocation;
UIImageView* cursorImage;
}
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
cursorImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 92.0, 92.0)];
cursorImage.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds));
cursorImage.image = [UIImage imageNamed:@"Cursor"];
cursorImage.backgroundColor = [UIColor clearColor];
cursorImage.hidden = NO;
[self.view addSubview:cursorImage];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
cursorLocation = CGPointMake(-1, -1);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
if ((cursorLocation.x == -1) && (cursorLocation.y == -1)) {
cursorLocation = [touch locationInView:self.view];
} else {
float xDiff = location.x - cursorLocation.x;
float yDiff = location.y - cursorLocation.y;
CGRect rect = cursorImage.frame;
if ((rect.origin.x + xDiff >=0) && (rect.origin.x + xDiff <= self.view.frame.size.width)) {
rect.origin.x += xDiff;
}
if ((rect.origin.y + yDiff >=0) && (rect.origin.y + yDiff <= self.view.frame.size.height)) {
rect.origin.y += yDiff;
cursorImage.frame = rect;
cursorLocation = location;
}
}
}
@end
My test worked beautifully! My question is, what could be preventing touchesBegan or touchesMoved from being received by the ViewController in my full app (the source of which is far too long for this question)? Spitball away - I'm out of ideas and I'd welcome any suggestions that you might have!