我正在使用drawRect&amp ;;在UIView中绘制一条线。 BezierPath。我还有一个带有contentview1和contentview2的分段控制器。我希望该行显示在内容view1中,但不是contentview2。我的行代码在uiview文件中,而分段控件在uiviewcontroller中。如何成功隐藏2个内容视图之间的界限。
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中,因此当选择另一个视图时它将被隐藏。
答案 0 :(得分:1)
将属性添加到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;