2017-08-07 133 views
1

我有一個帶有貝塞爾路徑遮罩圖層的視圖。現在我需要添加到我的視圖中的子視圖,具有雖然我的觀點是正確生成我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我試過了,但那也沒有幫助我 –

回答

1

有一對夫婦的方式來實現你想要什麼:

方法一: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; 
} 

點擊按鈕然後移動陰影在選定的選項卡下。代碼是快速和骯髒的,你需要添加更多的邏輯,清理等,但你明白了,方法一插入一個陰影視圖到每個方法,方法二移動一個陰影視圖關於標籤下。

相關問題