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

前端 未结 7 658
温柔的废话
温柔的废话 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-30 23:40

    Try using a block it will be more clear.

     // First create the view if you haven't already 
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; 
    // Add it as a subview. 
    [myViewController.view addSubview:myView]; 
    
    
    CGRect newFrameOfMyView = CGRectMake(0.0f, 0.0f, 200.0f, 200.0f); 
    /*
     * Alternatively you could just set the width/height or whatever you'd like with this: 
     * CGRect newFrameOfMyView = myView.frame; 
     * newFrameOfMyView.size.height = 200.0f; 
     * newFrameOfMyView.size.width = 200.0f; 
     */
    [UIView animateWithDuration:0.3f
         animations:^{
          myView.frame = newFrameOfMyView; 
         }
         completion:^(BOOL finished){ 
         NSLog( @"woo! Finished animating the frame of myView!" ); 
    }];
    

提交回复
热议问题