0

In a UIViewController I am adding a UIView named testView

@property(nonatomic) UIView * testView;

-(UIView *)testView{
    if (!_testView) {
        _testView = [UIView new];

        _puzzleView.translatesAutoresizingMaskIntoConstraints = NO;

  }
return testView

}

then I add that View to ViewController. I want to detect the touch in testView. How can I add the hitTest method in that UIView which is in a ViewController

  • check this https://stackoverflow.com/questions/18060365/hittestwithevent-and-subviews – R. Mohan Jul 29 '18 at 05:46
  • Simplest way is to put a `UITapGestureRecognizer` or any other `UIGestureRecognizer` and install it on the view you want. https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW8 – user9335240 Jul 29 '18 at 05:47

1 Answers1

0

In ViewController add follow code :

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    point = [self.testView.layer convertPoint:point fromLayer:self.layer]; 
    if ([self.testView.layer containsPoint:point]) {
        //touch on the testView
    }
 }
stack Stevn
  • 114
  • 6