我已经使用以下代码从我的主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方法没有接到电话,我要在哪里出错。任何帮助都很棒...
此答案将有助于Swift
在ViewDidload中启用您的customView
的用户互动 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