Xcode隐藏自创建按钮
我以编程方式创建了一个按钮:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(290, 120, 40,20); // position in the parent view and set the size of the button
[myButton setTitle:@"Back" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(backLogin:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[myButton setAlpha:0];
[self.view addSubview:myButton];
现在我想使用另一种方法再次隐藏此按钮,并使用动画使按钮消失(!)。 显然,我不能再次使用变量 myButton 并且我不想使该变量成为全局变量。我猜从图层中删除子视图不会产生动画。你有主意吗?我无法让它工作...谢谢!
I created a button programmatically:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(290, 120, 40,20); // position in the parent view and set the size of the button
[myButton setTitle:@"Back" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(backLogin:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[myButton setAlpha:0];
[self.view addSubview:myButton];
Now I would like to hide this button again using another method and use animation to make the button fade away (!).
Obviously, I cannot use the variable myButton again and I don't want to make the variable global. Removing the Subview from the layer won't animate I guess. Do you have an idea? I can't make it to work... Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的处理方式不正确,但首先,您在视图中添加了一个不可见的按钮。假设您打算添加 alpha 为 1 的按钮,我们开始吧。
首先,为按钮设置一个标签:
只要您不将该标签用于其他用途,任何标签都可以。
接下来,在您的其他方法中,我们可以迭代视图控制器中的子视图,我们可以找到标签为 1 的视图,并将其 alpha 设置为 0 并具有良好的淡入淡出效果:
但是,这确实是一个糟糕的方法它,我强烈建议只创建一个实例变量。
I don't think you're going about this the right way, but first of all, you're adding an invisible button to your view. Assuming you meant to add the button with an alpha of 1, here we go.
Fist of all, set a tag for the button:
Any will do, as long as you're not using that tag for something else.
Next in your other method we can iterate through the subviews in your view controller and we can find the view with the tag of 1, and set it's alpha to 0 with a nice fade effect:
However, this is really a bad way to go about it, and I would highly suggest just creating an instance variable.