简体   繁体   中英

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

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;
}];

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];



}

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

                         }];

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