ios - How to Display button in Collection view cell when user uses a Long press gesture. -



ios - How to Display button in Collection view cell when user uses a Long press gesture. -

well have been struck past 2 days , havent found solution where. working collection create button dynamically on each collection cell , hided it, when user uses long press gesture button should displayed in each , every cell. how should implement this. ideas thankful.

- (void)handlelongpress:(uilongpressgesturerecognizer*)gesturerecognizer { if (gesturerecognizer.state == uigesturerecognizerstateended) { userbuttonoutlet.hidden=yes; savebuttonoutlet.hidden=no; secondsleft = minutes = seconds = 0; if (timerr) { [timerr invalidate]; } timerr = [nstimer scheduledtimerwithtimeinterval:1.0f target:self selector:@selector(updatecounter:) userinfo:nil repeats:yes]; secondsleft=1800; cancelsessionbuttonoutlet.hidden=no; // [collectiondata reloaddata]; //do whatever want on end of gesture } else if(gesturerecognizer.state == uigesturerecognizerstatebegan) { nslog(@"uigesturerecognizerstatestarted"); } } - (ibaction)cancelsessionbutton:(id)sender { nslog(@"test"); cancelsessionbuttonoutlet.hidden=yes; userbuttonoutlet.hidden=no; savebuttonoutlet.hidden=yes; mycounterlabel.text=nil; secondsleft=0; nslog(@"the cancel session seconds left %d", secondsleft); [self stoptimer:nil]; } -(nsinteger)collectionview:(uicollectionview *)view numberofitemsinsection:(nsinteger)section; { homecoming 25; } - (nsinteger)numberofsectionsincollectionview: (uicollectionview *)collectionview { homecoming 1; } -(void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath { nslog(@"item clicked"); cgrect rect = [collectionview layoutattributesforitematindexpath:indexpath].frame; uistoryboard* storyboard = [uistoryboard storyboardwithname:@"main" bundle:nil]; uiviewcontroller* vc = [storyboard instantiateviewcontrollerwithidentifier:@"note"]; pc = [[uipopovercontroller alloc] initwithcontentviewcontroller:vc]; [pc presentpopoverfromrect:rect inview:collectionview permittedarrowdirections:uipopoverarrowdirectionany animated:yes]; nslog(@" index path %@", indexpath); } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { // we're going utilize custom uicollectionviewcell, hold image , label cell *mycell = [collectionview dequeuereusablecellwithreuseidentifier:@"tablecell" forindexpath:indexpath]; if ((long)indexpath.row < 20 || (long)indexpath.row == 22 ) { nslog(@"case"); mycell.roomlabel.text = [nsstring stringwithformat:@"{%ld,%ld}", (long)indexpath.row, (long)indexpath.section]; nslog(@"index path %lu" , (long)indexpath.row ); mycell.subjectlabel.text =@"english"; mycell.cellview.backgroundcolor= [uicolor graycolor]; button=[[uibutton alloc]init]; [button setframe:cgrectmake(5,5,10,23)]; [button settitle:@"x" forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(buttonpressed:) forcontrolevents:uicontroleventtouchupinside]; [button sethidden:no]; [mycell addsubview:button]; } else { mycell.userinteractionenabled = no; } homecoming mycell; }

how create bool flag say:

@interface myviewcontroller { bool shoulddisplaybutton; } @end

then add together long press gesture entire uicollectionview:

-(void)viewdidload { [super viewdidload]; ... uilongpressgesturerecognizer *longpress = [[uigesturerecognizer alloc] initwithtarget:self action:@selector(handlelongpress:)]; ... [self.mycollectionview addgesturerecognizer:longpress]; } -(void)handlelongpress:(uilongpressgesturerecognizer *)longpressgesture { // ------------------------------------------------------------------- // want trigger if user allow go of long press // otherwise, long press continuously execute block // ------------------------------------------------------------------- if(longpressgesture.state == uigesturerecognizerstateended) { // ---------------------------------------------------------------- // toggles boolean flag on , off reload // collection view. see collectionview cellforrowatindex // enable button based on value of boolean. // ---------------------------------------------------------------- if(shoulddisplaybutton) { shoulddisplaybutton = no; } else { shoulddisplaybutton = yes; } [self.mycollectionview reloaddata]; } } - (uicollectionviewcell *)collectionview:(uicollectionview *)cv cellforitematindexpath:(nsindexpath *)indexpath { .... button=[[uibutton alloc]init]; [button setframe:cgrectmake(5,5,10,23)]; [button settitle:@"x" forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(buttonpressed:) forcontrolevents:uicontroleventtouchupinside]; [mycell addsubview:button]; // --------------------------------------------------------------------- // here hide button if bool flag no, otherwise, // show button. // --------------------------------------------------------------------- if(shoulddisplaybutton) { [button sethidden:no]; } else { [button sethidden:yes]; } }

does help?

ios objective-c uicollectionview

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -