问题
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];
What I want now is to repeat the image to get a pattern. There is colorWithPatternImage, but that isn't made for animations.
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.
Is there any way?
回答1:
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.
来源:https://stackoverflow.com/questions/16461469/animating-uiimageview-with-colorwithpatternimage