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

前端 未结 7 665
温柔的废话
温柔的废话 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条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 23:36

    Here is a fun way of animating your views. In my example I have a touch action that will execute the block animations and a BOOL to make sure to reset the views back to their original states.

    First you put 2 UIViews on a UIViewController (you can color them different colors for contrast). Connect them to your "MainViewController.h" file you create to control the big view. The .h file should look like this:

    #import 
    
    @interface MainViewController : UIViewController
    @property (weak, nonatomic) IBOutlet UIView *topView;
    @property (weak, nonatomic) IBOutlet UIView *bottomView;
    
    @end
    

    and your .m file should look like this:

    #import "MainViewController.h"
    #define kViewAnimateWithDuration 0.35f
    #define kViewDelay 0.05f
    
    @interface MainViewController ()
    @property (nonatomic) BOOL setTouch;
    @end
    
    @implementation MainViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
            self.setTouch = NO;
        }
        return self;
    }
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInView:self.view];
    
        float width_tp = self.topView.frame.size.width;
        float height_tp = self.topView.frame.size.height;
    
        float width_b = self.bottomView.frame.size.width;
        float height_b = self.bottomView.frame.size.height;
    
        if ((CGRectContainsPoint(self.bottomView.frame, touchLocation)) && !self.setTouch){
    
            [UIView animateWithDuration:kViewAnimateWithDuration
                                  delay:kViewDelay
                                options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                             animations:^{
                                 //Move frame or transform view
                                 [self.topView setFrame:CGRectMake(self.topView.frame.origin.x, self.topView.frame.origin.y, width_tp, height_tp + 171)];
    
                                 [self.bottomView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y +171, width_b, height_b -171)];
    
                             } completion:^(BOOL finished) {
                                 self.setTouch = YES;
                             }];
    
    
    
        }
        if ((CGRectContainsPoint(self.bottomView.frame, touchLocation)) && self.setTouch) {
            [UIView animateWithDuration:kViewAnimateWithDuration
                                  delay:kViewDelay
                                options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                             animations:^{
                                 //Move frame or transform view
                                 [self.topView setFrame:CGRectMake(self.topView.frame.origin.x, self.topView.frame.origin.y, width_tp, height_tp - 171)];
    
                                 [self.bottomView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y -171, width_b, height_b +171)];
    
                             } completion:^(BOOL finished) {
                                 self.setTouch = NO;
                             }];
        }
    
    }
    

    So just make sure the topView has some small dimensions like x:0,y:0 width:320 height:43 and bottomView like x:0 y:40 width:320 height:528. Remember to color the backgrounds different colors. Then just run the program and you should see animations.

    For me I think one of the more important lines in the code is setFrame: that to me enables the frame to automagically redisplays the rectangle without having to use the DrawRect method. Setting the property changes the point specified by the center property and the size in the bounds rectangle accordingly.

    Of course there are other fun ways to do this but I hope this can help someone make iOS look magical like it is supposed to be! Happy journeys!

提交回复
热议问题