在Objective C中的UIView中命中测试

时间:2018-07-29 05:39:52

标签: ios objective-c

在UIViewController中,我要添加一个名为testView的UIView

@property(nonatomic) UIView * testView;

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

        _puzzleView.translatesAutoresizingMaskIntoConstraints = NO;

  }
return testView

}

然后将那个View添加到ViewController。我想在testView中检测触摸。如何在ViewController中的UIView中添加hitTest方法

1 个答案:

答案 0 :(得分:0)

在ViewController中添加以下代码:

- (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
    }
 }