2016-03-22 2 views
0

160フレームのWKInterfaceImageアニメーションがあります。アニメーションは素晴らしいです。今私は色合いを追加したい。 This SO questionには、インスペクタで[テンプレートイメージとしてレンダリング]オプションを使用してウォッチキットティントメソッドが記述されていますが、単一の静的イメージでのみ機能するようです。最後のフレームだけが色づけされており、最後のフレームには、インスペクタの色合いの色が私のコードの色合いではないことが示されています。私は最初のフレームだけをレンダリングし、すべてのフレームを無駄にレンダリングしようとしました。アニメーションWKInterfaceImageのテンプレートイメージとしてレンダリング

startAnimatingWithImagesInRangeメソッド内ですべてをループしたり、範囲を設定したり、setTintメソッドを組み込んだりする必要がありますか?

enter image description here

rotateButtonImage.setImageNamed("frame") 

    rotateButtonImage.startAnimatingWithImagesInRange(NSRange(location: 0, length: 159), duration: 1, repeatCount: 1) 
    rotateButtonImage.setTintColor(UIColor.redColor()) 

編集:だから私がやった拡張機能を作成することです。それはこのように見えます。 awakeWithContextの私のカスタムVCで次に

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 
} 
} 

私は呼ば:

rotateButtonImage.image = rotateButtonImage.image.imageWithColor(UIColor.redColor()) 

しかし、物事が自動完了しないいくつかの理由。私のWKInterfaceImageはrotateButtonImageと呼ばれ、FoundationとWatchKitなどをインポートしました。

拡張機能の戻り値の型はWKInterfaceImageでなければなりませんか?私はそれらを変更しようとしましたが、多くのエラーがありました。

は、だから、私は

ですから、インスペクタのメソッドを使用する必要が笑WatchKitでは動作しません、この拡張子を見つけたと思います。しかし、まだ色合いは私のアニメーションで動作していません。これはバグかもしれないと思いますか?単一画像は、コードが有効であっても色合いを使用できますが、複数のフレームを使用することはできません。

答えて

1

setTintColorは、ここで説明するように、画像に単一の画像テンプレートが含まれている場合にのみ機能します。

https://developer.apple.com/library/ios/documentation/WatchKit/Reference/WKInterfaceImage_class/index.html#//apple_ref/occ/instm/WKInterfaceImage/setTintColor

しかし、実際には、animatedImageの色を変更する方法があります。それは演奏者ではなく、大きな画像セットでこれをやりたいとは思わない。あなたはその配列をこのように使用

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) 
+0

をうん私はすでに私のポストであることを説明しました。ありがとうございました。 – tymac

関連する問題