1.cocoa アプリケーションを作成する (ドキュメントベースではない)
2.新しいクラス「StretchView」(サブクラスNSView)を作成します
3.Interface Builder を開き、「スクロール ビュー」をメイン ウィンドウにドラッグします。
4.「スクロールビュー」を選択し、クラス「StretchView」を設定します(クラスIDウィンドウで)
contentview のサイズは 500*500 で、strechview のサイズも 500*500 です (水平スクロールが有効になっています)。
次に、いくつかの数字(1、2、3、4 ......)を水平方向に次々と描き始めます。数値が範囲外の場合 (x 位置が 500 より大きい場合)、StretchView の幅を増やします。(この時点まではすべて正常に動作します)
次に、水平スクローラーを自動的に最後までスクロールして、StretchViewの幅を広げるたびに最後の数字が見えるようにしようとしました。
コードは次のとおりです。
//The timer is called every sec
-(void)myTimerAction:(NSTimer *) timer
{
NSLog(@"myTimerAction");
//......
int i = _myArray.count;
NSRect rect = [self frame];
int width = rect.size.width;
//The width between two number is 10
//When the x pos of current num is bigger then the scroll's width
if((i * 10) > width) {
//reset the width
width = i * 10;
[self setFrameSize:CGSizeMake(width, rect.size.height)];
//How to make it autoscroll???
//...............................
}
//......
[self setNeedsDisplay:YES];
}