How to animate the width and height of a UIView in Xcode?

前端 未结 7 657
温柔的废话
温柔的废话 2021-01-30 23:27

I have this subview I want to add to my main view, but make it expand from the origin. I read some of the Apple documentation but I don\'t understand where I am making mistake.

7条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 00:02

    Try this:

    customView.frame= CGRectMake(self.view.frame.origin.x +5,self.view.frame.origin.y+5,0, 150);
    [self.view addSubview:customView];
    CGRect frame = customView.frame;
    frame.size.width = 310;
    [UIView animateWithDuration:0.4
     animations:^{
      customView.frame= frame; 
     }];
    

    Or if you want to use beginAnimation method instead of block Methos , Use this :

    UIView *customView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x +5,self.view.frame.origin.y+5,0, 150)];
    [self.view addSubview:customView];
    CGRect frame = customView.frame;
    frame.size.width = 310;
    [UIView beginAnimations:@"animateAddContentView" context:nil];
    [UIView setAnimationDuration:0.4];
    customView.frame= frame;
    [UIView commitAnimations];
    

提交回复
热议问题