【发布时间】:2026-01-16 11:10:02
【问题描述】:
我目前有一个UIView,上面有UILabels。
这些UILabels 有圆角。
我按下了一个按钮,然后使用UIView transitionWithView 方法以编程方式删除所有这些标签。
但是,在过渡期间,圆角会丢失。
是否可以在过渡期间保持这些圆角? 即在过渡之前、期间和之后,拐角应保持圆角。
下面是一些示例代码:
@interface ExampleViewController
@property (strong, nonatomic) UIView *view;
@property (strong, nonatomic) UILabel *myLabel;
@end
@implementation ExampleViewController
- (void) viewDidLOad
{
self.myLabel = [[UILabel alloc] initWithFrame:self.view.frame];
[self.myLabel setText:@"Example Label"];
[self.myLabel setCenter:CGPointMake(100,100)]; // position the label somewhere on the screen
[self.myLabel.layer setCornerRadius:5]; // set the corner radius
[self.myLabel.layer setMasksToBounds:YES]; // found this particular line on another * thread (http://*.com/questions/11604215/uiview-transitionwithview-discarding-layer-settings)
[self.myLabel setHidden:NO];
[self.myLabel setBackgroundColor:[UIColor orangeColor]];
[self.myLabel setNumberOfLines:0];
[self.myLabel sizeToFit];
[self.view addSubview:self.myLabel];
}
// user interaction
- (IBAction)labelOff:(id)sender
{
BOOL hidden = [self.myLabel isHidden];
[UIView transitionWithView:self.myLabel
duration:1
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
[self.myLabel setHidden:!hidden];
}
@end
我正在使用 XCode 5 和 iOS 7。非常感谢任何帮助。
【问题讨论】:
-
你是什么意思在过渡期间保持这些圆角?
-
抱歉 - 忘记添加背景颜色和尺寸调整。已编辑。因此,当标签首次显示时,标签具有圆角。当我将标签转换为隐藏它时,角落变成“不圆”,就像它一样。
-
我的问题是:您想要过渡后的圆角还是只想要过渡时..?
-
嗯,好的——我想要在过渡之前、期间和之后的圆角(尽管之后标签无论如何都会被隐藏——但我想把标签带回来)。当我从隐藏过渡到非隐藏时,标签实际上确实保留了它们的圆角。这只是从非隐藏到隐藏的过渡,圆角似乎消失了。
-
这里是使用
transitionWithView:*.com/questions/11604215/…的实际答案。只需将视图层的masksToBounds属性设置为true