2014-07-15 9 views
6

में त्रिकोण आकार बनाना मैं इसमें त्रिकोण आइकन/आकार वाला बटन बनाने की कोशिश कर रहा हूं। मैंने Paintcode में त्रिकोण बनाया है, फिर बीज़र पथ की प्रतिलिपि बनाई है और इसे नीचे दिए गए बटन परत में जोड़ा है।UIButton

-(void)setupShowHideRouteButton { 
    UIBezierPath* bezierPath = UIBezierPath.bezierPath; 
    [bezierPath moveToPoint: CGPointMake(10.5, 8.5)]; 
    [bezierPath addCurveToPoint: CGPointMake(40.5, 8.5) controlPoint1: CGPointMake(39.5, 8.5) controlPoint2: CGPointMake(40.5, 8.5)]; 
    [bezierPath addLineToPoint: CGPointMake(26.39, 22.3)]; 
    [bezierPath addLineToPoint: CGPointMake(25.2, 23.5)]; 
    [bezierPath addLineToPoint: CGPointMake(10.5, 8.5)]; 
    [UIColor.blackColor setStroke]; 
    bezierPath.lineWidth = 1; 
    [bezierPath stroke]; 

    CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
    shapeLayer.frame = self.showHideRouteViewBtn.bounds; 
    shapeLayer.path = bezierPath.CGPath; 
    shapeLayer.fillColor = [UIColor clearColor].CGColor; 
    shapeLayer.strokeColor = [UIColor blackColor].CGColor; 
    shapeLayer.lineWidth = 2; 
    [self.showHideRouteViewBtn.layer addSublayer:shapeLayer]; 
} 

बहरहाल, यह काम करने के लिए प्रकट नहीं होता। मैं UIButton का पृष्ठभूमि रंग सेट कर रहा हूं, इसलिए मुझे पता है कि फ्रेम सही है और उम्मीद है कि आउटलेट काम कर रहा है, यह सिर्फ आकार जोड़ नहीं रहा है?

द्वितीय कार्य

UIBezierPath* bezierPath = UIBezierPath.bezierPath; 
[bezierPath moveToPoint: CGPointMake(10.5, 8.5)]; 
[bezierPath addCurveToPoint: CGPointMake(40.5, 8.5) controlPoint1: CGPointMake(39.5, 8.5) controlPoint2: CGPointMake(40.5, 8.5)]; 
[bezierPath addLineToPoint: CGPointMake(26.39, 22.3)]; 
[bezierPath addLineToPoint: CGPointMake(25.2, 23.5)]; 
[bezierPath addLineToPoint: CGPointMake(10.5, 8.5)]; 
[UIColor.blackColor setStroke]; 
bezierPath.lineWidth = 1; 
[bezierPath stroke]; 

// Create a mask layer and the frame to determine what will be visible in the view. 
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
CGRect maskRect = self.showHideRouteViewBtn.bounds; 

// Create a path with the rectangle in it. 
CGPathRef path = CGPathCreateWithRect(maskRect, NULL); 

// Set the path to the mask layer. 
maskLayer.path = bezierPath.CGPath; 

// Release the path since it's not covered by ARC. 
CGPathRelease(path); 

// Set the mask of the view. 
self.showHideRouteViewBtn.layer.mask = maskLayer; 

यह या तो काम नहीं किया।

+0

आप आकार परत की 'backgroundColor' सेट करते हैं परत – klcjr89

+0

मास्किंग का प्रयास करें, तो आप पुष्टि कर सकते हैं कि इसे सही ढंग से दृश्य के अंदर गठबंधन है? –

+0

@ troop231 - प्रश्न में दूसरा दृष्टिकोण देखें, यह मेरे लिए काम नहीं करता है या तो – StuartM

उत्तर

2

प्रयास करें निम्नलिखित:

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.frame = self.showHideRouteViewBtn.bounds; 
shapeLayer.path = bezierPath.CGPath; 
shapeLayer.fillColor = [UIColor clearColor].CGColor; 
shapeLayer.strokeColor = [UIColor blackColor].CGColor; 
shapeLayer.lineWidth = 2; 
self.showHideRouteViewBtn.layer.mask = shapeLayer; 
+0

धन्यवाद, यह चाल है। मैंने एक बेवकूफ गलती भी देखी जिस तरह से मैंने इसे हल किया ... रात में बहुत देर हो चुकी है, मेरा बुरा! धन्यवाद – StuartM

+0

खुशी हुई यह मदद की! कृपया स्वीकार करें – klcjr89

+0

को 5 मिनट का इंतजार करना होगा, आपको यह मिल जाएगा :) – StuartM