简体   繁体   English

如何在父视图范围之外添加子视图

[英]How to add subview out of the parent view bounds

I have a view with bezier path mask layer. 我有一个贝塞尔曲线路径蒙版层的视图。 Now I need to add to my view a subview, that has to be placed outside of the superView 现在,我需要在视图中添加一个子视图,该子视图必须放置在superView之外

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()
}

}

Although my view is generating correctly my shadowView is being cropped by superView bounds. 尽管我的视图生成正确,但是我的shadowView被superView边界裁剪了。 How can I avoid such behaviour? 如何避免这种行为?

在此处输入图片说明

There are a couple of ways to achieve what you want: 有两种方法可以实现您想要的:

METHOD ONE: INSERT SHADOW SHAPE INTO EACH TAB 方法一:将阴影形状插入每个选项卡

在此处输入图片说明

Create the tabs you want and then add a custom CAShapeLayer to each one: 创建所需的选项卡,然后向每个选项卡添加自定义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];

You can access the tabs and shadowLayer like this (given an array 'tabs'): 您可以像这样访问标签和shadowLayer(给定一个数组“标签”):

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

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

METHOD TWO: MOVE SHADOW VIEW 方法二:移动阴影视图

在此处输入图片说明

Another way is to create a single custom 'tabShadowView' and simply move that to the location of the selected tab. 另一种方法是创建单个自定义“ tabShadowView”,然后将其简单地移动到所选标签的位置。 Here's the code for laying out the tabs in a custom view: 以下是用于在自定义视图中布置标签的代码:

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

Put the above code in the place you layout your views. 将以上代码放在布局视图的位置。 The tabShadowView is the view that we move about, it's right at the bottom of the view hierarchy. tabShadowView是我们要移动的视图,它位于视图层次结构的底部。 Then the for loop just adds tabs in on top of it. 然后for循环仅在其顶部添加选项卡。 Here are the methods that it uses: 这是它使用的方法:

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

Tapping on a button then moves the shadow under the selected tab. 点击按钮,然后将阴影移动到所选选项卡下。 The code is quick and dirty, you'll need to add more logic, clean up etc, but you get the point, method one inserts a shadow view into each, method two moves a single shadow view about underneath the tabs. 代码是快速而又肮脏的,您需要添加更多的逻辑,清理等,但是您明白了,方法一在每个视图中插入一个阴影视图,方法二在选项卡下面移动一个阴影视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM