ios - 只绘制 ImageVIew 框架的角

标签 ios objective-c uiview uibezierpath cashapelayer

我试图只绘制 ImageView 框架的角。为此,我使用了 CAShapeLayer 和 Bezier 路径,并在 UIImageView 框架周围使用虚线很好地工作。但要求更像是,必须只显示角落。到目前为止我尝试过的是,

@property (strong, nonatomic)CAShapeLayer *border;

_border = [CAShapeLayer layer];
_border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
_border.fillColor = nil;
_border.lineDashPattern = @[@40, @50];
[pImageView.layer addSublayer:_border];
_border.path = [UIBezierPath bezierPathWithRect:pImageView.bounds].CGPath;
_border.frame = pImageView.bounds;

我使用了不同的 lineDashPattern 但无法获得结果,因为我没有找到 lineDashPattern 的正确文档。它是如何绘制的等等。 任何类型的帮助都会有用

我想要这样的东西

enter image description here

最佳答案

使用下面的代码,为我工作:

    //1. For imageView Border
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path =[UIBezierPath bezierPathWithRect:_imgView.bounds].CGPath;
    shapeLayer.strokeColor = [UIColor redColor].CGColor; //etc...
    shapeLayer.lineWidth = 2.0; //etc...
    shapeLayer.position = CGPointMake(0, 0); //etc...
    [_imgView.layer addSublayer:shapeLayer];


    //2. For Outside Corner

    float cWidth=30.0;

    float cSpace=_imgView.frame.size.width+20-(2*cWidth);

    NSNumber *cornerWidth=[NSNumber numberWithFloat:cWidth];
    NSNumber *cornerSpace=[NSNumber numberWithFloat:cSpace];

    CAShapeLayer *_border = [CAShapeLayer layer];

    _border.strokeColor = [UIColor blackColor].CGColor;

    _border.fillColor = nil;

    _border.lineWidth=2.0;

    //imageView frame is (width=240,height=240) and 30+200+30=260 -->10 pixel outside imageView
    _border.lineDashPattern = @[cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth];

    _border.path = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, _imgView.frame.size.width+20, _imgView.frame.size.height+20)].CGPath;

    [_imgView.layer addSublayer:_border];

输出:

img

关于ios - 只绘制 ImageVIew 框架的角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105826/

相关文章:

ios - Swift:在与 View 匹配的tapGestureRecognizer中播放声音

ios - 如何在没有 Storyboard的情况下以编程方式在 iOS8 上创建今日小部件?

ios - 从 PSD 中提取图像以用于 iOS 应用程序

ios - 使 UIScrollView 中的顶部图像在 ScrollView 边界上放大?

ios - 无法将 UIView 移到 UITableView 上

ios - 在单个 ViewController 应用程序中,内存保留周期是不可能的吗? ( swift/IOS)

objective-c - 模态视图 Controller 转换

ios - 在自动布局中隐藏不断增长的 UItextview 的键盘。如何克服这个?

iphone - 在另一个 UIViewController 之上添加 UIViewController 的最佳方法

ios - UITableViewCell 和 CALayer 的自动旋转问题