2013-10-22 1 views
15

tintColorは生命の救済者であり、全く新しい(簡単な)レベルのテーマになっています。そのような単純なもののためのコアグラフィックスのためのああそうcool.No必要があるUIImageviewの色合いの色に応じてiOS 7のUIImageviewの新しいtintColorプロパティを画像のアニメーションに使用できますか?

//the life saving bit is the new UIImageRenderingModeAlwaysTemplate mode of UIImage 
UIImage *templateImage = [[UIImage imageNamed:@"myTemplateImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
imageView.image = templateImage; 

//set the desired tintColor 
imageView.tintColor = color; 

上記のコードは意志「ペイント」画像の不透明部分。

私が直面する問題は、アニメーションにあります。

上記のコードの続き:

//The array with the names of the images we want to animate 
NSArray *imageNames = @[@"1",@"2"@"3"@"4"@"5"]; 

//The array with the actual images 
NSMutableArray *images = [NSMutableArray new]; 
for (int i = 0; i < imageNames.count; i++) 
{ 
    [images addObject:[[UIImage imageNamed:[imageNames objectAtIndex:i]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]]; 
} 

//We set the animation images of the UIImageView to the images in the array 
imageView.animationImages = images; 

//and start animating the animation 
[imageView startAnimating]; 

アニメーションが正しく行われているが、画像ではなく、UIImageViewのtintColorの元の色(GFX編集アプリケーションで使用される、すなわち色)を使用します。

私はアニメーションを自分自身で実行しようとしています(イメージをループし、UTImageViewのイメージプロパティをNSTimerディレイで設定して、人間の目でそれをキャッチできるようにします)。

これを行う前に、UIImageViewのtintColorプロパティが、私がそれを使ってやっていることをサポートしているかどうか、つまりアニメーションに使用するかどうかを質問したいと思います。

ありがとうございました。

+0

私の答えを試してみて、それが助けかどうかを確認してください。 –

+0

解決方法を見つけましたか? – Alf

答えて

0

.tintColorはおそらくそれを処理できます。 UIButtonのsetTitleColorメソッドにNSTimersを使用しています。ここに例があります。

UPDATED:iPhone 5s iOS 7.1で動作確認済み!画像を自分でアニメーション化するのではなく

- (void)bringToMain:(UIImage *)imageNam { 

    timer = [NSTimer scheduledTimerWithTimeInterval:.002 
              target:self 
              selector:@selector(animateTint) 
              userInfo:nil 
              repeats:YES]; 
} 

- (void)animateTint { 

    asd += 1.0f; 

    [imageView setTintColor:[UIColor colorWithRed:((asd/100.0f) green:0.0f blue:0.0f alpha:1.0f]]; 

if (asd == 100) { 

     asd = 0.0f 

     [timer invalidate]; 

    } 
} 
+1

これはうまくいきません。UIImageにsetTitleColorはありません。 –

+0

このheheを共有していただきありがとうございます。私はすぐに私の答えを更新します。 – Roecrew

+0

@Roecrew私たちはまだ待っています – deej

2

は、私は、色合いのカラーを使用して、個々のフレームをレンダリングしてからUIImageアニメーションをやらせることにしました。

+ (instancetype)animatedImageNamed:(NSString *)name tintColor:(UIColor *)tintColor duration:(NSTimeInterval)duration 
{ 
    NSMutableArray *images = [[NSMutableArray alloc] init]; 

    short index = 0; 
    while (index <= 1024) 
    { 
     NSString *imageName = [NSString stringWithFormat:@"%@%d", name, index++]; 
     UIImage *image = [UIImage imageNamed:imageName]; 
     if (image == nil) break; 

     [images addObject:[image imageTintedWithColor:tintColor]]; 
    } 

    return [self animatedImageWithImages:images duration:duration]; 
} 

- (instancetype)imageTintedWithColor:(UIColor *)tintColor 
{ 
    CGRect imageBounds = CGRectMake(0, 0, self.size.width, self.size.height); 

    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(context, 0, self.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextClipToMask(context, imageBounds, self.CGImage); 

    [tintColor setFill]; 
    CGContextFillRect(context, imageBounds); 

    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return tintedImage; 
} 

それは、それはまた、色合いの色を取ることを除いて(「image0」、「画像1」、などという名前のファイルを探し含む)だけ+ [UIImage animatedImageNamed:duration:]ように動作します:私は、次の方法でUIImageのカテゴリを作成しました。画像着色コードを提供するためのこの回答へ

ありがとう:https://stackoverflow.com/a/19152722/321527

+0

すばらしいアイデア。イメージとサイズの数に応じて、メモリとCPUの使用量が少なくなるため、作成時にディスクにキャッシュすることができます。 –

関連する問題