2016-03-24 59 views
0

我正在爲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已離開 - 我不再有想法,我歡迎您提供任何建議!

+0

我會建議先閱讀關於此[這裏]的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

+0

正如你所看到的,touchesBegan工作正常(我的測試代碼證明了這一點)。所以問題是,爲什麼它不能在完整的應用程序中工作?而且,如果pressBegan是正確的,那麼爲什麼沒有pressMoved呢? – headbanger

+0

爲了記錄,pressBegan也沒有被傳遞給視圖控制器。奇怪的。 – headbanger

回答

1

您的示例代碼沒有任何手勢識別器,但您的完整應用程序可能會有。一些手勢可能導致touchesBegan:永遠不會被髮送到響應者鏈。

您應該重試完整的應用程序,但儘可能多地移除了手勢識別器,並且您可能會發現touchesBegan:方法開始被調用。

如果確定問題是實際上是一個姿態,那麼你可以得到手勢的問題,或通過設置delaysTouchesBeganNO的手勢設置cancelsTouchesInViewNO與響應鏈方式進行合作(雖然這應該已經是默認的)。

+0

這是一個很好的建議。但是我已經嘗試過了。沒有修復它。此刻,我懷疑一個觀點可能會阻止響應者鏈。 – headbanger

相關問題