ios - How to add UITapGestureRecognizer inside a UIView with the target set ```self``` -
i created customized uiview , added uitapgesturerecognizer inside this:
@interface customizedview : uiview @property(nonatomic,strong) uilabel* label; @end @implementation customizedview -(void) targetactionforthisview:(id)sender{ nslog(@"clicked!"); } -(void) commoninit{ _label = [[uilabel alloc] initwithframe:cgrectmake(0,0,200,100)]; _label.text = @"test string 1234"; uitapgesturerecognizer* g = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(targetactionforthisview:)]; g.enabled = yes; [self setexclusivetouch:yes]; [self setuserinteractionenabled:yes]; [self setmultipletouchenabled:yes]; [_label addgesturerecognizer:g]; [self addsubview:_label]; } -(instancetype)init{ self = [super init]; [self commoninit]; return self; } -(instancetype)initwithframe:(cgrect)frame{ self = [super initwithframe:frame]; [self commoninit]; return self; } @end
then add view controller:
-(void)viewdidload { [super viewdidload]; customizedview* aview = [[customizedview alloc] initwithframe:cgrectmake(0,0,300,300)]; [self.view addsubview:aview]; }
but gesture won't work. know target set view's controller. why doesn't work? , how can fix it?
change code this
-(void)actionviewtap:(id)sender{ nslog(@"clicked!"); } -(void) commoninit{ _label = [[uilabel alloc] initwithframe:cgrectmake(0,0,200,100)]; _label.text = @"test string 1234"; _label.userinteractionenabled = true;//enable userinteractionenabled uitapgesturerecognizer* g = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(actionviewtap:)];//here should actionviewtap: g.enabled = yes; [self setexclusivetouch:yes]; [self setuserinteractionenabled:yes]; [self setmultipletouchenabled:yes]; [_label addgesturerecognizer:g]; [self addsubview:_label]; } -(instancetype)init{ self = [super init]; [self commoninit]; return self; } -(instancetype)initwithframe:(cgrect)frame{ self = [super initwithframe:frame]; [self commoninit]; return self; }
Comments
Post a Comment