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

时间:2015-02-11 06:18:45

标签: ios objective-c uibezierpath cashapelayer cgpath

使用以下代码,我总是得到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);
}

2 个答案:

答案 0 :(得分:0)

我明白了。它只是用path.bounds设置属性边界。

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

答案 1 :(得分:0)

当我使用CAShapeLayer判断触摸是否在帧中时...... 我找到了与你的问题相同的问题。

所以当我创建CAShapeLayer时,我使用Associate添加框架。

#import <objc/runtime.h>

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

@implementation CAShapeLayer (frameCategory)

//http://stackoverflow.com/questions/28447744/frame-origin-and-bounds-size-of-cashapelayer-are-set-to-0

static char kAssociatedObjectKey;

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

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