ios - Close UIView when UIButton is tapped -
in app have horizontal image gallery wherein when tap image, uiview called displaying enlarged images. uiview contains, scrollview, imageviews (dynamically created) , uibutton (close button) programmatically created. however, when tap close button uiview not dismissed. in viewdidload set gesturerecognizer.
[self.view addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(singletapaction:)]]; here's singletapaction method:
- (void)singletapaction:(uigesturerecognizer *)singletap_ { mtgallerypopup* galleryimage = [[[nsbundle mainbundle] loadnibnamed:@"mtgallerypopup" owner:self options:nil] lastobject]; if(galleryimage.hidden) { nslog(@"hidden"); [galleryimage dismisswithanimation:yes]; } else { nslog(@"not hidden"); [self showgallerydialog]; } } here's showgallerydialog method:
- (void)showgallerydialog { mtgallerypopup * galleryview = [[[nsbundle mainbundle] loadnibnamed:@"mtgallerypopup" owner:self options:nil] lastobject]; [galleryview setreferencevc:self]; [galleryview initwithtitle:@"image gallery"]; [galleryview setalpha:0.0f]; [self.view addsubview:galleryview]; [uiview beginanimations:nil context:nil]; [galleryview setalpha:1.0]; [uiview commitanimations]; } here's how creating uiview:
- (id)initwithtitle:(nsstring *)atitle { cgrect rect = [[uiscreen mainscreen] bounds]; // portrait bounds if (self = [super initwithframe:rect]) { cgrect screenrect = [[uiscreen mainscreen] bounds]; [self setbackgroundcolor:[uicolor colorwithred:0.0f green:0.0f blue:0.0f alpha:0.8f]]; self.scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0.0f, title_height, screenrect.size.width, 350)]; [self.scrollview setuserinteractionenabled:yes]; closebutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; [closebutton addtarget:self action:@selector(dismisswithanimation:) forcontrolevents:uicontroleventtouchupinside]; [closebutton settitle:@"x" forstate:uicontrolstatenormal]; [closebutton.layer setborderwidth:2]; [closebutton.layer setbordercolor:[uicolor whitecolor].cgcolor]; closebutton.layer.cornerradius=10; closebutton.layer.maskstobounds = yes; [closebutton settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal]; closebutton.titlelabel.font=[uifont systemfontofsize:16]; closebutton.frame=cgrectmake(self.frame.size.width-80, 8, 20, 20); [self addsubview:closebutton]; strurl = [nsstring stringwithformat:@"%@%@?temp_url_sig=%@&temp_url_expires=%lld", urlstorage, container_path, hmacstr, [@(floor([oneminlater timeintervalsince1970])) longlongvalue]]; [mutableurlstorage addobject:strurl]; [imagesarray addobject:[[uiimage alloc] initwithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:strurl]]]]; } cgsize pagesize = cgsizemake(item_width, self.scrollview.frame.size.height); _pgctr = [[uipagecontrol alloc] initwithframe:cgrectmake(0, screenrect.size.height-30, 320, 36)]; _pgctr.backgroundcolor=[uicolor graycolor]; int numberofpage=ceil((float)[imagesarray count]/2.5); _pgctr.numberofpages= numberofpage; self.scrollview.contentsize = cgsizemake(450*numberofpage, pagesize.height); int imgcurrentx = 10; (uiimageview* image in imagesarray) { @autoreleasepool { imageview = [[uiimageview alloc] initwithframe:cgrectmake(imgcurrentx, 45, 200, self.scrollview.frame.size.height - 10)]; imageview.image = (uiimage*)image; [imageview setuserinteractionenabled:yes]; [self.scrollview addsubview:imageview]; imgcurrentx = imgcurrentx + 220; } } [self addsubview:self.scrollview]; } return self; } when i'm on debug, happens goes singletap_ method , recreates again uiview (now 2 uiviews overlapping). doing wrong here?
any appreciated. thanks.
you created 2 objects of "mtgallerypopup" class. better way declare in ".h" file , initilise in viewdidload. per tap hide , show. hope solve problem
Comments
Post a Comment