将子视图添加到屏幕问题

时间:2013-09-16 17:03:11

标签: iphone ios animation uibutton addsubview

我在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];  }

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

由于

1 个答案:

答案 0 :(得分: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]; 
            });
        }
    });