以编程方式创建带有帧的UIImage动画



我正在生成一堆帧,我想在我的Apple Watch上做动画。

根据我在互联网上发现的信息,每个人似乎都在使用imageWithName来动画UIImage,例如以下SO帖子:是否有可能动画UIImage?

第一个答案建议使用以下代码UIImageView:

NSArray *animationFrames = [NSArray arrayWithObjects:
  [UIImage imageWithName:@"image1.png"],
  [UIImage imageWithName:@"image2.png"], 
  nil];
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = animationsFrame;
[animatedImageView startAnimating];

然而' UIImageView似乎不存在于XCode 7与Swift当我试图将其添加到我的Apple Watch代码?为什么?


我还尝试了下面我自己写的代码:

...
var frames = [UIImage]()
var animation = UIImage()
...
//1. generate frame
//2. add to array "frames"
animation.images = frames //apply frame array to main animatable UIImage.

最后一行给出了一个错误:

不能赋值给property: 'images'是一个get-only property

你应该使用

    UIImage.animatedImageWithImages(images: [UIImage], duration: NSTimeInterval)

创建您的动画UIImage

var animation = UIImage.animatedImageWithImages(images: frames, duration: NSTimeInterval)

最新更新