加载视图延迟

时间:2015-07-30 17:08:34

标签: ios uiviewcontroller lag

对于不明确的问题道歉。这里有固定的答案:

在我的初始视图控制器中,我有两个圆形按钮。以前是UIImage视图+手势识别器(出于ux原因),当我进行原型设计时,它在iPhone 4及更高版本上运行良好。 现在我用我在编程方式创建的正确的圆形UI按钮替换UIImage +手势,如我在viewDidLoad中调用的方法:

self.featureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_featureButton setBackgroundImage:[UIImage imageNamed:@"FeaturedArtistImage.jpg"] forState:UIControlStateNormal];
_featureButton.layer.cornerRadius = size/2;
_featureButton.frame =CGRectMake(self.view.frame.size.width/2-size/2, 50, size, size);
_featureButton.clipsToBounds = YES;
_featureButton.layer.borderColor=[UIColor whiteColor].CGColor;
_featureButton.layer.borderWidth=5.0f;

[_featureButton addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside]; 
[self.view insertSubview:self.featureButton belowSubview:self.artistLabel];

由于亚光建议使用主线程来创建这些按钮,我添加了这个:

 [[NSOperationQueue mainQueue] addOperationWithBlock:^ { 
//Create buttons here
 }];

我肯定有更好的性能,但在调用VC几次应用程序FPS下降后,ui元素消失并在几秒钟后逐个重新出现(我有3个UILabels和4个UIButtons)并最终在iPhone上崩溃4S,我认为这很令人惊讶。

我认为它可能来自另一个在启动后转到主要的VC之前调用的VC。在这一个我也创建了UIButtons,但是使用了一个子类。

    #import "RoundButtonClass.h"

@implementation RoundButtonClass
{   CGFloat centerViewX;
    CGFloat centerViewY;
    CGFloat startAngle;
    CGFloat endAngle;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {


        self.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionFinished) name:@"shared" object:nil];



    }
    return self;
}



-(void)drawRect:(CGRect)rect{

    dispatch_async(dispatch_get_main_queue(), ^{
        // do work here
        UIImage *maskedImage = [self maskImage:self.mainBkg withMask:self.maskImage];
        [self setBackgroundImage:maskedImage forState:UIControlStateNormal];


        if (self.isFree == YES ) {
            self.frame = self.freeFrame;
        }
        else{


            centerViewX = (self.frame.size.width/4)*2 - self.size/2.0;
            centerViewY = self.frame.size.height/4 - self.size/2.0 + self.offset;

        self.clipsToBounds = YES;
        self.layer.cornerRadius = self.size/2.0f;
        self.layer.borderColor=[UIColor whiteColor].CGColor;
        self.layer.borderWidth=3.0f;
        self.titleLabel.font=[UIFont fontWithName:@"MyriadPro-Cond" size:70];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;

        [self addTarget:self action:@selector(touch) forControlEvents:UIControlEventTouchUpInside];


                             }];

                         }];

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可能正在创建按钮并将它们添加到后台线程中的界面中。这是非法的,会导致你所描述的延迟。