如何在视图可见时添加动画?

时间:2015-04-17 05:04:26

标签: ios objective-c uitableview animation hidden

我在静态UIView中有一个UITableViewCellview设置为隐藏在viewDidLoad中。选择button后,view即可显示。

- (void)viewDidLoad {
    [self.myView setHidden:YES];
}

- (IBAction)myButton:(id)sender
{
    if (self.myView.hidden == YES) {
        [self.myView setHidden:NO];
    } else {
        [self.myView setHidden:YES];
    }

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        if (self.myView.hidden == YES) {
            retuen 252 - self.myView.bounds.size.height;
        } else return 252;
    }
    tableView.estimatedRowHeight = 150;
    return UITableViewAutomaticDimention;
}

现在发生的情况是,当选择button时,view变为可见,cells高度随动画更改;但是view立即可见,它会妨碍其他对象(直到cells动画结束)。

如何让view同时输入动画? (我更喜欢它的高度动画。)

修改

我尝试了以下操作来设置高度动画,以便它与cell

同步
if (self.myView.hidden == YES) {
    [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y - self.myView.frame.size.height, self.myView.frame.size.width, self.myView.frame.size.height)];

    [self.myView setHidden:NO];
    [UIView animateWithDuration:1.0 animations:^(){
        [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, self.myView.frame.size.height)];
    }];
}

但它所做的只是动画views位置(从上到下),而不是它的高度?

5 个答案:

答案 0 :(得分:1)

您可以像这样更改

if (self.myView.hidden == YES) {
    [self.myView setAlpha:0.0];
    [self.myView setHidden:NO];
}
[UIView animateWithDuration:0.2 animations:^{
    if (self.myView.hidden == YES) {
         [self.myView setAlpha:1.0];
    } else {
         [self.myView setAlpha:0.0];
    }
}
completion:^(BOOL finished) {
    if (self.myView.alpha == 0.0) {
        [self.myView setHidden:YES];
    }
}];

这可以帮助你。

享受编码!!

答案 1 :(得分:1)

在myButton:事件中你可以使用

- (IBAction)myButton:(id)sender
{

    if (self.myView.hidden == YES) {
        [self.myView setHidden:NO];
    } else {
        [self.myView setHidden:YES];
    }

   [self.myView setAlpha:0.0f];
   [UIView animateWithDuration:1.0
                          delay:0.0
                        options:nil
                     animations:^{
                         [self.myView setAlpha:1.0f];
                     }
                     completion:^(BOOL complete){
                         [self.myView setAlpha:1.0f];
                     }];

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
} 

如果要为视图的高度设置动画,可以执行 -

CGFloat viewHeight = self.myView.frame.size.height;
    [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, 0)];
           [UIView animateWithDuration:1.0
                                  delay:0.0
                                options:nil
                             animations:^{
                                 [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, viewHeight)];
                             }
                             completion:^(BOOL complete){
                                 [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, viewHeight)];
                             }];

答案 2 :(得分:0)

UIView上的隐藏属性不可动画,您需要取消隐藏它,然后将其设置为动画即将开始时应该处于的状态(因此可能设置alpha = 0或height = 0 )然后在动画块中,将其设置为您想要的正确状态(alpha = 1,height = cell.frame.size.height)

例如

view.hidden = NO;
view.alpha = 0;

[UIView animateWithDuration:0.25 animations:^() {
    view.alpha = 1;
}];

答案 3 :(得分:0)

//显示

   [UIView animateWithDuration:0.7f
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             viewtemp.alpha = 0.912f;
                         } completion:NULL];

//隐藏

 [UIView animateWithDuration:0.9f
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         viewtemp.alpha = 0.0f;
                     } completion:NULL];

答案 4 :(得分:0)

@arnold,您尝试更改其Y Position,而不是height,您需要在其Height上应用动画。如下所示。

if (self.myView.hidden == YES) {
[self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, 0)];

[self.myView setHidden:NO];
[UIView animateWithDuration:1.0 animations:^(){
    [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, self.myView.frame.size.height)];
}];
}