作者yllan (蓝永伦)
看板MacDev
标题Re: [问题] NSView制作动画
时间Thu Feb 19 17:49:52 2009
※ 引述《dryman (dryman)》之铭言:
: 我想使用-(IBAction)start :(id)sender //这是一个按钮
: 按下後,才开始绘图
: 原本我这样写:
: @implementation movement
^^^^^^^^ 改名成 Movement 较好,Class 应该是 CamelCase。
: -(id)initWithFrame:(NSRect)frame{
: self=[super initWithFrame:frame];
: if(self){ [self setNeedDisplay:NO]; }
: return self;
: }
- (id) initWithFrame: (NSRect)frame
{
if ([super initWithFrame: frame]) {
_animating = NO;
}
return self;
}
: -(IBAction)start:(id)sender{
: ...
: [self setNeedDisplay:YES];
: }
- (IBAction) start: (id)sender
{
_animating = YES;
[NSTimer scheduledTimerWithTImeInterval: (1.0 / 24.0)
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES];
}
: -(void)handleTimer:(NSTimer*)timer{
: ...
: [self setNeedDisplay:YES];
: }
- (void) handleTimer: (NSTimer *)timer
{
if (!_animating) {
[timer invalidate];
return;
}
[self setNeedsDisplay: YES];
}
: -(void)drawRect:(NSRect)rect{
: ...
: timer = [NSTimer scheduledTimerWithTimeInterval: 0.0
: target: self
: selector: @selector(handleTimer:)
: userInfo: nil
: repeats: NO];
: }
- (void) drawRect: (NSRect)rect
{
....
if (...) {
_animating = NO;
}
}
没 compile 过,你试试看吧?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.132.180.14
1F:→ dryman:我还没试 谢谢教学^ ^ 02/20 23:20