gpt4 book ai didi

ios - UIView 动画无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:45 25 4
gpt4 key购买 nike

我在使用 UIView subview 动画时遇到了一些问题。我想要做的是,当您按下主视图时, subview 将从顶部向下滑动,然后在下一次点击时它会向上滑动并被删除。但在当前状态下,它只执行第一次点击命令,在第二次点击时显示 nslog,但 View 和动画的移除不起作用。

这是事件处理函数中的代码:

- (void)tapGestureHandler: (UITapGestureRecognizer *)recognizer
{
NSLog(@"tap");

CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);
UIView *topBar = [[UIView alloc] initWithFrame:frame];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"topbar.png"]];
topBar.backgroundColor = background;

if (topBarState == 0) {
[self.view addSubview:topBar];
[UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 41.0f);}];
topBarState = 1;
} else if (topBarState == 1){
[UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);} completion:^(BOOL finished){[topBar removeFromSuperview];}];

NSLog(@"removed");
topBarState = 0;
}

}

如何使 subview 动画化并正确删除?

最好的问候

自由 Restful

最佳答案

您总是将 topBar 框架设置为 y = -41,因此对于 topBarState = 1,动画适用于 y=-41 到 y=-41 并且似乎不起作用

CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);
UIView *topBar = [[UIView alloc] initWithFrame:frame];

每次创建 View topBar 时。
在 .h 中声明 topBar 并在 viewDidLoad 中分配初始化。

- (void)viewDidLoad {
CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);
topBar = [[UIView alloc] initWithFrame:frame];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"topbar.png"]];
topBar.backgroundColor = background;
[self.view addSubview:topBar];
topBarState = 0;
}

- (void)tapGestureHandler: (UITapGestureRecognizer *)recognizer
{
if (topBarState == 0) {
[UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 41.0f);}];
topBarState = 1;
} else if (topBarState == 1){
[UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);} completion:^(BOOL finished){[topBar removeFromSuperview];}];
topBarState = 0;
}
}

关于ios - UIView 动画无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14235905/

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