简体   繁体   中英

How to show image with animation in UIImageView in swift

I am using swift to load a image from photo library. And add that image on a UIImageView. I want to show the image animated by running below code but it doesn't have any effect. The image is shown immediately after selection. Does anyone know what I should use in this case?

func imagePickerController(_picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : AnyObject]){
        imagePicker.dismissViewControllerAnimated(true, completion: nil);
        self.photoView.alpha = 0;
        self.photoView.image = info[UIImagePickerControllerOriginalImage] as? UIImage;
        UIImageView.animateWithDuration(1, animations: {
            self.photoView.alpha = 1;
        });
}

You can try out the following code

self.photoView.hidden = true;
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: { () -> Void in
     self.photoView.hidden = false
     }, completion: { (Bool) -> Void in    }
)

You can add view which you want to animate inside animations block.

EDIT

self.photoView.alpha = 0
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: { () -> Void in
     self.photoView.alpha = 1
     }, completion: { (Bool) -> Void in    }
)

Changing alpha value to creating animation effect

It almost seems like there is an internal issue with the animation loop being access from within this delegate method. I looked into a few options and the solution below dispatch_after() is the only option that worked for me for all my test cases.

Note: I'm not saying this is pretty or the final answer. Just reporting my findings and what works for me. The UIView.animateWithDuration(1, delay:0, options:[.ShowHideTransitionViews]... solution only worked for me on iOS8 not iOS9. YMMV

func imagePickerController(_picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : AnyObject]){
        _picker.dismissViewControllerAnimated(true, completion: nil)
        self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        self.imageView.alpha = 0

        let shortDelay = dispatch_time(DISPATCH_TIME_NOW, Int64(1_000))
        dispatch_after(shortDelay, dispatch_get_main_queue()) {
            UIView.animateWithDuration(1.0) {
                self.imageView.alpha = 1.0
            }
        }

//        // Does not work. No animation of alpha.
//            UIView.animateWithDuration(1.0) {
//                self.imageView.alpha = 1.0
//            }

//            // Does not work. No animation of alpha.
//            UIView.animateWithDuration(1.0) {
//                self.view.layoutIfNeeded()
//                self.imageView.alpha = 1.0
//            }
//            // Does not work. No animation of alpha.
//            UIView.animateWithDuration(1, delay:1, options:[], animations: {
//                self.imageView.alpha = 1
//            },  completion: nil)

//            // Works iOS8 Simulator. Does not work iOS9 Simulator (9.1) or device (9.2.1).
//            UIView.animateWithDuration(1, delay:0, options:[.ShowHideTransitionViews], animations: {
//                self.imageView.alpha = 1
//            }, completion: nil)

//            // Old school, does not work
//            UIView.commitAnimations()
//            UIView.beginAnimations("alphaAni", context: nil)
//            self.imageView.alpha = 1
//            UIView.commitAnimations()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM