6

我有这个

CGRect container = CGRectMake(conX, conY, 220, 50);
    UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:container cornerRadius:5.0];
    [[UIColor blueColor] setFill];
    [path fillWithBlendMode:kCGBlendModeNormal alpha:0.7];

我想在它周围找一个灰色的蛀虫,在网上看了很多东西,但看不到一种容易做到的方法,这可能吗?

4

2 回答 2

20

如果您不反对使用图层,那么这样的事情可能对您有用:

//create triangle path
UIBezierPath* path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 30)];
[path addLineToPoint:CGPointMake(100, 30)];
[path addLineToPoint:CGPointMake(100, 0)];
[path addLineToPoint:CGPointMake(0, 30)];

//apply path to shapelayer 
CAShapeLayer* greenPath = [CAShapeLayer layer];
greenPath.path = path.CGPath;
[greenPath setFillColor:[UIColor greenColor].CGColor];
[greenPath setStrokeColor:[UIColor blueColor].CGColor];
greenPath.frame=CGRectMake(0, 0,100,30);

//add shape layer to view's layer
[[self layer] addSublayer:greenPath];
于 2012-08-23T01:04:29.213 回答
1

您可以使用此扩展获得 bezierPath。

let path = UIBezierPath(yourView.bounds, cornerRadius: 12) 

let layer = CAShapeLayer()
layer.masksToBounds = false
layer.path = path.cgPath
layer.strokeColor = UIColor.red.cgColor
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = 5
layer.addSublayer(topLayer)



extension UIBezierPath {

        static func borderPathInRect(_ drawRect: CGRect, cornerRadius: CGFloat) -> UIBezierPath {
            let path = UIBezierPath()
            path.move(to: CGPoint(x: drawRect.origin.x, y: cornerRadius))

            path.addArc(withCenter: CGPoint(x: cornerRadius, y: cornerRadius),
                        radius: cornerRadius,
                        startAngle: CGFloat.pi,
                        endAngle: 3/2*CGFloat.pi ,
                        clockwise: true)

            path.addLine(to: CGPoint(x: drawRect.size.width - cornerRadius, y: 0))
            path.addArc(withCenter: CGPoint(x: drawRect.size.width - cornerRadius, y: cornerRadius),
                        radius: cornerRadius,
                        startAngle: 3/2*CGFloat.pi,
                        endAngle:  2*CGFloat.pi,
                        clockwise: true)

            path.addLine(to: CGPoint(x: drawRect.size.width, y: drawRect.size.height  - cornerRadius))
            path.addArc(withCenter: CGPoint(x: drawRect.size.width - cornerRadius, y: drawRect.size.height - cornerRadius),
                        radius: cornerRadius,
                        startAngle: 0,
                        endAngle:  CGFloat.pi/2,
                        clockwise: true)

            path.addLine(to: CGPoint(x: cornerRadius, y: drawRect.size.height))
            path.addArc(withCenter: CGPoint(x: cornerRadius, y: drawRect.size.height - cornerRadius),
                        radius: cornerRadius,
                        startAngle: CGFloat.pi/2,
                        endAngle:  CGFloat.pi,
                        clockwise: true)

            path.addLine(to: CGPoint(x: 0, y: cornerRadius))
            path.lineJoinStyle = .round

            return path
        }
    }
于 2020-04-09T22:50:17.457 回答