如何将子视图添加到父视图边界

问题描述:

我有一个带有贝塞尔路径遮罩图层的视图。现在我需要添加到我的视图中的子视图,具有虽然我的观点是正确生成我shadowView是由上海华界冒出来放在上海华如何将子视图添加到父视图边界

class MiddleSegmentView: UIView { 

override func layoutSubviews() { 
    super.layoutSubviews() 
    createMaskView() 
} 

func createPath() -> UIBezierPath { 
    let width = self.frame.size.width 
    let height = self.frame.size.height 
    let lineWidth = -height/tan(angle) 

    let path = UIBezierPath() 
    path.moveToPoint(CGPoint(x: 0.0, y: 0.0)) 
    path.addLineToPoint(CGPoint(x: width, y: 0)) 
    path.addLineToPoint(CGPoint(x: width - lineWidth, y: height)) 
    path.addLineToPoint(CGPoint(x: lineWidth, y: height)) 
    path.addLineToPoint(CGPoint(x: 0.0, y: 0.0)) 
    path.closePath() 

    return path 
} 

func createMaskView() { 
    let mask = CAShapeLayer() 
    mask.path = createPath().CGPath 
    self.layer.mask = mask 
} 

    //Creating shadow 

    override func drawRect(rect: CGRect) { 
    super.drawRect(rect) 
    //createShadow() 
    let shadowView = SegmentShadow(frame: CGRect(x: 0, y: 0, width: 10, height: 40)) 
    self.clipsToBounds = false 
    self.addSubview(shadowView) 
    shadowView.bringToFront() 
} 

} 

之外。我怎样才能避免这种行为?

enter image description here

+0

你可以在添加子视图后应用biezer路径吗? –

+0

@MohammadBashirSidani我试过了,但那也没有帮助我 –

有一对夫妇的方式来实现你想要什么:

方法一:INSERT影形态到各选项卡

enter image description here

创建所需的标签然后添加一个自定义CAShapeLayer到每个:

//insert a CAShapeLayer into each 'tab' 
CAShapeLayer * shadowLayer = [self trapezium]; 
shadowLayer.fillColor = grey.CGColor; 
shadowLayer.shadowOpacity = 1.0f; 
shadowLayer.shadowRadius = 1.0f; 
shadowLayer.shadowOffset = CGSizeZero; 
shadowLayer.shadowColor = [UIColor blackColor].CGColor; 
[tab.layer insertSublayer:shadowLayer atIndex:0]; 

您可访问的标签和shadowLayer这样的(给定一个数组 '标签'):

//then access like this 
UIView * tab = tabs[2]; 
[self.view bringSubviewToFront:tab]; 

CAShapeLayer * layer = (CAShapeLayer*)tab.layer.sublayers[0]; 
layer.fillColor = orange.CGColor; 

方法二:MOVE SHADOW VIEW

enter image description here

另一种方法是创建一个单独的自定义“tabShadowView”,并将其简单地移到选定选项卡的位置。以下是在自定义视图中布置标签的代码:

grey = [_peacock colourForHex:@"#ACA499" andAlpha:1.0f]; 
orange = [_peacock colourForHex:@"#CB9652" andAlpha:1.0f]; 

tabShadowView = [UIView new]; 
tabShadowView.frame = CGRectMake(0, 20, 100, 40); 
tabShadowView.layer.shadowPath = [self trapezium].path; 
tabShadowView.layer.shadowColor = [UIColor blackColor].CGColor; 
tabShadowView.layer.shadowOffset = CGSizeZero; 
tabShadowView.layer.shadowRadius = 1.0f; 
tabShadowView.layer.shadowOpacity = 0.5f; 
[self.view addSubview:tabShadowView]; 

customTabView = [UIView new]; 
customTabView.frame = CGRectMake(0, 0, w, 60); 
[self.view addSubview:customTabView]; 

NSArray * titles = @[@"Button A", @"Button B", @"Button C",@"Button D"]; 
tabs = [NSMutableArray new]; 

float xOff = 0.0f; 
for (NSString * title in titles){ 

    UIView * tab = [UIView new]; 
    tab.frame = CGRectMake(xOff, 20, 100, 40); 
    tab.backgroundColor = grey; 
    tab.layer.mask = [self trapezium]; 
    [customTabView addSubview:tab]; 
    [tabs addObject:tab]; 

    UIButton * button = [UIButton new]; 
    button.frame = tab.bounds; 
    [button setTitle:title forState:UIControlStateNormal]; 
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    button.titleLabel.textAlignment = NSTextAlignmentCenter; 
    button.titleLabel.font = [UIFont systemFontOfSize:14.0f weight:UIFontWeightRegular]; 
    [button addTarget:self action:@selector(tabSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setTag:[titles indexOfObject:title]]; 
    [tab addSubview:button]; 

    xOff += 70.0f; 
} 

[self updateTabsForSelected:tabs[1]]; 

将上面的代码放在您布置视图的位置。 tabShadowView是我们移动的视图,它位于视图层次结构的底部。然后,for循环只是在其上添加标签。以下是它使用的方法:

-(void)tabSelected:(UIButton *)button { 

    UIView * tab = tabs[(int)button.tag]; 
    [self updateTabsForSelected:tab]; 
} 
-(void)updateTabsForSelected:(UIView *)tab{ 

    for (UIView * view in customTabView.subviews){ 

     [customTabView sendSubviewToBack:view]; 
     view.backgroundColor = grey; 
    } 

    [customTabView bringSubviewToFront:tab]; 
    tab.backgroundColor = orange; 
    tabShadowView.transform = CGAffineTransformMakeTranslation([tabs indexOfObject:tab]*70, 0); 
} 
-(CAShapeLayer *)trapezium { 

    UIBezierPath * path = [UIBezierPath bezierPath]; 
    [path moveToPoint:CGPointZero]; 
    [path addLineToPoint:CGPointMake(20, 40)]; 
    [path addLineToPoint:CGPointMake(80, 40)]; 
    [path addLineToPoint:CGPointMake(100, 0)]; 
    [path closePath]; 

    CAShapeLayer * layer = [CAShapeLayer layer]; 
    layer.path = path.CGPath; 

    return layer; 
} 

点击按钮然后移动阴影在选定的选项卡下。代码是快速和肮脏的,你需要添加更多的逻辑,清理等,但你明白了,方法一插入一个阴影视图到每个方法,方法二移动一个阴影视图关于标签下。