I have an animation using a UIImageView:
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 1;
myAnimatedView.animation开发者_StackOverflow中文版RepeatCount = 1;
[myAnimatedView startAnimating];
How can I tell to animation to stop at the last frame (keeping the last image in the series visible)?
the image property on the UIImageView class has the following docs: "If the animationImages property contains a value other than nil, the contents of this property are not used."
So the trick to hold on the last frame of an animation in iOS4 is to first set the image property to that last frame (while animationImages is still nil), then set the animationImages property and call startAnimating. When the animation completes, the image property is then displayed. No callback/delegate needed.
You can set image of last frame to your UIImageView instance and then when animation will finish last frame image remains visible. Here is the code:
// create your array of images
NSArray *images = @[[UIImage imageNamed:@"video1.png"], [UIImage imageNamed:@"video2.png"]];
UIImageView *imageView = [UIImageView new];
imageView.image = images.lastObject;
imageView.animationImages = images;
imageView.animationDuration = 1.0; // seconds
imageView.animationRepeatCount = 1;
[imageView startAnimating];
After the animation finishes you can set your UIImageView image to the last frame of your animation.
myAnimatedView.image = [myAnimatedImages objectAtIndex:myAnimatedImages.count - 1]
I don't think there is a delegate method that would notify you on animationFinish event so you would need to start a NSTimer
with same duration as the animation to get the notification.
UIImageViewAnimation documentation
Swift version will be as given below but in objective-c logic will be same
you can hack it just setting the image
of the UIImageView
to the last image of the images array. just before the code of animation, like imageView.image = arrayOfImage.last
the whole logic is given below
imageView.image = arrayOfImage.last //(this line create magic)
imageView.animationImages = arrayOfImage
imageView.animationDuration = 1.0
imageView.animationRepeatCount = 1
imageView.startAnimating()
I just toss a UIView holding the first frame on top of the animation UIView... make the animation view hidden and set it's image as the last frame... and then have them switch hidden properties during the process.
so... just before your last line...
// quickly hide the view that only holds the first frame
myOtherView.hidden = YES;
// start the animation
[myAnimatedView startAnimating];
// show the animation view
myAnimatedView.hidden = NO;
// BAM, done. It will use the last image when it ends.
Less code, more IB, whenever possible.
I've achieved this by creating a subclass of UIImageView, overriding startAnimating() and setting the image property to be the last image in your image array before you call super.startAnimating(). This in addition to setting the animationRepeatCount to 1 achieves the desired effect.
Here is my Swift class:
class SuccessAnimationImageView: UIImageView {
var duration: NSTimeInterval = 2
override func didMoveToSuperview() {
super.didMoveToSuperview()
setupImages()
}
override func startAnimating() {
setLastFrame()
super.startAnimating()
}
func setupImages() {
animationImages = ImageHelper.successAnimationImages
image = animationImages?.first
animationRepeatCount = 1
}
func setLastFrame() {
image = animationImages?.last
}
}
Was having issues in start/stop animation & managing animation count So used this pod
imageViewObject.animate(withGIFNamed: "gif_name", loopCount: 1) {
print("animating image")
}
To start/stop animation :
imageViewObject.isAnimatingGIF ? imageViewObject.stopAnimatingGIF() : imageViewObject.startAnimatingGIF()
Have you set the removedOnCompletion property of the rotation animation to NO, e.g., rota.removedOnCompletion = NO;
That should leave the presentation layer where it was when the animation finished. The default is YES, which will snap back to the model value, i.e., the behavior you describe. The fillMode should also be set,
i.e., rota.fillMode = kCAFillModeForwards;
You can Stop the animation by setting the UIImageView property
animateImg.animationDuration = 1
精彩评论