0

我想从 URL 加载图像,然后将它们分配为不同 UIButtons 的背景图像。当视图出现时,我需要这样做。

我的问题是,当我尝试使图像在加载时淡入时,动画直到所有图像都加载完毕后才开始。我认为这是因为动画代码已被读取,但在它有时间执行之前,程序开始加载新图像。

如何让图像一个接一个地淡入淡出?

以下代码用于获取图像(调用 downloadImageByPrice atURL),然后执行动画。

-(void) obtainImage:(int)i atURL:(NSString *)URLString{

    UIImage *image = [self downloadImageByPrice:i atURL:URLString];

    // Make images fade in when they have been found
    [[_buttonArray objectAtIndex:i] setAlpha:0.0];

    [[_buttonArray objectAtIndex:i] setImage:image forState:UIControlStateNormal];

    [UIView beginAnimations:@"fadeIn" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.8];

    [[_buttonArray objectAtIndex:i] setAlpha:1.0];
    [UIView commitAnimations];


}

此方法用于一个接一个地加载所有图像。我希望我的循环等到每个图像都出现后再开始加载下一个。

-(void) loadAllImagesAtURL:(NSString *)URLString{
    for(int i =0; i<[_buttonArray count];i++){
        [self obtainImage:i atURL:URLString];
    }    
}

我试过使用选择器,或者没有运气的完成^方法,但我对这些概念的理解仍然很低。

谢谢

4

1 回答 1

2

看一下这个:

视图控制器.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) NSMutableArray *buttonArray;
-(void) loadAllImagesAtURL:(NSString *)URLString;
- (UIImage *)downloadImageAtURL:(NSString *)urlString;
@end

视图控制器.m

#import "ViewController.h"
@implementation ViewController
@synthesize buttonArray = _buttonArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setButtonArray:[NSMutableArray array]];
    int margin = 20;
    int buffer = 8;
    int width  = 100;
    int height = 50;
    for (int i = 0; i < 4; i = i + 1) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setAlpha:0.0];
        [btn setFrame:CGRectMake(margin + ((width + buffer) * i), margin, width, height)];
        [btn setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
        [[self buttonArray] addObject:btn];
        [[self view] addSubview:btn];
    }
    [self loadAllImagesAtURL:@"https://www.google.com/images/srpr/logo3w.png"];
}
-(void) loadAllImagesAtURL:(NSString *)URLString {
    static int i = -1;
    i = i + 1;
    if (i < [[self buttonArray] count]) {
        UIImage *image = [self downloadImageAtURL:URLString];
        // Make images fade in when they have been found
        [[_buttonArray objectAtIndex:i] setImage:image forState:UIControlStateNormal];
        [UIView animateWithDuration:1.0
                         animations:^{
                             [[_buttonArray objectAtIndex:i] setAlpha:1.0];
                         } 
                         completion:^(BOOL finished) {

                             [self loadAllImagesAtURL:URLString];
                         }
         ];
    }
}
- (UIImage *)downloadImageAtURL:(NSString *)urlString {
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    return image;
}
@end

对我来说,这会创建 4 个按钮(最初是不可见的),在下载图像时会淡入。这是你想要的吗?

于 2012-05-08T22:29:45.353 回答