我有一个类“GlobalView”,它扩展了NSView,并有两个子视图GreenRectView(扩展了NSView)。 我使用我的GreenRectView中的CALayer,以便能够围绕默认左下角以外的点旋转(使用setAffineTransform :)我的视图。因此我使用了“setAnchorPoint”。一切都很好,除了当我的绿色矩形旋转时,它不会超出其视图的范围。在iOS中,可以使用clipToBounds:NO来接受绘图显示在边界外但在Mac中它不适用于masksToBounds ...如何旋转我的greenrect并完全显示它无论旋转角度。
见图:
这是代码:
@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];
}