前へ Xcc>Xcc>クォーツの世界 次へ

NSTimerを使う

ということで、 MyView.h を開いて

- (void)timerMethod:( NSTimer *)aTimer;
を追加
int startRect;
NSTimer* adjustWindowTimer;
をメンバ変数にして
@interface MyView : NSView
{
IBOutlet NSTextField *resulttime;
int startRect;
NSTimer* timer;
}
- (IBAction)setDrawCommand:(id)sender;
- (void)timerMethod:( NSTimer *)aTimer;
NSTimer は Foundation/NSTimer.h に定義とあるので
#import <Foundation/NSTimer.h>
を追加。

 次に、 MyView.m に戻って initWithFrame の // Add initialization code here と書いてるところに startRect の初期値を入れる。

- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
startRect = 0;
}
return self;
}
startRectを受け取れるように drawAlphaRects を
static void drawAlphaRects(CGContextRef context, int startRect)
とし drawRect で
drawAlphaRects(context, startRect);
とする。

ここで、実行させてみて、さっきの描画と変化がないことを確認したら、いよいよ timerMethod にかかる。

Command + Option + ↓

  xxx.m ファイルを編集中なら xxx と同じ名前の xxx.h ファイルを、 xxx.h ならその逆のファイルを開いてくれる。

timerMethodの実装

 定期的に実行される timerMethod では startRect を1つづつ値を増やしていき、再描画依頼である

[self setNeedsDisplay:YES]
をおこなう。
- (void)timerMethod:( NSTimer *)aTimer
{
startRect++;
if (startRect >= 60)
startRect = 0;
[self setNeedsDisplay:YES];
}
で、これを定期的に呼び出すタイマーを最初に用意した Draw>DoIt メニューで呼び出される setDrawCommand で作成。
- (IBAction)setDrawCommand:(id)sender
{
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0/60.0
target:self
selector:@selector( timerMethod:)
userInfo:0
repeats:YES
] retain];
}
として実行。 Draw>DoIt メニューを選ぶと〜。

逆回転やんけ〜。

setNeedsDisplayにたどり着く方法

 今までの要領で、探しなさい。

スタートポジションもばっちり。

ま、そりゃそうだわな。小中高と+α度は反時計回りだったわけだし startRect-- が時計回りだわな。
- (void)timerMethod:( NSTimer *)aTimer
{
startRect--;
if (startRect < 0)
startRect = 60 - 1;
[self setNeedsDisplay:YES];
}
と修正して、無事時計回り、最後に startRect の初期値を
startRect = 15;
として、これでパーペキになったわけですな。