iOS 7中UIImageView的新tintColor属性能用于动画图片吗?

15

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;

上述代码将根据UIImageview的色调颜色“涂”图像的非透明部分,这很酷。对于像这样简单的东西,不需要核心图形。

我面临的问题是动画。

继续上述代码:

//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。
我将尝试自己执行动画(通过过度操作,例如循环遍历图像并设置UIImageView的图像属性,并带有NSTimer延迟,以便人眼可以捕捉到它)。
在这样做之前,我想问一下,UIImageView的tintColor属性是否支持我要尝试的这种用途,即用于动画。
谢谢。

请尝试我的答案,看看是否有帮助。 - Léo Natan
你找到解决方案了吗? - Micky
2个回答

2

我决定不自己制作动画图像,而是使用着色渲染每个帧,然后让UIImage完成动画。我为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;
}

它的工作方式与+ [UIImage animatedImageNamed:duration:]相同(包括查找名为“image0”,“image1”等的文件),只是它还需要一个着色颜色。
感谢这个答案提供图像着色代码:https://dev59.com/Omcs5IYBdhLWcg3waTRD#19152722

不错的想法。根据图像数量和大小,可能会占用一些内存和 CPU,因此您可能希望在创建时将它们缓存到磁盘上。 - Léo Natan

0

.tintColor 可能可以处理它。我经常使用 NSTimers 来为 UIButton 的 setTitleColor 方法服务。这是一个例子。

更新:已在 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方法。 - Ben Collier
@Roecrew 我们还在等待。 - deej
@deej 已更新并且可以使用!抱歉让你等了这么久,呵呵。 - maelswarm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接