【问题标题】:Adding subviews to screen issue将子视图添加到屏幕问题
【发布时间】:2013-09-16 17:03:10
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: iphone ios animation uibutton addsubview


    【解决方案1】:

    尝试将初始化代码移动到后台队列中,并且只在主队列上调用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]; 
                });
            }
        });
    

    【讨论】:

    • 感谢您的回复。但这使得屏幕时间增加了 5 倍,并且仍然同时出现。任何其他想法:)
    • 我一直在玩这个想法,我在每个按钮下都有标签,奇怪的是,如果我移动 dispatch_async(dispatch_get_main_queue(), ^{ 标签几乎立即出现但带有图像的按钮仍然比以前花费更长的时间。希望有人能帮助我......!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多