ios - 在分段控件之间切换时隐藏线条

标签 ios objective-c uiview uisegmentedcontrol

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

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];
}

我不想将该行添加到内容 View ,因此当选择另一个 View 时它会被隐藏。

最佳答案

将属性添加到 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;

关于ios - 在分段控件之间切换时隐藏线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34521863/

相关文章:

ios - 如何在 Objective c 中以编程方式将 2 个 UIButton 放置在 UIView 中,宽度相等?

iphone - 如何使 'touchesBegan' 方法适用于特定 View ?

ios - 在 xcode 12 中创建通用框架时出现 Lipo 错误

ios - 在 SwiftUI/Swift/Objective-C/Xamarin 中将底部边框线添加到 UI TextField View

ios - insertSubview :belowSubview doesnt work 的简单案例

objective-c - 触摸详细信息披露指示器时 Mapview 注释崩溃

Xcode4 中的 Objective-C 调试技巧?

ios - 在 Swift 中使用视觉格式通过约束格式化 TextView

ios - 正确使用 MVC 和 Singleton Swift

ios - 如何使用按钮在 UIPageViewController 中向前和向后导航?