Search code examples
iosobjective-ccore-animationeasing

How can I animate a box to expand and contract on touch?


I read the intro to core animation from Apple, searched all over for 'ios tweening,' 'ios easing,' and related thins but surprisingly came up empty handed.

How can I make my UILabel contract and hide its subview buttons while keeping the UILabel showing? Links, code, design overview, or anything that gets this done will be extremely helpful.


Solution

  • Surprised no one stepped to this, the answer is pretty straightforward. But maybe that's why.

    I'm not sure about animating the UILabel itself, though as it is a subclass of UIView, I suspect it would be fairly normal.

    The simplest way to get this done is to use the UIView static method:

    + (void)animateWithDuration:(NSTimeInterval)duration 
                     animations:(void (^)(void))animations
    

    In my case, I have a UILabel, myLabel on top of a UIView myContainer with a UIButton dismissButton. Presuming I want dismissButton, when clicked, to roll up myContainer I would Crtl-drag my dismissButton button to the proper viewController.m and in the -(IBAction) method write:

    -(IBAction)dismissButtonPressed:(id)sender
    {
       [UIView animateWithDuration:1.0
                        animations:^{ 
               [self.myContainer.frame = CGRectMake(0,0,frame.bounds.size.width,20)];
               [self.dismissButton removeFromSuperView]; }];
    }
    

    Note: This is from my limited memory, so point out any errors.

    Note2: If you are using this to help yourself, watch those semicolons, brackets and braces

    Now, most people reading this for reference might want more functionality that clicking a button which rolls up a view forever. There are more detailed animateWith* methods.

    There is

    +(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
    

    which allows a completion block to be done once the view is finished animating. Note from Paul Hegarty's iOS class: You may call more animate methods from your completion block.

    There is another animation method that lets us specify animation options.

    + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
    

    The options include:

    enum {
       UIViewAnimationOptionLayoutSubviews            = 1 <<  0,
       UIViewAnimationOptionAllowUserInteraction      = 1 <<  1,
       UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2,
       UIViewAnimationOptionRepeat                    = 1 <<  3,
       UIViewAnimationOptionAutoreverse               = 1 <<  4,
       UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5,
       UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6,
       UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7,
       UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8,
    
       UIViewAnimationOptionCurveEaseInOut            = 0 << 16,
       UIViewAnimationOptionCurveEaseIn               = 1 << 16,
       UIViewAnimationOptionCurveEaseOut              = 2 << 16,
       UIViewAnimationOptionCurveLinear               = 3 << 16,
    
       UIViewAnimationOptionTransitionNone            = 0 << 20,
       UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
       UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
       UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
       UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
       UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
       UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
       UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,
    };
    typedef NSUInteger UIViewAnimationOptions;
    

    Hopefully someone can benefit from this long winded intro to animating a UIView.