Equivalent of clipToBounds for Mac (NSView's layer)

2.6k views Asked by At

I have a class "GlobalView" which extends NSView and has two subviews GreenRectView (which extends NSView). I use the CALayer from my GreenRectView in order to be able to rotate (with setAffineTransform:) my view around a point other than the default bottom-left point. Thus I used "setAnchorPoint". Everything works well except that when my green rect is rotated , it is not draw outside the bounds of its view. In iOS it is possible to use clipToBounds:NO to accept that drawing is display outside bounds but in Mac it doesn't work with masksToBounds... How can I rotate my greenrect and display it entirely whatever the angle of rotation.

See picture : Screenshot

enter image description here And here is the code :

    @implementation GlobalView
- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        g = [[GreenRectView alloc]initWithPoint:NSMakePoint(200, 200)];
        [self addSubview:g];

        g = [[GreenRectView alloc]initWithPoint:NSMakePoint(100, 400)];
        [self addSubview:g];    
    }
    return self;
}

#import <Quartz/Quartz.h>
#include <ApplicationServices/ApplicationServices.h>

@implementation GreenRectView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {     
        [self setWantsLayer:YES];
        layer = [self layer];

        [self setLayer:layer];
        [layer setAnchorPoint:NSMakePoint(0.0,0.5)];
        [layer setMasksToBounds:NO];

        [menuLayer addSublayer:layer];

    }
    return self;
}

- (id)initWithPoint:(CGPoint)p {
    return [self initWithFrame:NSMakeRect(p.x, p.y, 120, 100)];
}

- (void)drawRect:(NSRect)dirtyRect {
    [[NSColor greenColor] setFill];
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,0, self.frame.size.width, self.frame.size.height) xRadius:5.5 yRadius:5.5];
    [path stroke];  
}
0

There are 0 answers