繁体   English   中英

如何在UIImageView中快速显示带有动画的图像

[英]How to show image with animation in UIImageView in swift

我正在使用swift从照片库加载图像。 并将该图像添加到UIImageView上。 我想通过运行以下代码来显示动画图像,但没有任何效果。 选择后立即显示图像。 有谁知道在这种情况下我应该使用什么?

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;
        });
}

您可以尝试以下代码

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

您可以在动画块中添加要设置动画的视图。

编辑

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

更改Alpha值以创建动画效果

动画循环似乎是从此委托方法内部进行访问的一个内部问题。 我研究了一些选项, dispatch_after()下面的解决方案是适用于我所有测试用例的唯一选项。

注意:我并不是说这很漂亮,也不是最终答案。 只是报告我的发现以及对我有用的东西。 UIView.animateWithDuration(1, delay:0, options:[.ShowHideTransitionViews]...解决方案仅对我适用于iOS8,不适用于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()

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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