Mac X 용 Aaron 's Cocoa 프로그래밍의 17 번째 장에 있습니다.이 예제에서는 NSView를 NSScrollView에 포함합니다.
연습을 위해 뷰에 프로그래밍 방식으로 NSButton을 추가했습니다.
문제는 스크롤 뷰에 처음 나타나는 버튼의 이상한 동작이지만 세로 스크롤러를 아래로 내려 가면 버튼이 사라지고 스크롤 뷰의 맨 아래에 다시 나타납니다.) 설명하기 어려운, 나는 비디오가 더 문제를 설명하기 위해 만든 : 책이 말하는 것처럼 내가 (클래스 StretchView를 NSView의 서브 클래 싱하고 불렀다NSView 및 NSScrollView : 다른 위치에서 사라지고 다시 나타나는 버튼
http://tinypic.com/player.php?v=k1sacz&s=6
).
#import <Cocoa/Cocoa.h>
@interface StretchView : NSView
{
@private
NSBezierPath* path;
}
- (NSPoint) randomPoint;
- (IBAction) click : (id) sender;
@end
#import "StretchView.h"
@implementation StretchView
- (void) awakeFromNib
{
// Here I add the button
NSView* view=self;
NSButton* button=[[NSButton alloc] initWithFrame: NSMakeRect(10, 10, 200, 100)];
[button setTitle: @"Click me"];
[button setTarget: self];
[button setAction: @selector(click:)];
[view addSubview: button];
}
- (IBAction) click:(id)sender
{
NSLog(@"Button clicked");
}
- (void) drawRect:(NSRect)dirtyRect
{
NSRect bounds=[self bounds];
[[NSColor greenColor] set];
[NSBezierPath fillRect: bounds];
[[NSColor whiteColor] set];
[path fill];
}
- (id) initWithFrame:(NSRect)frameRect
{
self=[super initWithFrame: frameRect];
if(self)
{
// here i dra some random curves to the view
NSPoint p1,p2;
srandom((unsigned int)time(NULL));
path=[NSBezierPath bezierPath];
[path setLineWidth: 3.0];
p1=[self randomPoint];
[path moveToPoint: p1];
for(int i=0; i<15; i++)
{
p1=[self randomPoint];
p2=[self randomPoint];
[path curveToPoint: [path currentPoint] controlPoint1: p1 controlPoint2: p2 ];
[path moveToPoint: p1];
}
[path closePath];
}
return self;
}
- (NSPoint) randomPoint
{
NSPoint result;
NSRect r=[self bounds];
result.x=r.origin.x+random()%(int)r.size.width;
result.y=r.origin.y+random()%(int)r.size.height;
return result;
}
@end
질문 :
이 코드입니다
1) 당사 버튼이 사라 않습니다 - 다시 어떻게이 문제를 방지하려면?
2) 곡선이 흰색으로 채워지는 이유는 무엇입니까? 나는 그들을 채우지 않은 작은 선으로 그려야했다.
Mac OS X Lion을 사용하고 있습니다. –
http://stackoverflow.com/questions/8689601/nsscrollview-scroll-bars-are-of-the-wrong-length와 동일한 문제가있을 수 있으며 이는 Lion의 버그 일 수 있습니다. – Vervious
네 말이 맞아, 방금 프레임의 프레임 크기와 높이를 바꿨으므로 더 이상이 문제가 발생하지 않았다. –