如果我在這兩者之間切換數百次會發生什麼?將留下未使用的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];
}
你應該只在一次視圖**中添加元素**,並保持對視圖的引用,然後對其進行動畫處理,而不是一直插入。 – Lefteris
@Lefteris如何做到這一點?我是iOS編程新手 – Maysam
'@property(strong,nonatomic)UIView * viewA'並初始化一次,然後作爲子視圖添加到'viewDidLoad'方法中。 –