iphone - Identifying an UIImageView without tag -
i have generic generated imageviews in scrollview each of these images have 2 gesturerecognizer single/double tapping on imageview. problem how identify whether imageview tapped first or second time. in scenarios it's easy use tag of imageview, have 2 different gesturerecognizer on each imageview , every gesturerecognizer uses different identification method based on tag number, identify image.
here generate imageviews dynamically:
-(void) initlevels{ _level = [level alloc]; _unit = [unit alloc]; self->_units = [[nsmutablearray alloc] init]; _keys = [[nsmutablearray alloc] init] ; int x = 0; int y = 0; int = 0; (nsobject *object in self->_levels) { if ([object iskindofclass:_level.class] && != 0) { x = x + margin_right + object_width; y = 0; } else if ([object iskindofclass:_unit.class]){ _unit = (unit *) object; [self->_units addobject:_unit.description]; uiimageview *imageview = [[uiimageview alloc] initwithimage:self.box]; [imageview setframe:cgrectmake(x, y, object_width, box_height)]; imageview.highlighted = true; imageview.tag = i; //when not outlined gesturerecognizer singletapping works on other hand double tap gesturerecognizer works first object, because its' tag set on 0. uitapgesturerecognizer *doubletapgesture = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(unitdoubletapped:)]; doubletapgesture.numberoftapsrequired = 2; imageview.userinteractionenabled = yes; [imageview addgesturerecognizer:doubletapgesture]; uitapgesturerecognizer *singletapgesture = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(unitsingletapped:)]; singletapgesture.numberoftapsrequired = 1; //telling singletapgesture fail doubletapgesture, both doesn't fire @ same time [singletapgesture requiregesturerecognizertofail:doubletapgesture]; [imageview addgesturerecognizer:singletapgesture]; uilabel *labelheadline = [[uilabel alloc] initwithframe:cgrectmake(5, 2, 220, 20)]; [labelheadline setfont:[uifont boldsystemfontofsize:12]]; labelheadline.textalignment = nstextalignmentcenter; [labelheadline setbackgroundcolor:[[uicolor whitecolor] colorwithalphacomponent:0]]; labelheadline.text = _unit.headline; labelheadline.numberoflines = 0; [labelheadline sizetofit]; uilabel *labelprice = [labelutils deeplabelcopy:labelheadline withtext:[nsstring stringwithformat:@"price: %@",_unit.price] withframe:no]; [labelprice settextalignment:nstextalignmentleft]; [labelprice setframe:cgrectmake(labelheadline.frame.origin.x, labelheadline.frame.origin.y + labelheadline.frame.size.height + 2, 220, 20)]; uilabel *labelcrm = [labelutils deeplabelcopy:labelheadline withtext:[nsstring stringwithformat:@"crm: %@", _unit.crm] withframe:no]; [labelcrm settextalignment:nstextalignmentleft]; [labelcrm setframe:cgrectmake(labelprice.frame.origin.x, labelprice.frame.origin.y + labelprice.frame.size.height + 2, 220, 20)]; uitextview *textview= [[uitextview alloc] initwithframe:cgrectmake(0,0, object_width, object_height)]; [textview addsubview:labelheadline]; [textview addsubview:labelprice]; [textview addsubview:labelcrm]; [textview setuserinteractionenabled:no]; [textview seteditable:no]; textview.backgroundcolor = [[uicolor whitecolor]colorwithalphacomponent:0]; textview.textalignment = nstextalignmentleft; [imageview addsubview:textview]; [_scrollview addsubview:imageview]; y = y + margin_bottom + box_height; } [self->_keys addobject:[nsnumber numberwithint:i]]; i++; }//remove last keys in _keys array while ([self->_keys count] > ([self->_units count])) { [_keys removelastobject]; i--; } self.contents = [nsdictionary dictionarywithobjects:self->_units forkeys:_keys]; }
here code 2 gesture recognizer
-(void)unitdoubletapped:(uigesturerecognizer *)gesturerecognizer{ self->_unitviewfordoubletapidentification = (uiimageview *)gesturerecognizer.view; switch (self->_unitviewfordoubletapidentification.tag) { case 0: [_unitviewfordoubletapidentification sethighlightedimage:self.transparentbox]; self->_unitviewfordoubletapidentification.tag = 1; break; case 1: [_unitviewfordoubletapidentification sethighlightedimage:self.box]; self->_unitviewfordoubletapidentification.tag = 0; break; default: break; } }
and here singletap
- (ibaction)unitsingletapped:(id)sender { [self dismissallpoptipviews]; uigesturerecognizer *gesturerecognizer = [uigesturerecognizer alloc]; gesturerecognizer = (uigesturerecognizer *)sender; uiimageview *imageview = [[uiimageview alloc] init]; imageview = (uiimageview *)gesturerecognizer.view; if (sender == _currentpoptipviewtarget) { // dismiss poptipview , self.currentpoptipviewtarget = nil; } nsstring *contentmessage = nil; uiimageview *contentview = nil; nsnumber *key = [nsnumber numberwithint:imageview.tag]; id content = [self.contents objectforkey:key]; if ([content iskindofclass:[nsstring class]]) { contentmessage = content; } else { contentmessage = @"a large amount ot text in bubble\najshdjashdkgsadfhadshgfhadsgfkasgfdasfdhasdkfgaodslfgkashjdfg\nsjfkasdfgkahdjsfghajdsfgjakdsfgjjakdsfjgjhaskdfjadsfgjdsfahsdafhjajdskfhadshfadsjfhadsjlfkaldsfhfldsa\ndsfgahdsfgajskdfgkafd"; } nsarray *colorscheme = [_colorschemes objectatindex:foo4random()*[_colorschemes count]]; uicolor *backgroundcolor = [colorscheme objectatindex:0]; uicolor *textcolor = [colorscheme objectatindex:1]; cmpoptipview *poptipview; if (contentview) { poptipview = [[cmpoptipview alloc] initwithcustomview:contentview]; } else { poptipview = [[cmpoptipview alloc] initwithmessage:contentmessage]; } [poptipview presentpointingatview:imageview inview:self.view animated:yes]; poptipview.delegate = self; poptipview.disabletaptodismiss = yes; poptipview.preferredpointdirection = pointdirectionup; if (backgroundcolor && ![backgroundcolor isequal:[nsnull null]]) { poptipview.backgroundcolor = backgroundcolor; } if (textcolor && ![textcolor isequal:[nsnull null]]) { poptipview.textcolor = textcolor; } poptipview.animation = arc4random() % 2; poptipview.has3dstyle = (bool)(arc4random() % 2); poptipview.dismisstapanywhere = yes; [poptipview autodismissanimated:yes attimeinterval:3.0]; [_visiblepoptipviews addobject:poptipview]; self.currentpoptipviewtarget = sender; }
hope can me, in advance.
that's lot of code go through.. add more identifiers uiviews (ie other tag identifier) using obj-c runtime associative references..
so category attach uiview:
#import "uiview+addons.h" #import <objc/runtime.h> #define kanimationduration 0.25f @implementation uiview (addons) static char infokey; static char secondaryinfokey; -(id)info { return objc_getassociatedobject(self, &infokey); } -(void)setinfo:(id)info { objc_setassociatedobject(self, &infokey, info, objc_association_retain_nonatomic); } -(id)secondaryinfo { return objc_getassociatedobject(self, &secondaryinfokey); } -(void)setsecondaryinfo:(id)info { objc_setassociatedobject(self, &secondaryinfokey, info, objc_association_retain_nonatomic); } @end
here example of own code use above address problem similar 1 you're facing:
if (![[view info] boolvalue]) { // not selected? self.movetofoldernumber = [view secondaryinfo]; [view setinfo:@yes]; }
Comments
Post a Comment