gpt4 book ai didi

ios - 使用 colorWithPatternImage 为 UIImageView 设置动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:10:14 26 4
gpt4 key购买 nike

我有一个 UIImageView,它使用以下代码进行动画处理:

    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,但它不是为动画制作的。

我想要整个背景充满动画图案。我可以使用 64x64 的图像,而不是使用非常大的图像 (960x640),然后重复该图像以填满屏幕。

有什么办法吗?

最佳答案

保留您的代码,但使用 UIImageView 使用我的子类:

//
// 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

如果您使用界面生成器创建 View ,您只需在身份检查器中设置 ImageView 的类。

关于ios - 使用 colorWithPatternImage 为 UIImageView 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16461469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com