用colorWithPatternImage动画UIImageView [英] Animating UIImageView with colorWithPatternImage
问题描述
我有一个 UIImageView
,它使用以下代码进行了动画处理:
I have a UIImageView
that is animated using the following code:
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for(int i = 1; i < 15; i++) {
NSString *str = [NSString stringWithFormat:@"marker_%i.png", i];
UIImage *img = [UIImage imageNamed:str];
if(img != nil) {
[imageArray addObject:img];
}
}
_imageContainer.animationImages = imageArray;
_imageContainer.animationDuration = 0.5f;
[_imageContainer startAnimating];
我现在想要的是重复图像以得到图案。有 colorWithPatternImage
,但这不是动画制作的。
What I want now is to repeat the image to get a pattern. There is colorWithPatternImage
, but that isn't made for animations.
我希望整个背景充满动画图案。
而不是使用非常大的图像(960x640),例如,我可以使用64x64的图像并将其重复以填充屏幕。
I want the whole background filled with a animated pattern. Instead of using a very large images (960x640) i could use a image of 64x64 for example and repeat that to fill the screen.
有什么办法吗? ?
推荐答案
将代码保持原样,而是使用 UIImageView
使用我的子类:
Leave your code as it is, but instead using UIImageView
use my subclass:
//
// AnimatedPatternView.h
//
#import <UIKit/UIKit.h>
@interface AnimatedPatternView : UIImageView;
@end
//
// AnimatedPatternView.m
//
#import "AnimatedPatternView.h"
@implementation AnimatedPatternView
-(void)setAnimationImages:(NSArray *)imageArray
{
NSMutableArray* array = [NSMutableArray arrayWithCapacity:imageArray.count];
for (UIImage* image in imageArray) {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0);
UIColor* patternColor = [UIColor colorWithPatternImage:image];
[patternColor setFill];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextFillRect(ctx, self.bounds);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[array addObject:img];
}
[super setAnimationImages:array];
}
@end
如果使用界面生成器,您只需要在身份检查器中设置图像视图的类即可。
If you create the view using interface builder you only need to set the class of the image view in the identity inspector.
这篇关于用colorWithPatternImage动画UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!