2013-07-22 17 views
3

我想知道是否有人可以請求幫助。基本上我在下面有一些目標C代碼,有誰知道我可以如何將它附加到Mac OS X的CAShapeLayer而不是iOS ?.如何將NSBezierPath附加到CAShapeLayer


****//// Color Declarations 
NSColor* fillColor = [NSColor colorWithCalibratedRed: 0 green: 0.886 blue: 0.886 alpha: 1]; 
NSColor* strokeColor = [NSColor colorWithCalibratedRed: 0 green: 0 blue: 0 alpha: 1]; 
//// Star Drawing 
NSBezierPath* starPath = [NSBezierPath bezierPath]; 
[starPath moveToPoint: NSMakePoint(106, 91.5)]; 
[starPath lineToPoint: NSMakePoint(110.21, 82.56)]; 
[starPath lineToPoint: NSMakePoint(119.18, 86.7)]; 
[starPath lineToPoint: NSMakePoint(116.65, 77.15)]; 
[starPath lineToPoint: NSMakePoint(126.19, 74.56)]; 
[starPath lineToPoint: NSMakePoint(118.11, 68.86)]; 
[starPath lineToPoint: NSMakePoint(123.75, 60.75)]; 
[starPath lineToPoint: NSMakePoint(113.91, 61.58)]; 
[starPath lineToPoint: NSMakePoint(113.01, 51.74)]; 
[starPath lineToPoint: NSMakePoint(106, 58.7)]; 
[starPath lineToPoint: NSMakePoint(98.99, 51.74)]; 
[starPath lineToPoint: NSMakePoint(98.09, 61.58)]; 
[starPath lineToPoint: NSMakePoint(88.25, 60.75)]; 
[starPath lineToPoint: NSMakePoint(93.89, 68.86)]; 
[starPath lineToPoint: NSMakePoint(85.81, 74.56)]; 
[starPath lineToPoint: NSMakePoint(95.35, 77.15)]; 
[starPath lineToPoint: NSMakePoint(92.82, 86.7)]; 
[starPath lineToPoint: NSMakePoint(101.79, 82.56)]; 
[starPath closePath]; 
[fillColor setFill]; 
[starPath fill]; 
[strokeColor setStroke]; 
[starPath setLineWidth: 1]; 
[starPath stroke];**** 

基本上,我使用的應用程序Paintcode並想創建形狀,然後將它們轉換爲將要用來製作動畫一個CAShapeLayer。

任何幫助將不勝感激。

問候。

+0

我覺得這後會幫助http://stackoverflow.com/questions/15318918/how-to-make-my-uibezierpath-animated-with-cashapelayer – BigBadOwl

+0

我認爲該鏈接僅適用於iOS,正如我以前所見,我需要一些代碼爲OS X.無論如何,謝謝。 – Nightowl

回答

6

CAShapeLayer的路徑屬性是CGPathRef,但不同於與iOS的[UIBezierPath CGPath],有令人驚訝的是不是一個OSX NSBezierPath轉換爲CGPathRef任何內置的方法。然而,在蘋果文檔,他們張貼片段在https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CocoaDrawingGuide/Paths/Paths.html#//apple_ref/doc/uid/TP40003290-CH206-SW2做轉換:

@implementation NSBezierPath (BezierPathQuartzUtilities) 
// This method works only in OS X v10.2 and later. 
- (CGPathRef)quartzPath 
{ 
    int i, numElements; 

    // Need to begin a path here. 
    CGPathRef   immutablePath = NULL; 

    // Then draw the path elements. 
    numElements = [self elementCount]; 
    if (numElements > 0) 
    { 
     CGMutablePathRef path = CGPathCreateMutable(); 
     NSPoint    points[3]; 
     BOOL    didClosePath = YES; 

     for (i = 0; i < numElements; i++) 
     { 
      switch ([self elementAtIndex:i associatedPoints:points]) 
      { 
       case NSMoveToBezierPathElement: 
        CGPathMoveToPoint(path, NULL, points[0].x, points[0].y); 
        break; 

       case NSLineToBezierPathElement: 
        CGPathAddLineToPoint(path, NULL, points[0].x, points[0].y); 
        didClosePath = NO; 
        break; 

       case NSCurveToBezierPathElement: 
        CGPathAddCurveToPoint(path, NULL, points[0].x, points[0].y, 
             points[1].x, points[1].y, 
             points[2].x, points[2].y); 
        didClosePath = NO; 
        break; 

       case NSClosePathBezierPathElement: 
        CGPathCloseSubpath(path); 
        didClosePath = YES; 
        break; 
      } 
     } 

     // Be sure the path is closed or Quartz may not do valid hit detection. 
     if (!didClosePath) 
      CGPathCloseSubpath(path); 

     immutablePath = CGPathCreateCopy(path); 
     CGPathRelease(path); 
    } 

    return immutablePath; 
} 
@end 

你可以此類別中粘貼,然後設置你的層的形狀:

layer.path = [starPath quartzPath]; 
+0

你可能希望這是'CGPathRef quartzPath = [starPath quartzPath]; layer.path = quartzPath; CGPathRelease(quartzPath);否則你會泄漏返回的CGPathRef – rudy