iphone - stopping UIImageView animation gradually and smoothly -
i have next simple uiimageview animation:
-(void) setuptheanimation { self.imgview.animationimages = imagesarr; [self.imgview setanimationrepeatcount:-1]; self.imgview.animationduration =0.9; [self.imgview startanimating]; [self performselector:@selector(stoptheanimation) withobject:nil afterdelay:4.0]; } -(void) stoptheanimation { [self.imgview stopanimating]; } but face problem when animation stops not know lastly frame stops @ !! ending of animation not smooth @ all.
so need to:
1) know lastly frame @ animation ends, set lastly image of animation , leads smooth stopping.
2) stop animation gradually, i.e. changing duration time before stops slow downwards first stop it.
this link sample project.
i know original implementation uses animationimages, don't know of way utilize animationimages directly continuously variable duration. however, pretty simple feature implement yourself. if so, can code dynamic duration value between images in array.
in next code, replace animationimages custom stepping function, , dynamically adjust duration after stop has been requested. take note little different original code, specified hard end time. code specifies when gear spin should start slowing down.
if have hard animation period, can adjust phone call stoptheanimation business relationship deceleration factor take (i increment duration 10% per step, during deceleration, until steps slower given threshold value):
// animation stops when step duration reaches value: #define stop_threshold_seconds 0.1f #define num_images 35 @implementation viewcontroller { nsmutablearray *imagesarr; int currentimage; bool stoprequested; nstimeinterval duration; } -(void) setuptheanimation { stoprequested = no; currentimage = 0; duration = 0.9f / num_images; [self stepthroughimages]; [self performselector:@selector(stoptheanimation) withobject:nil afterdelay:4.0]; } - (void) stepthroughimages { self.imgview.image = [imagesarr objectatindex: currentimage]; if (currentimage == num_images - 1) { currentimage = 0; } else { currentimage++; } if (stoprequested && duration < stop_threshold_seconds) { // we're slowing downwards gradually duration *= 1.1f; dispatch_time_t poptime = dispatch_time(dispatch_time_now, (int64_t)(duration * nsec_per_sec)); dispatch_after(poptime, dispatch_get_main_queue(), ^(void){ [self stepthroughimages]; }); } else if (!stoprequested) { dispatch_time_t poptime = dispatch_time(dispatch_time_now, (int64_t)(duration * nsec_per_sec)); dispatch_after(poptime, dispatch_get_main_queue(), ^(void){ [self stepthroughimages]; }); } } -(void) stoptheanimation { stoprequested = yes; } iphone ios objective-c uiimageview
No comments:
Post a Comment