2

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?

4

1 回答 1

3

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.

于 2013-05-11T15:56:59.400 回答