为动态创建的 UIView 设置动画



我想出了如何动态添加 UIView 的大部分内容,但现在我在单击 uibutton 并显示 uiview 时尝试创建 alpha 动画。不确定我在这里错过了什么:

- (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 是一个 UIScrollView

谢谢

您可以尝试此方法: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;
}];

修改了您的代码,将视图 1st 添加到 scrIcon。然后执行动画。

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

}

在 ViewDidLoad: 只需初始化您的视图.....

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

最新更新