RollRoll
RollRoll

Reputation: 8462

Set animation for a dynamically created UIView

I figured out most of the thing how to add UIView dynamically, but now I'm stuck trying to create a alpha animation when a uibutton is clicked and the uiview is shown up. Not sure what I am missing here:

- (void)buttonPressedAction:(id)sender
{
    CGRect frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    frame.size = self.scrIcon.frame.size;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];

    UIView* view = [[UIView alloc] initWithFrame:frame];

    view.alpha = 1.0;

    [UIView commitAnimations];

    view.backgroundColor = [UIColor redColor];
    [scrIcon addSubview:view];


    /*
    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
    CategoryViewController* theView = [sb instantiateViewControllerWithIdentifier:@"categoryIdentifier"];

    theView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:theView animated:YES];*/

}

scrIcon is a UIScrollView

thanks

Upvotes: 1

Views: 1078

Answers (3)

ShengHuaWu
ShengHuaWu

Reputation: 140

You can try this method: animateWithDuration:animations:

UIView *view = [[UIView alloc] initWithFrame:self.scrIcon.frame];
view.backgroundColor = [UIColor redColor];
view.alpha = 0.0;

[scrIcon addSubview:view];

[UIView animateWithDuration:1.0 animations:^{
    view.alpha = 1.0;
}];

Upvotes: 4

Manu
Manu

Reputation: 4750

In ViewDidLoad: just init Your View…..

YourAnimateView = [[UIView alloc]initWithFrame:CGRectMake(-320, 0, 320, 320)];
[self.view addSubview:YourAnimateView];


In Your Button Action Method : just put this

[UIView animateWithDuration:0.5 delay:0.1 
                            options:UIViewAnimationOptionTransitionCurlDown
                         animations:^{
                             //animation code
                             YourAnimateView.frame = CGRectMake(0, 0, 320, 345);

                         } completion:^(BOOL finished) {
                             //completion code

                         }];

Upvotes: 2

Anand
Anand

Reputation: 1973

Modified your code add the view 1st to scrIcon. Then perform the animation.

- (void)buttonPressedAction:(id)sender
{
    CGRect frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    frame.size = self.scrIcon.frame.size;

    UIView* view = [[UIView alloc] initWithFrame:frame];

    view.alpha = 0.0f;

    view.backgroundColor = [UIColor redColor];
    [scrIcon addSubview:view];


    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];

    view.alpha = 1.0;

    [UIView commitAnimations];



}

Upvotes: 2

Related Questions