如何将 NSBezierPath 附加到 CAShapeLayer [英] How to attach an NSBezierPath to a CAShapeLayer

查看:32
本文介绍了如何将 NSBezierPath 附加到 CAShapeLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮忙.基本上我在下面有一些 Objective C 代码,有人知道我如何将它附加到 Mac OS X 而不是 iOS 的 CAShapeLayer 吗?.

I was wondering if someone could please help. Basically I have some Objective C code below, does anyone know how I can attach this to a CAShapeLayer for Mac OS X not iOS?.

****//// 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.


Basically I use the application Paintcode and would like to create shapes then convert them to a CAShapeLayer that will be used to animate.

任何帮助将不胜感激.

问候.

推荐答案

CAShapeLayer 的 path 属性是一个 CGPathRef,但是,与 iOS 的 [UIBezierPathCGPath],令人惊讶的是没有任何内置方法将 OSX NSBezierPath 转换为 CGPathRef.但是,在 Apple 文档中,他们在 https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CocoaDrawingGuide/Paths/Paths.html#//apple_ref/doc/uid/TP40003290-CH206-SW2:

CAShapeLayer's path property is a CGPathRef, but, unlike with iOS's [UIBezierPath CGPath], there surprisingly isn't any built-in method to convert an OSX NSBezierPath to a CGPathRef. However, in the Apple docs they post a snippet to do the conversion at 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

您可以粘贴到此类别中,然后使用以下命令在图层上设置形状:

You could paste in this category, and then set the shape on your layer with:

layer.path = [starPath quartzPath];

这篇关于如何将 NSBezierPath 附加到 CAShapeLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆