开发者

How to animate expand and shrink uiview in place?

开发者 https://www.devze.com 2023-03-14 11:29 出处:网络
I have a series of uiview开发者_StackOverflow社区s, 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. Anot

I have a series of uiview开发者_StackOverflow社区s, 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];  
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号