【问题标题】:Clip UIView using UIBezierPath in iOS [duplicate]在 iOS 中使用 UIBezierPath 剪辑 UIView [重复]
【发布时间】:2014-07-23 12:33:38
【问题描述】:

如附图所示,谁能指导我如何根据添加的 bezierpath 图层来剪辑背景视图。

提前致谢!

【问题讨论】:

  • 需要裁剪顶部还是底部?
  • @iDev 需要剪辑顶部

标签: ios ios7 core-graphics uibezierpath


【解决方案1】:

要剪辑顶部,您可以使用 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];

【讨论】:

  • WWDC 2013 有一个很好的例子(我认为)。
【解决方案2】:

您可以使用以下类:

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

【讨论】:

  • 这对于他想要的渐变填充来说会很困难。
  • 我不明白会有什么困难。
  • 其实,没有。这只会重新创建他在问题中的图像。他想使用路径来掩盖背景图像。
  • 我认为这是另一种观点。
  • @SviatoslavYakymiv 我用过这个类,但是 bezierpath 没有填充。你能告诉我可能是什么原因吗?
猜你喜欢
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
相关资源
最近更新 更多