使用UILabel的UIView transitionWithView在转换期间丢失圆角

时间:2014-01-29 11:40:01

标签: ios uiview uilabel

我目前有一个UIViewUILabel坐在它上面。

这些UILabel有圆角。

我按了一个按钮,然后使用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 stackoverflow thread (http://stackoverflow.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 。任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:2)

我不确定在过渡期间角落半径是如何被移除的。但是这里有一种可以使用的替代方法,它具有相同的最终结果。

- (IBAction)labelOff:(id)sender
{
    [UIView animateWithDuration:1.0f
                     animations:^{
                         self.myLabel.alpha = !self.myLabel.alpha;
                     }];
}

答案 1 :(得分:0)

我希望你需要清空UILabel.text但属性应该是有用的。对吧?,然后

BOOL hidden; // Declare under @interface

- (IBAction)labelOff:(id)sender 
{
   if(hidden){
      hidden =false;
      [self.myLabel setText:@""];
   }
   else{
      hidden =true;
     [self.myLabel setText:@"Example Label"];
   }
   [UIView transitionWithView:self.myLabel 
                     duration:1
                      options:UIViewAnimationOptionTransitionCrossDissolve
                   animations:NULL
                   completion:NULL];


}