0

As in image attached, can anyone guide me how I can clip the background view based on the bezierpath layer added on it.

Thanks in advance! enter image description here enter image description here

Hassan ilyas
  • 103
  • 8

2 Answers2

2

To clip the top part you may use belowLine

UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(0, 0)];
[aPath appendPath:pathExact];//Path exact is your original path drawn
[aPath addLineToPoint:CGPointMake(self.frame.size.width, 0)];
[aPath addLineToPoint:CGPointMake(0, 0)];
[aPath closePath];

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = aPath.CGPath;
[self.layer setMask:shapeLayer];
Rajesh
  • 10,318
  • 16
  • 44
  • 64
1

You can use the following class:

UIBezierPathView.h

#import <UIKit/UIKit.h>

@interface UIBezierPathView : UIView

- (instancetype) initWithBezierPath:(UIBezierPath *)bezierPath;

@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, strong) UIColor *strokeColor;

@end

UIBezierPathView.m

#import "UIBezierPathView.h"



@interface UIBezierPathView()

@property (nonatomic, strong) UIBezierPath *bezierPath;

@end

@implementation UIBezierPathView

- (id) initWithBezierPath:(UIBezierPath *)bezierPath
{
    self = [self initWithFrame:bezierPath.bounds];
    if(self)
    {
        self.bezierPath = bezierPath.copy;
        [self.bezierPath applyTransform:CGAffineTransformMakeTranslation(-CGRectGetMinX(self.frame), -CGRectGetMinY(self.frame))];
        self.backgroundColor = [UIColor clearColor];
        self.fillColor = [UIColor clearColor];
        self.strokeColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [self.strokeColor setStroke];
    [self.fillColor setFill];
    [self.bezierPath fill];
    [self.bezierPath stroke];
}

@end
Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43