ios - Trying swipe feature but the object I want to move is not there when I Run it (objective c) -
i'm sorry had ask this. i'm trying implement swipe feature 1 of image view image view follows finger. i'm trying make y coordinate doesn't change. i've been referencing tutorial
however in tutorial uses tableview, want similar using view controller. problem whenever run it, image view doesn't appear. shows white screen without object want able move finger.
sorry n00b way of asking. thank much.
swipeablecell.m
#import "swipeablecell.h" static cgfloat const kbouncevalue = 20.0f; @interface swipeablecell() <uigesturerecognizerdelegate> @property (nonatomic, weak) iboutlet uibutton *button1; @property (nonatomic, weak) iboutlet uibutton *button2; @property (nonatomic, weak) iboutlet uiview *mycontentview; @property (nonatomic, weak) iboutlet uilabel *mytextlabel; @property (nonatomic, strong) uipangesturerecognizer *panrecognizer; @property (nonatomic, assign) cgpoint panstartpoint; @property (nonatomic, assign) cgfloat startingrightlayoutconstraintconstant; @property (nonatomic, weak) iboutlet nslayoutconstraint *contentviewrightconstraint; @property (nonatomic, weak) iboutlet nslayoutconstraint *contentviewleftconstraint; @end @implementation swipeablecell - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { } return self; } - (void)awakefromnib { [super awakefromnib]; self.panrecognizer = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(panthiscell:)]; self.panrecognizer.delegate = self; [self.mycontentview addgesturerecognizer:self.panrecognizer]; } - (void)panthiscell:(uipangesturerecognizer *)recognizer { switch (recognizer.state) { case uigesturerecognizerstatebegan: self.panstartpoint = [recognizer translationinview:self.mycontentview]; self.startingrightlayoutconstraintconstant = self.contentviewrightconstraint.constant; break; case uigesturerecognizerstatechanged: { cgpoint currentpoint = [recognizer translationinview:self.mycontentview]; cgfloat deltax = currentpoint.x - self.panstartpoint.x; bool panningleft = no; if (currentpoint.x < self.panstartpoint.x) { //1 panningleft = yes; } if (self.startingrightlayoutconstraintconstant == 0) { //2 //the cell closed , opening if (!panningleft) { cgfloat constant = max(-deltax, 0); //3 if (constant == 0) { //4 [self resetconstraintcontstantstozero:yes notifydelegatedidclose:no]; } else { //5 self.contentviewrightconstraint.constant = constant; } } else { cgfloat constant = min(-deltax, [self buttontotalwidth]); //6 if (constant == [self buttontotalwidth]) { //7 [self setconstraintstoshowallbuttons:yes notifydelegatedidopen:no]; } else { //8 self.contentviewrightconstraint.constant = constant; } } } else { cgfloat adjustment = self.startingrightlayoutconstraintconstant - deltax; //1 if (!panningleft) { cgfloat constant = max(adjustment, 0); //2 if (constant == 0) { //3 [self resetconstraintcontstantstozero:yes notifydelegatedidclose:no]; } else { //4 self.contentviewrightconstraint.constant = constant; } } else { cgfloat constant = min(adjustment, [self buttontotalwidth]); //5 if (constant == [self buttontotalwidth]) { //6 [self setconstraintstoshowallbuttons:yes notifydelegatedidopen:no]; } else { //7 self.contentviewrightconstraint.constant = constant; } } } self.contentviewleftconstraint.constant = -self.contentviewrightconstraint.constant; //8 } break; case uigesturerecognizerstateended: if (self.startingrightlayoutconstraintconstant == 0) { //1 cgfloat halfofbuttonone = cgrectgetwidth(self.button1.frame) / 2; //2 if (self.contentviewrightconstraint.constant >= halfofbuttonone) { //3 [self setconstraintstoshowallbuttons:yes notifydelegatedidopen:yes]; } else { [self resetconstraintcontstantstozero:yes notifydelegatedidclose:yes]; } } else { cgfloat buttononeplushalfofbutton2 = cgrectgetwidth(self.button1.frame) + (cgrectgetwidth(self.button2.frame) / 2); //4 if (self.contentviewrightconstraint.constant >= buttononeplushalfofbutton2) { //5 //re-open way [self setconstraintstoshowallbuttons:yes notifydelegatedidopen:yes]; } else { //close [self resetconstraintcontstantstozero:yes notifydelegatedidclose:yes]; } } break; case uigesturerecognizerstatecancelled: if (self.startingrightlayoutconstraintconstant == 0) { [self resetconstraintcontstantstozero:yes notifydelegatedidclose:yes]; } else { [self setconstraintstoshowallbuttons:yes notifydelegatedidopen:yes]; } break; default: break; } } - (cgfloat)buttontotalwidth { return cgrectgetwidth(self.frame) - cgrectgetminx(self.button2.frame); } - (void)resetconstraintcontstantstozero:(bool)animated notifydelegatedidclose:(bool)notifydelegate { if (notifydelegate) { [self.delegate celldidclose:self]; } if (self.startingrightlayoutconstraintconstant == 0 && self.contentviewrightconstraint.constant == 0) { return; } self.contentviewrightconstraint.constant = -kbouncevalue; self.contentviewleftconstraint.constant = kbouncevalue; [self updateconstraintsifneeded:animated completion:^(bool finished) { self.contentviewrightconstraint.constant = 0; self.contentviewleftconstraint.constant = 0; [self updateconstraintsifneeded:animated completion:^(bool finished) { self.startingrightlayoutconstraintconstant = self.contentviewrightconstraint.constant; }]; }]; } - (void)setconstraintstoshowallbuttons:(bool)animated notifydelegatedidopen:(bool)notifydelegate { if (notifydelegate) { [self.delegate celldidopen:self]; } if (self.startingrightlayoutconstraintconstant == [self buttontotalwidth] && self.contentviewrightconstraint.constant == [self buttontotalwidth]) { return; } self.contentviewleftconstraint.constant = -[self buttontotalwidth] - kbouncevalue; self.contentviewrightconstraint.constant = [self buttontotalwidth] + kbouncevalue; [self updateconstraintsifneeded:animated completion:^(bool finished) { self.contentviewleftconstraint.constant = -[self buttontotalwidth]; self.contentviewrightconstraint.constant = [self buttontotalwidth]; [self updateconstraintsifneeded:animated completion:^(bool finished) { self.startingrightlayoutconstraintconstant = self.contentviewrightconstraint.constant; }]; }]; } - (void)updateconstraintsifneeded:(bool)animated completion:(void (^)(bool finished))completion { float duration = 0; if (animated) { duration = 0.1; } [uiview animatewithduration:duration delay:0 options:uiviewanimationoptioncurveeaseout animations:^{ [self layoutifneeded]; } completion:completion]; } #pragma mark - uigesturerecognizerdelegate - (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer { return yes; } - (void)prepareforreuse { [super prepareforreuse]; [self resetconstraintcontstantstozero:no notifydelegatedidclose:no]; } - (void)opencell { [self setconstraintstoshowallbuttons:no notifydelegatedidopen:no]; } @end
swipeablecell.h
#import <uikit/uikit.h> @protocol swipeablecelldelegate <nsobject> - (void)buttononeactionforitemtext:(nsstring *)itemtext; - (void)buttontwoactionforitemtext:(nsstring *)itemtext; - (void)celldidopen:(uitableviewcell *)cell; - (void)celldidclose:(uitableviewcell *)cell; @end @interface swipeablecell : uitableviewcell - (void)opencell; @property (nonatomic, strong) nsstring *itemtext; @property (nonatomic, weak) id <swipeablecelldelegate> delegate; @end
Comments
Post a Comment