gpt4 book ai didi

ios - 动画后移除 UIView 不丢失其他元素

转载 作者:行者123 更新时间:2023-11-29 03:21:09 30 4
gpt4 key购买 nike

如果我在这两者之间切换数百次会怎样?将成为未使用的 UIView 的左撇子?我的主 UIView 上有几个控件,我想将它们作为一组控件进行动画(向上滑动和向下滑动)几次,然后我决定将它们添加到 UIView 并为该 View 设置动画。但似乎每次当我将 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];
}

最佳答案

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

您可以随时创建 View ,例如在 viewDidLoad 委托(delegate)中,如下所示:

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

然后在动画中获取对 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){
}];
}

关于ios - 动画后移除 UIView 不丢失其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21073380/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com