Uitapgesturerecognizer Programmatically Trigger a Tap in My View

UITapGestureRecognizer Programmatically trigger a tap in my view

I think you have multiple options here:

  1. May be the simplest would be to send a push event action to your view but i don't think that what you really want since you want to be able to choose where the tap action occurs.

    [yourView sendActionsForControlEvents: UIControlEventTouchUpInside];

  2. You could use UI automation tool that is provided with XCode instruments. This blog explains well how to automate your UI tests with script then.

  3. There is this solution too that explain how to synthesize touch events on the iPhone but make sure you only use those for unit tests. This sounds more like a hack to me and I will consider this solution as the last resort if the two previous points doesn't fulfill your need.

How to trigger tap gesture recognizer of UIView programmatically

I solved this by creating fake touches. Triggering gesture was not possible as gestures does not expose its action and selector. But as I have UIView on which I want to tap. I can create a fake touch on that UIView. This fake touch will automatically trigger the tap gesture. This link was very helpful for this.

UITapGestureRecognizer not triggering on UIImageView created programmatically

i solved by applying gesture on view, instead of each image view separately.

[self.view addGestureRecognizer:tap];

and then in handler/selector guessTapObject function, i used this

CGPoint tapLocation = [gesture locationInView:self.view]; // gives the tap coordinates
for (UIView * view in self.view.subviews) { // gives view by iterating on each subview
CGRect dropRect = [[[view layer] presentationLayer] frame]; // specific way of getting the frame with respect to its layer

if(CGRectContainsPoint(dropRect, tapLocation)){ // checks for tap in the boundaries of view frame
if (view.tag > 900) // my specific view
// done what i want to do with that specific one
// as i removed it from my superview
}
}
}


Related Topics



Leave a reply



Submit