-1

我在 iPhone 应用程序的屏幕上添加了大量图像按钮。我的代码一切正常。

问题是在每个子视图都准备好显示之前,子视图不会显示。因此,例如我有 48 个子视图,我不想等到每个子视图都准备好后,我希望能够在加载时看到每个子视图出现在屏幕上。

我希望这是有道理的。随着我向集合中添加更多图像,问题变得越来越大。

这是我的代码供您查看:

for (int i = 0; i < imgNumbers.count; i++) {
    myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect buttonValue = [[btn9Locations objectAtIndex:location] CGRectValue];
    myButton.frame = buttonValue;
    NSString *imagePart1 = [[NSString alloc] initWithFormat:@"%@", imgNumbers[i]];

    [myButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDownRepeat];

    NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
    NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:imagePart1];

    NSString *imageLocation = [[NSString alloc]initWithFormat:@"%@", [fileURL path]];
    UIImage * image = [UIImage imageWithContentsOfFile:imageLocation];

    [myButton setBackgroundColor:[UIColor colorWithRed:0.94 green:0.39 blue:0.13 alpha:0.0]];
    [myButton setBackgroundImage:image forState:UIControlStateNormal];
    [myButton setTitle:[imageNames[i] uppercaseString] forState:UIControlStateNormal];
    [myButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:20]];
    [myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [myButton setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
    [myButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] forState:UIControlStateNormal];
    myButton.imageView.layer.cornerRadius = 7.0f;
    myButton.layer.shadowRadius = 3.0f;
    myButton.layer.shadowColor = [UIColor blackColor].CGColor;
    myButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    myButton.layer.shadowOpacity = 0.5f;
    myButton.layer.masksToBounds = NO;
    [buttons addObject:myButton];
    [scrollview addSubview:myButton];  }

希望这是有道理的,我已经看到应用程序,当您向下滚动时,每个项目都会淡入屏幕,但我似乎无法先对这部分进行排序!

谢谢

4

1 回答 1

3

尝试将初始化代码移动到后台队列中,并且只addSubview调用主队列。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        for (int i = 0; i < imgNumbers.count; i++) {
            myButton = [UIButton buttonWithType:UIButtonTypeCustom];
            CGRect buttonValue = [[btn9Locations objectAtIndex:location] CGRectValue];
            myButton.frame = buttonValue;
            NSString *imagePart1 = [[NSString alloc] initWithFormat:@"%@", imgNumbers[i]];

            [myButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDownRepeat];

            NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
            NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:imagePart1];

            NSString *imageLocation = [[NSString alloc]initWithFormat:@"%@", [fileURL path]];
            UIImage * image = [UIImage imageWithContentsOfFile:imageLocation];

            [myButton setBackgroundColor:[UIColor colorWithRed:0.94 green:0.39 blue:0.13 alpha:0.0]];
            [myButton setBackgroundImage:image forState:UIControlStateNormal];
            [myButton setTitle:[imageNames[i] uppercaseString] forState:UIControlStateNormal];
            [myButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:20]];
            [myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
            [myButton setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
            [myButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] forState:UIControlStateNormal];
            myButton.imageView.layer.cornerRadius = 7.0f;
            myButton.layer.shadowRadius = 3.0f;
            myButton.layer.shadowColor = [UIColor blackColor].CGColor;
            myButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
            myButton.layer.shadowOpacity = 0.5f;
            myButton.layer.masksToBounds = NO;
            [buttons addObject:myButton];
            dispatch_async(dispatch_get_main_queue(), ^{
               [scrollview addSubview:myButton]; 
            });
        }
    });
于 2013-09-16T17:17:10.850 回答