簡體   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