ios Swift - 如何为图像制作动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24364504/
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
Swift - How to animate Images?
提问by ChenSmile
I'm trying to animate images in particular time- duration. It is working fine in Objective C. However, it is not working for Swift, where is my mistake?
我正在尝试在特定时间段内为图像设置动画。它在 Objective C 中运行良好。但是,它不适用于 Swift,我的错误在哪里?
The code for Objective-C is -
Objective-C 的代码是 -
-(void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *imgListArray=[NSMutableArray array];
for (int i=0; i<=11; i++)
{
NSString *strImageName=[NSString stringWithFormat:@"c%d.png", i];
NSLog(@"%@",strImageName);
UIImage *image=[UIImage imageNamed:strImageName];
[imgListArray addObject:image];
}
self.imgView.animationImages = imgListArray;
self.imgView.animationDuration =1.0f;
[self.imgView startAnimating];
// Do any additional setup after loading the view, typically from a nib
}
The Code for swift is-
swift的代码是-
override func viewDidLoad()
{
super.viewDidLoad()
var imgListArray :NSMutableArray = []
for countValue in 1...11
{
var strImageName : String = "c\(countValue).png"
var image = UIImage(named:strImageName) // suggested by Anil
imgListArray.addObject(image)
}
// Swift code HERE for Objective c
}
回答by Anil Varghese
[UIImage imageNamed (strImageName)]
This not swift code. In swift it would be
这不是快速代码。很快就会
UIImage(named:strImageName)
Modified code:
修改后的代码:
var imgListArray :NSMutableArray = []
for countValue in 1...11
{
var strImageName : String = "c\(countValue).png"
var image = UIImage(named:strImageName)
imgListArray .addObject(image)
}
self.imageView.animationImages = imgListArray;
self.imageView.animationDuration = 1.0
self.imageView.startAnimating()
回答by Jeffrey Neo
for Swift 2, use [UIImage]
instead.
对于 Swift 2,请[UIImage]
改用。
var images: [UIImage] = []
for i in 1...2 {
images.append(UIImage(named: "c\(i)")!)
}
myImageView.animationImages = images
myImageView.animationDuration = 1.0
myImageView.startAnimating()
回答by Tiago Almeida
In swift you can go one step further and have a simple line to do this:
在 swift 中,您可以更进一步,并使用简单的一行来执行此操作:
let loadingImages = (1...11).map { UIImage(named: "c\(self.imageView.animationImages = loadingImages
self.imageView.animationDuration = 1.0
self.imageView.startAnimating()
)")! }
Then you can do the rest and inject this into an imageView
然后你可以做剩下的事情并将其注入到 imageView 中
func animate_images()
{
let myimgArr = ["1.jpg","2.jpg","3.jpg"]
var images = [UIImage]()
for i in 0..<myimgArr.count
{
images.append(UIImage(named: myimgArr[i])!)
}
imgView_ref.animationImages = images
imgView_ref.animationDuration = 0.04
imgView_ref.animationRepeatCount = 2
imgView_ref.startAnimating()
}
回答by Mili Shah
In swift 3 - Create Array of images and just animate that.
在 swift 3 - 创建图像数组并为其设置动画。
imgView_ref.stopAnimating()
And for stop animation just write
对于停止动画只需写
let images = [img1, img2, img3] // use of #imageLiterals here
let animation = UIImage.animatedImage(with: images, duration: 1)
self.myImageView.image = animation
回答by winklerrr
Swift 3+
斯威夫特 3+
UIImageViews
can animate images in two different ways (the other answers already cover the first way in deep):
UIImageViews
可以以两种不同的方式为图像制作动画(其他答案已经深入涵盖了第一种方式):
- Create an
[UIImage]
array with the images to animate - Set the
animationImages
property of the view with this array - Set the
animationDuration
property - Start the animation
- Create an
- Encapsulate the animation in an
UIImage
usinganimatedImage(with:duration:)
- Set the normal
image
property of the view
- Encapsulate the animation in an
[UIImage]
用要动画的图像创建一个数组animationImages
使用此数组设置视图的属性- 设置
animationDuration
属性 - 开始动画
- 将动画封装在
UIImage
using 中animatedImage(with:duration:)
- 设置
image
视图的 normal属性
- 将动画封装在
The following code uses #imageLiterals:
以下代码使用#imageLiterals:
func runAnimation (imgView: UIImageView, arrayPos: Int) {
let timingArray = [7.0,10.0] //Durations of each
let frameArray = [43,45] //Frames for each
var imgArray: Array<UIImage> = [] //Setup empty array
for i in 1 ... frameArray[arrayPos] {
//Fill empty array with images!!
let imageToAdd = UIImage(named: "animationNum_\(arrayPos)-frameNum_\(i)")
imgArray.append(imageToAdd!)
}
imgView.animationImages = imgArray
imgView.animationDuration = timingArray[arrayPos]
imgView.animationRepeatCount = 2
imgView.startAnimating()
}
Pro:
亲:
If you have to change the image of an UIImageView a lot and maybe one of those images should be animated, then you don't have to start/stop the animation every time anymore neither you need to set the animatedImages property back to nil
to display the image stored in the image property of the image view.
如果您必须大量更改 UIImageView 的图像,并且其中一个图像应该是动画的,那么您不必每次都开始/停止动画,也不需要将 animationImages 属性设置回nil
以显示图像存储在图像视图的图像属性中。
Con:
骗局:
You can't start/stop the animation, if it's encapsulated in a single UIImage
instead of an array.
如果动画封装在单个UIImage
而不是数组中,则无法启动/停止动画。
More on #imageLiterals
:
更多关于#imageLiterals
:
If you want to use an image literal, either type imageLiteral
or just type the name of an image from your assets folder and Xcode's code completion will suggest it.
如果您想使用图像文字,请输入imageLiteral
或仅输入您的资产文件夹中的图像名称,Xcode 的代码完成功能会给出建议。
Further reading:
进一步阅读:
- Another good blog postabout using
#imageLiterals
and#colorLiterals
with animations to follow.
- 另一个关于使用和跟随动画的好博客文章。
#imageLiterals
#colorLiterals
回答by Dave G
For anyone running multiple animations, depending on a variable, try the below.
对于根据变量运行多个动画的任何人,请尝试以下操作。
For example, you could run an animation sending 0 or 1 (maybe based on if your app knows its daytime or nighttime):
例如,您可以运行发送 0 或 1 的动画(可能基于您的应用程序是否知道其白天或夜间):
var counter = 1
var timer = NSTimer()
@IBOutlet weak var someImg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: Selector("doSomeAnimation"), userInfo: nil, repeats: true)
}
func doSomeAnimation() {
//I have four pngs in my project, which are named frame1.png ... and so on
if counter == 4 {
counter = 1
}else {
counter++
}
someImg.image = UIImage(named: "frame\(counter).png")
}
回答by Michel Go?i
another approach if you want to animate an array of images:
如果要为图像数组设置动画,请使用另一种方法:
##代码##Hope it helps!!
希望能帮助到你!!