在分段控件之间切换时隐藏一行

Hiding a line while switching between segmented controls

我正在使用 drawRect 和 BezierPath 在 UIView 中画一条线。我还有一个带有 contentview1 和 contentview2 的分段控制器。我希望该行显示在内容视图 1 而不是内容视图 2 中。我的行的代码在 uiview 文件中,而分段控件在 uiviewcontroller 中。我怎样才能成功隐藏两个内容视图之间的线。

InformationView.m (UIView)

- (void)drawRect:(CGRect)rect {

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10.0, 10.0)];
[path addLineToPoint:CGPointMake(100.0, 100.0)];
path.lineWidth = 3;
[[UIColor blueColor] setStroke];
[path stroke];

}

InformationViewController.m (UIViewController)

segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(12, 80, 300, 30);
[segmentedControl addTarget:self action:@selector(segmentAction) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
[self.view addSubview:contentView2];
[self.view addSubview:contentView ];

-(void) segmentAction {

if (segmentedControl.selectedSegmentIndex == 0) {

    [contentView setHidden:NO;
    [contentView2 setHidden:YES];


}
if (segmentedControl.selectedSegmentIndex == 1) {

    [contentView setHidden:YES];
    [contentView2 setHidden: NO];
}

我不会将行添加到ContentView,因此选择其他视图时将隐藏它。

添加一个属性到InformationView

.h

@interface InformationView : UIView

@property (assign, nonatomic ,getter=isLineHidden) BOOL lineHidden;

@end

.m

@implementation InformationView

- (void)drawRect:(CGRect)rect {
    if (self.lineHidden == false) {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(10.0, 10.0)];
        [path addLineToPoint:CGPointMake(100.0, 100.0)];
        path.lineWidth = 3;
        [[UIColor blueColor] setStroke];
        [path stroke];
    }
}

-(void)setLineHidden:(BOOL)lineHidden{
    if (_lineHidden != lineHidden) {
        _lineHidden = lineHidden;
        [self setNeedsDisplay];
     }
}

@end

然后,当你想隐藏行时

 self.yourView.lineHidden = true;