ios - Adding action to custom uiview -
i have been trying simple thing : adding action simple custom view. have looked on internet , found 2 "easy" solution :
uitapgesturerecognizer
uibutton
i want programmatically , need handle tap.
here code far, i've tried both solutions separately , , doesn't work !
.m
#import "areaview.h" @implementation areaview #define grey 27.0/255.0 #define pink_r 252.0/255.0 #define pink_g 47.0/255.0 #define pink_b 99.0/255.0 - (id) initwithframe:(cgrect)frame imagename:(nsstring *)imagename areaname:(nsstring *)areaname minimumspending:(int)minimumspending andcapacity:(int)capacity { self = [self initwithframe:frame]; if (self) { self.backgroundcolor = [uicolor colorwithred:grey green:grey blue:grey alpha:1]; self.userinteractionenabled=yes; //init variables _areaname=areaname; _capacity=capacity; _minimumspending=minimumspending; //image view _logoimageview = [[uiimageview alloc]initwithframe:cgrectmake(5, 4, 66, 50)]; //_logoimageview.image = [uiimage imagenamed:imagename]; _logoimageview.backgroundcolor = [uicolor graycolor]; //label _areanamelabel = [[uilabel alloc]initwithframe:cgrectmake(0, 54, 76, 18)]; _areanamelabel.textalignment = nstextalignmentcenter; _areanamelabel.textcolor = [uicolor whitecolor]; _areanamelabel.font = [uifont systemfontofsize:12.0]; _areanamelabel.text = areaname; //button _button = [[uibutton alloc]initwithframe:self.bounds]; _button.userinteractionenabled=yes; _button.backgroundcolor=[uicolor yellowcolor]; [_button addtarget:self action:@selector(handletap:) forcontrolevents:uicontroleventtouchupinside]; //tap gesture racognizer uitapgesturerecognizer *taprecognizer = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapped:)]; [taprecognizer setnumberoftapsrequired:1]; [taprecognizer setdelegate:self]; [self addgesturerecognizer:taprecognizer]; [self addsubview:_logoimageview]; [self addsubview:_areanamelabel]; [self addsubview:_button]; } return self; } -(void)handletap:(uibutton *)button { nslog(@"tapped!"); } -(void)tapped:(uitapgesturerecognizer *)recognizer { nslog(@"tapped!"); } @end
.h
#import <uikit/uikit.h> @interface areaview : uiview <uigesturerecognizerdelegate> @property (nonatomic) uiimageview *logoimageview; @property (nonatomic) uilabel *areanamelabel; @property (nonatomic) nsstring *areaname; @property (nonatomic) int minimumspending; @property (nonatomic) int capacity; - (id) initwithframe:(cgrect)frame imagename:(nsstring *)imagename areaname:(nsstring *)areaname minimumspending:(int)minimumspending andcapacity:(int)capacity; @property (nonatomic, strong) uibutton *button; @end
thanks help!
edit
the problem both handletap , tapped never fired if comment button solution or tap gesture 1 test them separately. button implementation, can see on interface clicking on nothing.
my uiview added programmatically several times (for several views) in uiscrollview.
edit 2
the problem more complicate that. custom view inside scrollview inside different custom view main function rewrite hittest, touches on view held scrollview. (here purpose of that).
it seems long hittest involved, doesn't work.
implement delegate method, you.
- (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldreceivetouch:(uitouch *)touch { id touchedview = gesturerecognizer.view; if ([touchedview iskindofclass:[uibutton class]]) { return no; //it won't invoke gesture method, it'll fire button method. } return yes; }
Comments
Post a Comment