【问题标题】:Animating UIImageView with colorWithPatternImage使用 colorWithPatternImage 动画 UIImageView
【发布时间】:2013-05-09 12:09:38
【问题描述】:

我有一个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),然后重复该图像来填充屏幕。

有什么办法吗?

【问题讨论】:

  • 你到底是什么意思重复得到一个模式?你想让你的背景动画吗?或者你想录制动画并重复它?你到底想做什么?
  • 我编辑了我的第一篇文章以提供更多细节。
  • 所以你想要colorWithPatternImage: 的标准功能但动画?那是一个艰难的。嗯。
  • 我想您可以重复该视图并根据需要多次推动它,并在每次通过时调整其位置。当然,问题是时机永远不会完美。

标签: ios objective-c animation uiimageview


【解决方案1】:

保持您的代码不变,而是使用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

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

【讨论】:

  • 不错!!!工作真的很容易。 png图像的透明度丢失了,现在变黑了,有什么办法解决这个问题吗? (不是很重要,但我以后可能需要它)。
  • @clankill3r 只需将 UIGraphicsBeginImageContextWithOptions 的第二个参数更改为 NO
猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
相关资源
最近更新 更多