简体   繁体   中英

iOS Simulate Single Tap Gesture

I'm building an Augmented Reality app that when it scan single image, I want it automatically play video.

Currently, I have to tap a button on the image to play the video using single tap gesture method.

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

Now I want to simulate using NSNotification ,when the scan finish and the video is ready to play

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

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

But it will crash because I send it nil, how do I send the sender.state to the handleTap method since it expect sender.state=UIGestureRecognizerStateEnded .

Thanks.

You needn't test the state of UITapGestureRecognizer, its a discrete gesture, it will have only one state (UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded) when the handleTap method called.

So you can do it like this:

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

and simulate it:

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

In the .h file:

UITapGestureRecognizer *tapRecog;

and into .m file: in viewDidLoad :

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

and then:

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

then modify the the test function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM