动画后删除UIView而不会丢失其他元素

时间:2014-01-12 10:01:44

标签: ios objective-c uiview

如果我在这两者之间切换数百次会怎样?将被遗弃未使用的UIView s? 我的主UIView上有几个控件,我想将它们设置为一组控件动画(向上滑动和向下滑动)几次,然后我决定将它们添加到UIView和为该视图制作动画。但似乎每当我将view添加到superView时它仍然存在,如果我运行[view removeFromSuperView],它也会删除我添加到view的所有控件。它会影响性能还是会导致内存泄漏问题?

 (void)slideUp{
    repeatViewTop = datePickerButton.frame.origin.y;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, repeatViewTop)];
    [view addSubview:taskTitle];
    [view addSubview:taskNote];
    [view addSubview:datePickerButton];
    [view addSubview:taskSelectedDateViewer];
    [self.view addSubview:view];

    [UIView animateWithDuration:0.4f
                          delay:0.1f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         float tempY=view.frame.origin.y;
                         [view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)];
                         [view setAlpha:0.4f];
                     }
                     completion:^(BOOL finished){
                     }];
    [self.view insertSubview:view atIndex:0];
}

这将滑下这些控件:

- (void)slideDown{
    taskTitle.userInteractionEnabled=NO;
    //150=view.frame.origin.y after sliding up
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -114, SCREEN_WIDTH, repeatViewTop)];
    [view addSubview:taskTitle];
    [view addSubview:taskNote];
    [view addSubview:datePickerButton];
    [view addSubview:taskSelectedDateViewer];
    [self.view addSubview:view];

    [UIView animateWithDuration:0.4f
                          delay:0.1f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         float tempY=view.frame.origin.y *(-1);
                         [view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)];
                         [view setAlpha:1.0f];
                     }
                     completion:^(BOOL finished){


                     }];
    [self.view insertSubview:view atIndex:0];
}

1 个答案:

答案 0 :(得分:1)

您应该仅在视图一次中添加元素并保持对视图的引用,然后为其设置动画,而不是一直插入它。

您可以随时创建视图,例如在viewDidLoad委托中,如下所示:

- (void)viewDidLoad {
    //Other code here


    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, repeatViewTop)];
    view.tag = 1999;
    [view addSubview:taskTitle];
    [view addSubview:taskNote];
    [view addSubview:datePickerButton];
    [view addSubview:taskSelectedDateViewer];
    [self.view addSubview:view];
}

然后在动画中获得对此视图的引用,如下所示:

(void)slideUp{
    repeatViewTop = datePickerButton.frame.origin.y;
    UIView *view = [self.view viewWithTag:1999];

    [UIView animateWithDuration:0.4f
                          delay:0.1f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         float tempY=view.frame.origin.y;
                         [view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)];
                         [view setAlpha:0.4f];
                     }
                     completion:^(BOOL finished){
                     }];
}