2014-04-21 111 views
1

我正在構建一個增強現實應用程序,當它掃描單個圖像時,我希望它自動播放視頻。iOS模擬單擊手勢

目前,我必須點擊圖像上的按鈕才能使用單擊手勢方法播放視頻。

- (void)handleTap:(UITapGestureRecognizer *)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) { 
     // handling code 
     CGPoint touchPoint = [sender locationInView:eaglView]; 
     [eaglView handleTouchPoint:touchPoint]; 
    } 
} 

現在我想用NSNotification,當掃描結束和視頻準備播放

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"singleTap" object:nil]; 

-(void)test 
{ 
    [self handleTap:nil]; 
} 

模擬但它會崩潰,因爲我把它爲零,我怎麼送sender.state到因爲它期望sender.state=UIGestureRecognizerStateEndedhandleTap方法。

謝謝。

回答

2

你不必測試UITapGestureRecognizer,其離散手勢的狀態下,將只有一個狀態(UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded)當handleTap方法調用。

所以,你可以做這樣的:

- (void)handleTap:(UITapGestureRecognizer *)sender { 
    // handling code 
    CGPoint touchPoint = [sender locationInView:eaglView]; 
    [eaglView handleTouchPoint:touchPoint]; 
} 

,並模擬它:

-(void)test 
{ 
    [self handleTap:nil]; 
} 
0

在.h文件:

UITapGestureRecognizer *tapRecog;

,併爲.m文件: in viewDidLoad

tapRecog = [UITapGestureRecognizer alloc] init]; 
[YourImage addObserver:self forKeyPath:@"singleTap" options:0 context:nil]; 
[YourImage addSubview:tapRecog]; 

然後:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if ([keyPath isEqualToString:@"singleTap"]) 
    { 
     [self test]; 
    } 
} 

然後修改的測試功能:

-(void)test 
{ 
    [self handleTap:tapRecog]; 
}