【发布时间】:2016-03-24 11:24:51
【问题描述】:
我正在为 tvOS 编写游戏,该游戏需要使用触控板在屏幕上移动对象 - 而不是鼠标指针在 Mac 上移动。我遇到的问题是,在我的游戏中,UIViewController 没有接收到 touchesBegan 或 touchesMoved。在网上阅读了一些关于 touchesBegan not working on tvOS 的胡言乱语后,我写了一个小程序来测试这个理论
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
我的测试效果很好!我的问题是,什么会阻止 touchesBegan 或 touchesMoved 在我的完整应用程序中被 ViewController 接收(对于这个问题来说,它的来源太长了)? Spitball away - 我没有想法,我欢迎你提出任何建议!
【问题讨论】:
-
如您所见,touchesBegan 工作正常(如我的测试代码所示)。所以问题是,为什么它不能在完整的应用程序中工作?而且,如果 pressesBegan 是正确的,那么为什么没有 pressesMoved 呢?
-
作为记录,pressesBegan 也没有被传递给视图控制器。很奇怪。
标签: cocoa-touch tvos