【发布时间】:2014-07-23 12:33:38
【问题描述】:
如附图所示,谁能指导我如何根据添加的 bezierpath 图层来剪辑背景视图。
提前致谢!
【问题讨论】:
-
需要裁剪顶部还是底部?
-
@iDev 需要剪辑顶部
标签: ios ios7 core-graphics uibezierpath
如附图所示,谁能指导我如何根据添加的 bezierpath 图层来剪辑背景视图。
提前致谢!
【问题讨论】:
标签: ios ios7 core-graphics uibezierpath
要剪辑顶部,您可以使用 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];
【讨论】:
您可以使用以下类:
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
【讨论】: