嵌套的UIView动画会降低视图加载速度

时间:2013-12-04 18:22:14

标签: ios objective-c uiview

我有一个方法可以生成以下类型的动画:

 -(void)loadButtonView{

AppDelegate *delegateVariable = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIView *scrollButtonPlaceHolder = [[UIView alloc] initWithFrame:CGRectMake(4, 230, 312,320)];
[scrollButtonPlaceHolder setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:153.0/255.0 blue:255.0/255.0 alpha:1]];
scrollButtonPlaceHolder.tag = 999;
scrollButtonPlaceHolder.layer.cornerRadius = 10; // this value vary as per your desire
scrollButtonPlaceHolder.clipsToBounds = YES;
for (int i = 1; i < 5; i ++) {
    NSString *prodName = nil;
    UIImage *img = nil;
if([delegateVariable.archiveArray count] >= i ){
    PriceMatchDetails *pmd =  [delegateVariable.archiveArray objectAtIndex:[delegateVariable.archiveArray count]-i];
    NSURL *url = [NSURL URLWithString:pmd.imageURL];
    NSData *data = [NSData dataWithContentsOfURL:url];
    img = [[UIImage alloc] initWithData:data];
    prodName = pmd.productName;
}else{
    prodName = @"N.A";

    NSURL *url = [NSURL URLWithString:@"http://i.imgur.com/LYFebhz.png"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    img = [[UIImage alloc] initWithData:data];


}

    CGFloat x1 = 20;
    CGFloat y1 = 20;

    if (i%2 == 1) {
        if(i ==3){
            y1 = y1 +150;
        }

    }else if (i%2 == 0){
        if(i == 2){
            x1 = x1 + 150;
        }else{
            x1 = x1 + 150;
            y1 = y1 +150;
        }

    }


   UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(x1,y1, 120, 120);
    button.layer.cornerRadius = 10; // this value vary as per your desire
    button.clipsToBounds = YES;



    //[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]
    [self animateButton:button andLabelString:prodName andimage:img];
    [scrollButtonPlaceHolder addSubview:button];

}



[self.view addSubview:scrollButtonPlaceHolder];

}


-(void)animateButton:(UIButton *)button1 andLabelString:(NSString *)labelText andimage:(UIImage *)btnImage{\
int randNum = rand() % (7 -5) + 5;
button1.alpha = 1.0;
[button1 setBackgroundImage:btnImage forState:UIControlStateNormal];
[UIView animateWithDuration:randNum delay:0 options:UIViewAnimationOptionAllowUserInteraction
                 animations:^(void) {

                     button1.alpha = 0.0;
                 }
                 completion:^(BOOL finished){
                     [UIView animateWithDuration:randNum delay:0 options:UIViewAnimationOptionAllowUserInteraction
                                      animations:^(void) {
                                           button1.alpha = 1.0;
                                          [button1 setBackgroundImage:nil forState:UIControlStateNormal];
                                          [button1 setTitle:labelText forState:UIControlStateNormal];
                                          button1.titleLabel.font = [UIFont boldSystemFontOfSize:8.0];
                                          button1.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
                                          button1.titleLabel.textAlignment = NSTextAlignmentCenter;

                                      }
                                      completion:^(BOOL finished){
                                          [UIView animateWithDuration:randNum delay:0 options:UIViewAnimationOptionAllowUserInteraction
                                                           animations:^(void) {
                                                               button1.alpha = 0.0;


                                                           }completion:^(BOOL finished){
                                                               [button1 setTitle:nil forState:UIControlStateNormal];
                                                               [self animateButton:button1 andLabelString:labelText andimage:btnImage];
                                                               }];

                                      }];


                 }];
}

此方法在 - (void)ViewDiDAppear

下调用

当我使用presentViewController委托方法

呈现此页面时
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
  vc.view.backgroundColor = [UIColor clearColor];
  self.modalPresentationStyle = UIModalPresentationFullScreen;
  [self presentViewController:vc animated:NO completion:nil];

使用IBAction。当我按下该按钮时,此loadButtonView方法会减慢转换速度。 在启动时,首先显示相同的视图控制器,并在视图出现后单独将新的scrollButtonPlaceHolder添加到视图中。但转换仍然等待loadButtonView启动。有关如何在显示ViewController后缓慢加载此视图的任何建议吗?

0 个答案:

没有答案