2014-01-12 77 views
0

如果我在这两者之间切换数百次会发生什么?将留下未使用的UIView s的汉德? 我在主UIView上有几个控件,我想多次将它们作为一组控件进行动画(向上滑动和向下滑动),然后我决定将它们添加到UIView并为该视图添加动画。但似乎每次当我将view添加到superView时,它仍然存在,如果我运行[view removeFromSuperView],它还会删除我已添加到view的所有控件。它会影响性能还是会导致内存泄漏问题?移除动画后的UIView,而不会丢失其他元素

(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

你应该只在一次视图**中添加元素**,并保持对视图的引用,然后对其进行动画处理,而不是一直插入。 – Lefteris

+0

@Lefteris如何做到这一点?我是iOS编程新手 – Maysam

+0

'@property(strong,nonatomic)UIView * viewA'并初始化一次,然后作为子视图添加到'viewDidLoad'方法中。 –

回答

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){ 
        }]; 
} 
+0

谢谢你的工作。 – Maysam

+0

我只会添加'viewDidLoad'调用'super'作为第一件事,它现在不会改变任何东西,但这是一个很好的做法 –

相关问题