为动态创建的UIView设置动画

时间:2012-10-05 03:31:55

标签: iphone objective-c ios xcode

我想出了大部分动态添加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

感谢

3 个答案:

答案 0 :(得分:4)

您可以尝试此方法:animateWithDuration:动画:

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

答案 1 :(得分:2)

修改您的代码将视图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];



}

答案 2 :(得分:2)

在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

                         }];