I have programmed a dynamic view from my main viewcontroller using below code
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];
Now i am trying to get touch events from my UIView class using below snippets
-(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];
}
but touchesBegan method does not get call, where am i going wrong. Any help would be great...
This answer will help in swift
In ViewDidLoad enable userInteraction for yourCustomView
yourCustomView.userInteractionEnabled = true
call this method
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")
}
}
Hopefully this will help..! :D