ios - UIView 底边模式

标签 ios objective-c cocoa-touch uiview

我想在我的 UIView 的底部边缘添加一个鲨鱼齿/三角形/尖头图案。像这样:

enter image description here

现在只用代码就可以做到这一点吗?以及如何?

最佳答案

这应该让你上路:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBezierPath *pth = [UIBezierPath bezierPath];

    CGFloat w = 240.0;
    CGFloat h = 200.0;

    CGPoint p = CGPointZero;

    // start at top-left
    [pth moveToPoint:p];

    // line to top-right
    p.x = w;
    [pth addLineToPoint:p];

    // line to bottom-right
    p.y = h;
    [pth addLineToPoint:p];

    // line to 40 left, 40 up
    p.x -= 40;
    p.y -= 40;
    [pth addLineToPoint:p];

    // line to 40 left, 40 down (back to bottom)
    p.x -= 40;
    p.y += 40;
    [pth addLineToPoint:p];

    // line to 40 left, 40 up
    p.x -= 40;
    p.y -= 40;
    [pth addLineToPoint:p];

    // line to 40 left, 40 down (back to bottom)
    p.x -= 40;
    p.y += 40;
    [pth addLineToPoint:p];

    // line to 40 left, 40 up
    p.x -= 40;
    p.y -= 40;
    [pth addLineToPoint:p];

    // line to 40 left, 40 down (back to bottom)
    p.x -= 40;
    p.y += 40;
    [pth addLineToPoint:p];

    // line to starting point - top-left
    [pth closePath];

    // 240 x 200 rectangle at 40,40
    CGRect r = CGRectMake(40, 40, w, h);

    // create a UIView
    UIView *v = [[UIView alloc] initWithFrame:r];
    v.backgroundColor = [UIColor redColor];

    // create a CAShapeLayer
    CAShapeLayer *maskShape = [CAShapeLayer layer];

    // add the path to the CAShapeLayer
    maskShape.path = pth.CGPath;

    // set the view's layer mask to the CAShapeLayer
    v.layer.mask = maskShape;

    // add the masked subview to the view
    [self.view addSubview:v];

}

关于ios - UIView 底边模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332011/

相关文章:

ios - 为什么需要在图像格式之间进行如此多的转换? (UIImage, CGImage, CIImage)

ios - 如何在 iOS 应用程序中使用 Objective C 解析 Ruby on rails webservice

objective-c - 如何使用EventKit Framework提前一年设置提醒

ios - XCUITest 和动态生成的 View

ios - 如何在完成 block 之前解决嵌套的异步调用

ios - 使用 XCode 9/iOS 11 时出现 Phonegap 错误

iOS 控制中心触摸传递到我的 ScrollView ?

objective-c - AWS IOS SDK 1.7.0 -> 架构 armv7s 的 undefined symbol :

objective-c - 跟踪 NSTextField( cocoa )的选择范围变化

ios - UIButton 触摸 Action 不在触摸时调用但在触摸移动时调用