在UIView类iOS目标c中不会调用触摸事件

问题描述 投票:0回答:1

我使用下面的代码从我的主viewcontroller编写了一个动态视图

ViewClass *View;

   View = [[ViewClass alloc] initWithFrame:CGRectMake(70, 100, 200, 200)];
   View.layer.cornerRadius = View.frame.size.width/2;
   View.layer.masksToBounds = YES;
   View.backgroundColor = [UIColor clearColor];
   View.userInteractionEnabled = YES;
   [self.view addSubview:View];

现在我正试图通过下面的片段从我的UIView类中获取触摸事件

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {

    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    locationOfTouch = [touch locationInView:self];
}

    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{

    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [[event allTouches] anyObject];
    locationOfTouch = [touch locationInView:self];
}

但是touchesBegan方法没有得到电话,我哪里出错了。任何帮助都会很棒......

ios uiview touchesbegan touchesmoved touch-events
1个回答
2
投票

这个答案将有助于迅速

在ViewDidLoad中为自定义视图启用用户交互

 yourCustomView.userInteractionEnabled = true

叫这个方法

 override func touchesBegan(touches: Set, withEvent event: UIEvent) {

     super.touchesBegan(touches, withEvent: event)

     let touch: UITouch = touches.first as! UITouch

     if (touch.view == yourCustomView){
     print("touchesBegan | This is an yourCustomView")
     }else{
     print("touchesBegan | This is not an yourCustomView")
     }

  }

希望这会有所帮助..! :d

© www.soinside.com 2019 - 2024. All rights reserved.