CAShapeLayer 的 frame.origin 和 bounds.size 设置为 0

frame.origin and bounds.size of CAShapeLayer are set to 0

通过以下代码,我总是得到 shapeLayer.frame.origin.x = 0 shapeLayer.frame.origin.y =0 shapeLayer.bounds.size.width = 0 的值 shapeLayer.bounds.size.height=0 有人有想法吗?谢谢你的回答。

-(void) makeShape {
UIBezierPath * path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(408.000000,230.000000) ];
[path addCurveToPoint:CGPointMake(365.000000,251.000000) controlPoint1:CGPointMake(399.399994,236.000000) controlPoint2:CGPointMake(365.000000,239.000000) ];
[path addCurveToPoint:CGPointMake(309.000000,287.000000) controlPoint1:CGPointMake(353.799988,252.000000) controlPoint2:CGPointMake(309.000000,252.000000) ];
[path addLineToPoint:CGPointMake(236.000000,321.000000) ];
[path addLineToPoint:CGPointMake(207.000000,384.000000) ];
[path addCurveToPoint:CGPointMake(157.000000,416.000000) controlPoint1:CGPointMake(197.000000,388.000000) controlPoint2:CGPointMake(157.000000,390.000000) ];
[path addLineToPoint:CGPointMake(90.000000,489.000000) ];
[path addCurveToPoint:CGPointMake(49.000000,566.000000) controlPoint1:CGPointMake(81.800003,490.000000) controlPoint2:CGPointMake(49.000004,506.000000) ];
[path addCurveToPoint:CGPointMake(19.000000,640.000000) controlPoint1:CGPointMake(35.400002,567.000000) controlPoint2:CGPointMake(18.999998,568.000000) ];
[path addLineToPoint:CGPointMake(365.000000,800.000000) ];
[path addLineToPoint:CGPointMake(415.000000,700.000000) ];
[path  closePath];
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [[UIColor blackColor] CGColor];
shapeLayer.fillColor = [UIColor grayColor].CGColor;
shapeLayer.lineWidth = 1.5f;
shapeLayer.lineJoin = kCALineJoinRound;
shapeLayer.path = path.CGPath;
[self.view.layer addSublayer:shapeLayer];
float x= shapeLayer.frame.origin.x;
float y = shapeLayer.frame.origin.y;
float w= shapeLayer.bounds.size.width;
float h = shapeLayer.bounds.size.height;
NSLog(@"Point(%f,%f)",x,y);
NSLog(@"Size(%f,%f)",w,h);
}

我明白了。只是用 path.bounds.

设置 属性 边界

但是当没有设置边界时,这会缩小形状并修改它的位置

当我用CAShapeLayer判断touch是否在frame中时... 我发现与您的问题相同。

所以我在创建时使用 Associate 添加框架 CAShapeLayer

#import <objc/runtime.h>

// To solve CAShapeLayer Frame(0,0,0,0)

@implementation CAShapeLayer (frameCategory)

//

static char kAssociatedObjectKey;

- (void)setFrameValue:(id)frameValue {
    objc_setAssociatedObject(self, &kAssociatedObjectKey, frameValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)frameValue {
    return objc_getAssociatedObject(self, &kAssociatedObjectKey);
}