xcode 如何快速在 UIImageView 中显示带有动画的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35084443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show image with animation in UIImageView in swift
提问by Joey Yi Zhao
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?
我正在使用 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;
});
}
回答by Vishnu gondlekar
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
更改 alpha 值以创建动画效果
回答by Cirec Beback
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.
从这个委托方法中访问动画循环似乎存在内部问题。我研究了几个选项,下面的解决方案dispatch_after()
是唯一对我的所有测试用例都有效的选项。
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
注意:我不是说这很漂亮或最终答案。只报告我的发现以及对我有用的方法。该UIView.animateWithDuration(1, delay:0, options:[.ShowHideTransitionViews]...
解决方案仅适用于 iOS8 而不是 iOS9。青年会
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()
}