2015-02-11 83 views
1

使用下面的代碼,我總是得到shapeLayer.frame.origin.x = 0的形狀shapeLayer.frame.origin.y = 0 shapeLayer.bounds.size。 width = 0 shapeLayer.bounds.size.height = 0 有沒有人有想法?謝謝您的回答。CAShapeLayer的frame.origin和bounds.size設置爲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); 
} 

回答

0

我知道了。它只是用path.bounds設置屬性邊界。

但這種縮小的形狀,並修改它的位置時,沒有邊界被設置爲

0

當我使用CAShapeLayer判斷觸摸是在框架... 我發現同樣的問題,你的問題。

因此,我使用關聯來添加一個幀當我創建CAShapeLayer

#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); 
}