UIView Touch Detection Using Interface Builder

How can I detect strokes in a UIviewController for a UIView with only code (without the Builder interface)?

I found the touchhesBegan method, but it is never called. I have not initialized anything else in this method.

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
+2
source share
4 answers
 #import "TouchView.h" //TouchView.h #import <Foundation/Foundation.h> #import "TouchViewDelegate.h" @interface TouchView : UIView { id <TouchViewDelegate> delegate; } @property (retain) id delegate; @end //TouchView.m @implementation TouchView @synthesize delegate; -(id) initWithFrame:(CGRect)frame { self.userInteractionEnabled = YES; return self; } -(id) initWithCoder:(NSCoder *)aDecoder { self.userInteractionEnabled = YES; return self; } -(void) awakeFromNib { self.userInteractionEnabled = YES; } -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [delegate touchDown:self]; } -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [delegate touchUp:self]; } @end //TouchViewDelegate.h #import <UIKit/UIKit.h> @protocol TouchViewDelegate -(void) touchDown:(id) sender; -(void) touchUp:(id)sender; @end 
+6
source

I would read the "Touch Events" sections of the iPhone Programming Guide.

UIViewController and UIView are UIResponders that are responsible for handling events.

To handle events, you need to override the touch * methods to do what you want. To find out what touch events have occurred, see UIEvent .

+1
source

In your init method:

 self.userInteractionEnabled = YES; 
0
source

Fix for madmik3,

you are missing super calls in your initWithFrame.

 -(id) initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.userInteractionEnabled = YES; } return self; } 
0
source

Source: https://habr.com/ru/post/909587/


All Articles