3

我收到上述错误,但我不确定如何解决它,有人能告诉我为什么我会收到此错误,以便我可以修复它吗?

代码本身可以工作并将图像(文本层除外)绘制到屏幕上,所以我只想删除错误。我已经搜索了堆栈溢出并找到了“强制转换”,但是在尝试了这种技术之后,错误仍然存​​在。

我认为这是因为我正在使用这些方法

[CardElementsCreationClass drawHeart]

[CardElementsCreationClass drawValueWithSuit:suit AndValue:value]

我的.m

#import "CardCreationClass.h"

@implementation CardCreationClass

//--------------------
//Create a card
//
+ (UIView *) newPlayingCardWithSuit:(NSString *)suit
                          AndValue:(NSString *)value {  
    //Create playing card UIVIew
    CGRect playingCardBounds = CGRectMake(100, 50, 100, 200);
    //CGPoint playingCardPosition = CGPointMake(100, 50);

    UIView* playingCard = [[UIView alloc] initWithFrame:playingCardBounds];

    //CREATE BACK OF CARD
    //

    //CREATE FRONT OF CARD
    CALayer* front = [CALayer layer];

    //Determine suit
    CAShapeLayer *cardSuitShapeLayer;

    if ([suit isEqualToString:@"heart"]) {
        cardSuitShapeLayer = [CardElementsCreationClass drawHeart];
    }

    [front addSublayer:cardSuitShapeLayer];

    //Determine value
    CATextLayer *cardValueTextLayer = [CardElementsCreationClass drawValueWithSuit:suit AndValue:value];

    [front addSublayer:cardValueTextLayer];

    //Add layers to card
    [playingCard.layer addSublayer:front];

    NSLog(@"Type = %@", [playingCard class]);

    //Return - **ERROR HAPPENS HERE**
    return playingCard;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [CardCreationClass newPlayingCardWithSuit:@"heart" AndValue:@"A"];
}

@end

绘图代码

#import "CardElementsCreationClass.h"

@implementation CardElementsCreationClass

//--------------------
//HEARTS
//
+ (CGMutablePathRef)newHeartPath {
    //Declare
    CGMutablePathRef heartPath = CGPathCreateMutable();

    //Create shape
    CGPathAddEllipseInRect(heartPath, NULL, CGRectMake(0, 0, 20, 20));
    CGPathAddEllipseInRect(heartPath, NULL, CGRectMake(20, 0, 20, 20));

    CGPathMoveToPoint(heartPath, NULL, 37.5, 16.5);

    CGPathAddLineToPoint(heartPath, NULL, 37.5, 16.5);
    CGPathAddLineToPoint(heartPath, NULL, 20, 37.5);
    CGPathAddLineToPoint(heartPath, NULL, 2.5, 16.5);
    CGPathAddLineToPoint(heartPath, NULL, 20, 10);

    CGPathCloseSubpath(heartPath);

    //Return
    return heartPath;
}

+ (CGMutablePathRef)newHeartHighlightPath {
    //Declare
    CGMutablePathRef heartHighlightPath = CGPathCreateMutable();

    //Create hightlight
    CGPathAddArc(heartHighlightPath, NULL, 0, 0, 7, -(110 * M_PI) / 180, -(0 * M_PI), NO);

    CGPathAddLineToPoint(heartHighlightPath, NULL, 4, 0);

    CGPathAddCurveToPoint(heartHighlightPath, NULL, 4, -7, 0, -7, 0, -7);

    //Return
    return heartHighlightPath;
}

+ (CAShapeLayer*)drawHeart {
    CAShapeLayer* heartShapeLayer = [CAShapeLayer layer];

    //Context
    CGContextRef context = UIGraphicsGetCurrentContext();

    //Declare
    CGMutablePathRef heartPath = [self newHeartPath];

    /*CGPathAddArc(heartPath, NULL, 10, 10, 10, M_PI, 0, false); // Left hump
    CGPathAddArc(heartPath, NULL, 30, 10, 10, M_PI, 0, false); // Right hump
    CGPathAddLineToPoint(heartPath, NULL, 20, 37.5); // Pointy end

    CGPathCloseSubpath(heartPath);*/

    //Line
    CGContextSetLineWidth(context, 8.0);
    CGContextSetLineJoin(context, kCGLineJoinRound);

    //Colour shape
    CGContextSetCMYKStrokeColor(context, 0.17, 1, 1, 0.07, 1);
    CGContextSetCMYKFillColor(context, 0.07, 1, 1, 0.01, 1);

    //Draw shape
    CGContextTranslateCTM(context, 4, 4);

    CGContextAddPath(context, heartPath);
    CGContextDrawPath(context, kCGPathStroke);

    CGContextAddPath(context, heartPath);
    CGContextDrawPath(context, kCGPathFill);

    //Colour highlight
    CGContextSetCMYKFillColor(context, 0, 0, 0, 0, 0.3);

    //Left highlight
    CGMutablePathRef heartHightlightLeft = [self newHeartHighlightPath];

    //Draw highlight
    CGContextTranslateCTM(context, 10, 9);

    CGContextAddPath(context, heartHightlightLeft);
    CGContextDrawPath(context, kCGPathFill);

    //Right highlight
    CGMutablePathRef heartHightlightRight = [self newHeartHighlightPath];

    //Draw hightlight
    CGContextTranslateCTM(context, 20, 0);

    CGContextAddPath(context, heartHightlightRight);
    CGContextDrawPath(context, kCGPathFill);

    //Attach
    heartShapeLayer.path = heartPath;

    //Release
    CGPathRelease(heartPath);
    CGPathRelease(heartHightlightLeft);
    CGPathRelease(heartHightlightRight);

    //Return
    return heartShapeLayer;
}

//--------------------
//VALUES
//
+ (CATextLayer*)drawValueWithSuit:(NSString *)suit
                         AndValue:(NSString *)value {
    CATextLayer* valueTextLayer = [CATextLayer layer];

    valueTextLayer.string = value;
    valueTextLayer.borderWidth = 2.0;
    valueTextLayer.borderColor = [UIColor blackColor].CGColor;

    valueTextLayer.bounds = CGRectMake(0, 0, 50, 50);

    //Determine colour
    if ([suit isEqualToString:@"heart"] || [suit isEqualToString:@"diamond"]) {
        valueTextLayer.foregroundColor = [UIColor redColor].CGColor;
    } else if ([suit isEqualToString:@"club"] || [suit isEqualToString:@"spade"]) {
        valueTextLayer.foregroundColor = [UIColor blackColor].CGColor;
    }

    //Return
    return valueTextLayer;
}

@end
4

1 回答 1

2

只需重命名方法

+ (UIView *) newPlayingCardWithSuit:(NSString *)suit
                           AndValue:(NSString *)value

以不以单词开头的东西new。例如:

+ (UIView *) playingCardWithSuit:(NSString *)suit
                        andValue:(NSString *)value
于 2012-06-17T12:47:41.337 回答