Animating UIImageView with colorWithPatternImage

岁酱吖の 提交于 2019-12-06 12:44:35

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!