【问题标题】:Can the new tintColor property of UIImageview in iOS 7 be used for animating images?iOS 7 中 UIImageview 的新 tintColor 属性可以用于动画图像吗?
【发布时间】:2013-10-22 15:46:25
【问题描述】:

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

动画正确执行,但图像使用其原始颜色(即 gfx 编辑应用程序中使用的颜色)而不是 UIImageView 的 tintColor。

我将尝试自己执行动画(通过做一些超出顶部的操作,例如循环遍历图像并使用 NSTimer 延迟设置 UIImageView 的图像属性,以便人眼可以捕捉到它)。

在此之前,我想问一下 UIImageView 的 tintColor 属性是否应该支持我正在尝试用它做的事情,即将它用于动画。

谢谢。

【问题讨论】:

  • 请尝试我的回答,看看是否有帮助。
  • 您找到解决方案了吗?

标签: ios objective-c animation uiimageview tintcolor


【解决方案1】:

我决定使用淡色渲染各个帧,然后让 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://stackoverflow.com/a/19152722/321527

【讨论】:

  • 好主意。会占用少量内存和 CPU,具体取决于图像的数量和大小,因此您可能希望在创建时将它们缓存到磁盘。
【解决方案2】:

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

    }
}

【讨论】:

  • 这行不通,UIImage 上没有 setTitleColor。
  • @Roecrew 我们还在等待
  • @deej 已更新且有效!抱歉,拖了这么久嘿嘿。
猜你喜欢
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多