gpt4 book ai didi

objective-c - 绘制 NSRect 时出现问题

转载 作者:行者123 更新时间:2023-12-03 16:50:51 26 4
gpt4 key购买 nike

我的问题是,当 Action UpdateRect 调用drawRect方法时,我的矩形没有更新高度!

当我点击按钮时,我期望看到我的矩形高度为 20,但它仍然是 10。为什么?

@implementation Graphic_view

int height = 10; //The height of my rect.

-(IBAction)updateRect:(id)sender {
height += 10;
//Calling the drawrect method
[self performSelector:@selector(drawRect:)];
}

-(void)drawRect:(NSRect)dirtyRect {
NSLog(@"DrawRect has been called !");
// Drawing code here.
NSRect viewBounds = [self bounds];
NSColor *color = [NSColor orangeColor];
[colore set];
NSRectFill(viewBounds);
NSRect myRect;
myRect.origin.x = 20;
myRect.origin.y = 20;
myRect.size.height = height;
myRect.size.width = 100;
NSColor *whiteColor = [NSColor whiteColor];
[whiteColor set];
NSRectFill(myRect);
}

@end

最佳答案

你永远不应该自己调用drawRect:。而是调用 setNeedsDisplay:

-(IBAction)updateRect:(id)sender {
height += 10;
// Schedule the drawrect method
[self setNeedsDisplay:YES];
}

注意:iOS 等效项是不带参数的 setNeedsDisplay

关于objective-c - 绘制 NSRect 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7402727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com