Swift 在动画WKInterfaceImage上渲染为模板图像

Swift 在动画WKInterfaceImage上渲染为模板图像,swift,watchkit,watchos-2,Swift,Watchkit,Watchos 2,我有一个160帧的WKInterfaceImage动画。动画很棒。现在我想添加一个色调。提到了使用检查器中的“渲染为模板图像”选项的WatchKit染色方法,但它似乎只适用于单个静态图像。它只对最后一帧进行着色,并且对最后一帧进行着色,这是我在检查器中的着色颜色,而不是我的代码着色。我尝试只渲染第一帧,渲染所有帧,但没有效果 我是否必须循环使用所有这些方法,或者设置一个范围,或者在StartAnimagingWithImagesRange方法中包含setTint方法 编辑:所以我做的是创建一个

我有一个160帧的WKInterfaceImage动画。动画很棒。现在我想添加一个色调。提到了使用检查器中的“渲染为模板图像”选项的WatchKit染色方法,但它似乎只适用于单个静态图像。它只对最后一帧进行着色,并且对最后一帧进行着色,这是我在检查器中的着色颜色,而不是我的代码着色。我尝试只渲染第一帧,渲染所有帧,但没有效果

我是否必须循环使用所有这些方法,或者设置一个范围,或者在StartAnimagingWithImagesRange方法中包含setTint方法

编辑:所以我做的是创建一个扩展。看起来像这样

WKImage+Tint.swift

extension UIImage {

func imageWithTintColor(colorTint : UIColor) -> UIImage {

    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
    colorTint.setFill()

    let context : CGContextRef = UIGraphicsGetCurrentContext()! as CGContextRef 
    CGContextTranslateCTM(context, 0, self.size.height)
    CGContextScaleCTM(context, 1.0, -1.0)
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    let rect : CGRect = CGRectMake(0, 0, self.size.width, self.size.height)
    CGContextClipToMask(context, rect, self.CGImage)
    CGContextFillRect(context, rect)

    let newImage : UIImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
}
}
然后在我的自定义VC中,
awakeWithContext
我调用:

rotateButtonImage.image = rotateButtonImage.image.imageWithColor(UIColor.redColor())
但由于某些原因,事情并不是自动完成的。我的WKInterfaceImage被称为“代码> RoTestButoObjyIs/CODE”,我已经导入了基础和WHOTEKIT等

我的扩展或函数返回类型应该是
WKInterfaceImage
类型吗?我试着改变这些,但却犯了很多错误

所以我想我发现这个扩展在WatchKit lol中不起作用


因此,您必须使用Inspector方法。但它仍然是色彩不工作,我的动画。我想这可能是个错误?单个图像可以使用色调,但即使代码有效,也可能不使用多个帧。

setTintColor仅在图像包含单个图像模板时才起作用,如下所述

:

但是,事实上,有一种方法可以重新制作动画图像。它没有性能,而且你不想用大量的图像来实现这一点

 static func animatedImagesWithColor(color: UIColor) -> [UIImage] {
    var animatedImages = [UIImage]()

    (0...60).forEach { imageNumber in
        if let img = UIImage(named: "MyImage\(imageNumber)") {
            UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale);

            let context = UIGraphicsGetCurrentContext()

            color.setFill()

            CGContextTranslateCTM(context, 0, img.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextClipToMask(context, CGRectMake(0, 0, img.size.width, img.size.height), img.CGImage);
            CGContextFillRect(context, CGRectMake(0, 0, img.size.width, img.size.height));

            animatedImages.append(UIGraphicsGetImageFromCurrentImageContext())
            UIGraphicsEndImageContext()

        }
    }

    return animatedImages
}
您可以这样使用该阵列:

let animation = UIImage.animatedImageWithImages(UIImage.animatedImagesWithColor(.redColor()), duration: 50)
let range = NSRange(location: 0, length: 60)

animationGroup.setBackgroundImage(animation)
animationGroup.startAnimatingWithImagesInRange(range, duration: 50, repeatCount: -1)
let animation = UIImage.animatedImageWithImages(UIImage.animatedImagesWithColor(.redColor()), duration: 50)
let range = NSRange(location: 0, length: 60)

animationGroup.setBackgroundImage(animation)
animationGroup.startAnimatingWithImagesInRange(range, duration: 50, repeatCount: -1)