2012-04-17 6 views
4

を使用しているとき、私はUIViewbackgroundColorプロパティをアニメーション化するとき、私は次のコードスニペットを使用しています:のアニメーションのbackgroundColor colorWithPatternImage

[UIView beginAnimations:@"fade" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [[self view] setBackgroundColor: [UIColor greenColor]; 
    [UIView commitAnimations]; 

これが基本色(赤、緑、グレー、などとうまく動作します...)しかし、私はこのような色を使用しようとすると:[UIColor colorWithPatternImage:[UIImage imageNamed:@"img1.png"]];アニメーションは動作しません、ビューは新しい背景を設定しますが、希望のフェードアニメーションを設定しません。

UIView's backgroundColorにはcolorWithPatternImageを受け入れるようにアニメーションできますか?

ありがとうございます。

+2

だけ推測:view.layer.backgroundColorが実際にアニメーション化されこれにはCGColorデータが必要です。 「基本的な」色はこのプロパティを明確に定義していますが、colorWithPatternImageで作成された色はそうではありません。再び、私はちょうど推測しています。 –

+1

colorWithPatternImageの色のCGColorデータを取得する方法はありますか? – JAHelia

+0

わかりません:私は現時点ではWindowsコンピュータにあり、実際には何もテストできません。うまくいけばsomeneは前方に来る、そうでなければ仕事の後にいくつかのテストをしようとします。 –

答えて

2

//Function which will give you RGB color from your image..(copy and add this function in your code before calling it) 
- (UIColor*)getRGBAsFromImage: (UIImage*)image atX: (int)xx andY: (int)yy count: (int)count 
{ 
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; 

// First get the image into your data buffer 
CGImageRef imageRef = [image CGImage]; 
NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData = malloc(height * width * 4); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
              bitsPerComponent, bytesPerRow, colorSpace, 
              kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextRelease(context); 

// Now your rawData contains the image data in the RGBA8888 pixel format. 
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; 
UIColor *acolor; 
for (int ii = 0 ; ii < count ; ++ii) 
{ 
    CGFloat red = (rawData[byteIndex] * 1.0)/255.0; 
    CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0; 
    CGFloat blue = (rawData[byteIndex + 2] * 1.0)/255.0; 
    CGFloat alpha = (rawData[byteIndex + 3] * 1.0)/255.0; 
    byteIndex += 4; 

    acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 
    [result addObject:acolor]; 
} 

free(rawData); 
return acolor; 
} 


// Here, the code for animation that you want..use it and enjoy.. 
UIImage *image = [UIImage imageNamed:@"Black.png"]; 
    UIColor *color= [self getRGBAsFromImage:image atX:100 andY:100 count:1]; 
    [UIView beginAnimations:@"fade" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [[self view] setBackgroundColor: color]; 
    [UIView commitAnimations]; 

..私はそれをテスト...私はこの私のcode..useを変更していると、それは100%の仕事だこれはうまく働いています...これはあなたを助けることを願っています...

+0

申し訳ありませんが、動作しませんでした。4.3および5.0でテストされました。 – JAHelia

+0

5分前に4.1でテストしましたが、正常に動作しました... – Nit

+0

5または4.3でテストできますか? – JAHelia

2

私は

...アニメーションのために必要なステップで使用されるすべての色が始まるRGBの色と終了RGB色とコンポーネントRGB値を持つ色であることを推測していますが、画像を使用している場合はもちろん不可能のthats ...

ようにアルファを持つことフェードインのUIView第二温度と同様に、回避策を試みる= 0、α= 1 値変化...

関連する問題