addSubview animation

前端 未结 6 896
情书的邮戳
情书的邮戳 2020-11-28 01:58

I have main UIView where I display different data. Then I put a button, which displays subview like this:

- (IBAction) buttonClicked:(id)sender
{
    UIView          


        
6条回答
  •  星月不相逢
    2020-11-28 02:48

    I found that even with the animation blocks and options, trying to animate addSubview alone would not do anything for me. I found a workaround which is to set the subview's alpha to 0.0, add the subview, and then animate the setting of the alpha from 0.0 to 1.0. This gives me the fade in effect I was looking for.

    Hope this helps someone else.

    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    redView.alpha = 0.0;
    
    [self.view addSubview:redView];
    
    [UIView animateWithDuration:0.5 animations:^{
      redView.alpha = 1.0;
    }];
    

提交回复
热议问题