2013-10-22 8 views
15

tintColor hayat kurtarıcıdır, uygulama temasını yepyeni (kolay) bir seviyeye taşır. Böyle basit bir şey için çekirdek grafikleri için oh çok cool.No ihtiyaçtır UIImageView'ın tonu, renk göreiOS 7'deki UIImageview öğesinin yeni tintColor özelliği görüntüleri canlandırmak için kullanılabilir mi?

//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; 

Yukarıdaki kod olacak "boya" resmin şeffaf olmayan parçalar.

Karşılaştığım sorun animasyonlar.

Yukarıdaki koddan devam edilmesi:

//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]; 

animasyon doğru uygulanan ancak görüntü yerine UIImageView'ın tintColor kendi özgün renk (gfx düzenleme uygulamada kullanılan diğer bir deyişle renk) kullanın.

Animasyonu kendim gerçekleştirmeye çalışıyorum (resimlerin içinden geçerken ve UIImageView öğesinin görüntü özelliğini bir NSTimer gecikmesiyle ayarlayarak insan gözünün yakalayabilmesi gibi).

Bunu yapmadan önce, UIImageView ürününün tintColor özelliğinin, onunla yapmaya çalıştığım şeyi desteklemesi gerekiyorsa, bunu animasyonlar için kullanıp kullanmayacağımı sormak istiyorum.

Teşekkürler. Muhtemelen bu işlemin üstesinden gelebilirsiniz.

+0

benim cevap denemek ve eğer yardımcı olur bakınız. –

+0

Bunun için bir çözüm buldunuz mu? – Alf

cevap

0

.tintColor Her zaman UButton'un setTitleColor yöntemi için NSTimers kullanıyorum. İşte bir örnek.

GÜNCELLEME: iPhone 5s iOS 7.1'de test edildi ve çalışıyor! görüntüleri kendimi animasyon yerine

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

Bu işe yaramaz, UIImage'da setTitleColor yok. –

+0

Bu hehe'yi paylaştığınız için teşekkür ederiz. Cevabımı yakında güncelleyeceğim. – Roecrew

+0

@Roecrew hala bekliyoruz – deej

2

, ben bir tonlama rengi kullanarak tek tek kareleri işlemek ve sonra UIImage animasyon yapalım karar verdi.

+ (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; 
} 

O da bir tonlama rengi alır dışında ("Image0", "image1" vb adlı dosyaları arayan dahil) sadece + [UIImage animatedImageNamed:duration:] gibi çalışır: Aşağıdaki yöntemlerle UIImage bir kategori oluşturdu. görüntü renklendirme kod sağlamak için bu cevaba

Teşekkür: https://stackoverflow.com/a/19152722/321527

+0

Harika bir fikir. Görüntülerin ve boyutların sayısına bağlı olarak küçük bir bellek ve CPU yoğun, böylece bunları oluşturma üzerine diske önbelleğe almak isteyebilirsiniz. –

İlgili konular