ios - Animations not working properly -
i present view controller modally in application. i'd user able "flick" view away gesture. wrote code below that:
- (void)handlepan:(uipangesturerecognizer *)recognizer { cgfloat elasticthreshold = 100; cgfloat dismissthreshold = 200; cgpoint translation = [recognizer translationinview:self.view]; cgfloat newy = 0; cgfloat translationfactor = 0.5; if (recognizer.state == uigesturerecognizerstateended) { if (translation.y < dismissthreshold) { newy = 0; } } else { if (translation.y > elasticthreshold) { cgfloat frictionlength = translation.y - elasticthreshold; cgfloat frictiontranslation = 30 * atan(frictionlength/120) + frictionlength/10; newy = frictiontranslation + (elasticthreshold * translationfactor); } else { newy = translation.y*translationfactor; } } if (translation.y > dismissthreshold) { [uiview animatekeyframeswithduration:0.5 delay:0.0 options:0 animations:^{ [uiview addkeyframewithrelativestarttime:0.0 relativeduration:0.5 animations:^{ self.overlay.effect = nil; self.collectionview.transform = cgaffinetransformmaketranslation(0, self.view.frame.size.height); }]; [uiview addkeyframewithrelativestarttime:0.1 relativeduration:0.1 animations:^{ self.pagecontrol.frame = cgrectmake((self.view.frame.size.width-200)/2, self.view.frame.size.height, 200, 20); }]; } completion:^(bool finished) { if (finished) { [self dismissviewcontrolleranimated:yes completion:nil]; } }]; } else { self.collectionview.transform = cgaffinetransformmaketranslation(0, newy); self.pagecontrol.transform = cgaffinetransformmaketranslation(0, (newy+self.collectionview.frame.size.height)-20); } }
this hooked uigesturerecognizer
:
self.pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)]; self.pan.delegate = self; self.pan.maximumnumberoftouches = 1; [self.view addgesturerecognizer:self.pan];
however, happens completion block executes immediately. you'll see view move down (because of dismissviewcontrolleranimated
) , @ same time see overlay.effect
go away. however, animations happen , view controller dismiss silently.
any ideas what's going wrong here?
this occurs because nesting uiview
animation blocks. should use dispatch_after
this:
double delayinseconds = 0.5; dispatch_time_t poptime = dispatch_time(dispatch_time_now, delayinseconds * nsec_per_sec); dispatch_after(poptime, dispatch_get_main_queue(), ^(void){ [self dismissviewcontrolleranimated:yes completion:nil]; });
Comments
Post a Comment