我正在爲tvOS編寫一款遊戲,需要使用觸控板在屏幕上移動對象 - 而不是像鼠標指針在Mac上移動一樣。我遇到的問題是,在我的遊戲中,UIViewController沒有收到touchesBegan或touchesMoved。看了網上的一些胡言亂語有關的touchesBegan不tvOS工作,我寫了一個小程序來測試這個理論TVOS UIViewController沒有收到touchesBegan或touchesMoved
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已離開 - 我不再有想法,我歡迎您提供任何建議!
我會建議先閱讀關於此[這裏]的tvOS文檔(https://developer.apple.com/library/tvos/documentation/General/Conceptual/AppleTV_PG/DetectingButtonPressesandGestures.html#//apple_ref/doc/ UID/TP40015241-CH16-SW1)。我想你可能想要pressBegan ... 此外,這已幾乎已被問[這裏](http://stackoverflow.com/questions/32516535/how-can-i-receive-touches-using-tvos ) – earthtrip
正如你所看到的,touchesBegan工作正常(我的測試代碼證明了這一點)。所以問題是,爲什麼它不能在完整的應用程序中工作?而且,如果pressBegan是正確的,那麼爲什麼沒有pressMoved呢? – headbanger
爲了記錄,pressBegan也沒有被傳遞給視圖控制器。奇怪的。 – headbanger