简体   繁体   中英

How to animate expand and shrink uiview in place?

I have a series of uiviews , and i want to create an animation where a button press event will expand the uiview from the bottom and reveal more information about the view. Another button press will shrink the view back to original.

How do i proceed with this?

There are a series of uiviews laid out in a horizontal scroll view like a coverflow. I want to display additional information about each uiview on click of a button.

Do drop a comment if you need more information.

TIA,

Praveen S

This code will expand your view...do the reverse to shrink it

CGRect tempFrame=view.frame;
tempFrame.size.width=200;//change acco. how much you want to expand
tempFrame.size.height=200;
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.2];
view.frame=tempFrame;
[UIView commitAnimations];

For that use this code

In .h file

#import <UIKit/UIKit.h>
@interface ExpandedViewController : UIViewController
{
    UIView * expandiView;
    BOOL viewExpanded;
}
- (IBAction)expandOrShrinkView:(id)sender;
@end

and in your .m file

- (void)viewDidLoad
{
    [super viewDidLoad];

    aview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    aview.backgroundColor = [UIColor redColor];
    [self.view addSubview:aview];

    viewExpanded = NO;
}

And in your button action add this code

- (IBAction)expandOrShrinkView:(id)sender {
    [UIView animateWithDuration:0.8f delay:0.0f options:UIViewAnimationOptionTransitionNone animations: ^{
        if (!viewExpanded) {
            viewExpanded = YES;
           aview.frame = CGRectMake(10, 10, 100, 200);
        }else{
            viewExpanded = NO;
           aview.frame = CGRectMake(10, 10, 100, 100);
        }
    } completion:nil];  
}

When you click the button first time it will expand the view from bottom and when you click the button second time it will shrink the View from bottom change the frame size to your need..

And if you want shrink function in another button than use two action method like this

- (IBAction)expandView:(id)sender {
        [UIView animateWithDuration:0.8f delay:0.0f options:UIViewAnimationOptionTransitionNone animations: ^{
               aview.frame = CGRectMake(10, 10, 100, 200);
        } completion:nil];  
    }

- (IBAction)ShrinkView:(id)sender {
        [UIView animateWithDuration:0.8f delay:0.0f options:UIViewAnimationOptionTransitionNone animations: ^{
               aview.frame = CGRectMake(10, 10, 100, 100);
        } completion:nil];  
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM