我有一个UITableView*myTableView的ivar.我有从互联网加载的数据所以当数据加载时我应该调用[myTableView setNeedsDisplay],我知道我可以/应该调用[myTableView reloadData]但是混淆是调用setNeedsDisplay也应该工作但是不起作用.
我只是想找一个关于setNeedsDisplay做什么的解释?在UITableView实例上调用时.
根据文档setNeedsDisplay在"下一个绘图周期"中调用drawRect.所以任何人都可以告诉我下一个绘图周期是什么.
在我的代码中,我希望"动画"绘制一条线的延迟,所以在向视图添加新行之后,我调用setNeedsDisplay - 它工作正常一次.
在drawRect方法中,我绘制线并调用线的方法来增加line-lengthl.现在我想再次调用setNeedsDisplay来重绘该行 - 所以它得到了一个"成长"的动画.
但它只调用setNeedsDisplay一次并且再也不会调用,除非我添加另一行.我也尝试在这个类中调用一个方法,它调用setNeedsDisplay,以确保你不能在drawRect中调用它.
- (void)drawRect:(CGRect)rect {
for(GameLine *line in _lines) {
if(line.done) {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 5.0f);
CGContextSetStrokeColor(c, lineColor);
CGContextBeginPath(c);
CGContextMoveToPoint(c, line.startPos.x, line.startPos.y);
CGContextAddLineToPoint(c, line.endPos.x, line.endPos.y);
CGContextStrokePath(c);
}else {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 5.0f);
CGContextSetStrokeColor(c, delayColor);
CGContextBeginPath(c);
CGContextMoveToPoint(c, line.delayStartPos.x, line.delayStartPos.y);
CGContextAddLineToPoint(c, line.delayEndPos.x, line.delayEndPos.y);
CGContextStrokePath(c);
[line incrementDelayLine];
[self setNeedsDisplay];
}
}
}
Run Code Online (Sandbox Code Playgroud)
_lines是一个带有GameLine对象(非原子,保留)属性的NSMutableArray.
在我的情况下,在drawRect:调用每个单元后不会立即setNeedsDisplay调用.例如,我认为以下代码与我的情况相同.
for (int i = 0; i < 100; ++i)
{
[self setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的项目,涉及使用drawRect绘制一个形状,但我不相信我的drawRect函数实际上是被调用的.这是代码.(其他所有内容都只是"单一视图应用程序"的默认设置.)
-(void)drawRect:(CGRect)aRect{
NSLog(@"testing...");
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 120);
CGContextAddLineToPoint(context, 20, 100);
CGContextClosePath(context);
[[UIColor greenColor] setFill];
[[UIColor redColor] setStroke];
CGContextDrawPath(context,kCGPathFillStroke);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)
任何建议都会很棒 - 感谢阅读!
我有一个带有子视图的VC.在subview level我有一个按钮.我希望按钮导致视图更新.
该IBAction代码是在VC.它目前所做的只是切换一个BOOL值.
回到子视图中drawRect:方法是相应地测试BOOL和更改视图的代码.
我的观察是,当按下按钮时,IBAction代码会运行,切换BOOL,但drawRect:方法不会被调用.
我尝试手动调用drawRect(不是很好的做法而且没有用).我尝试调用setNeedsDisplay,但这也没有用(无法弄清楚如何引用以编程方式创建的子视图).
建议欢迎.
IBAction代码很简单,但包括在下面:
- (IBAction)rotateWindowButtonWasPressed:(id)sender
{
// just flip the boolean here. Need to make the code change in drawRect: in top2View.m
if ([myGlobals gWinVertical])
[myGlobals setGWinVertical:NO];
else
[myGlobals setGWinVertical:YES];
// Hoping this would force a redraw, but bounds have to change...:(
//_top2ViewOutlet.contentMode = UIViewContentModeRedraw;
}
Run Code Online (Sandbox Code Playgroud) 我setNeedsDisplay在一个新线程中调用该方法,但我没有看到我的视图中的任何更改.在调用setNeedsDisplay新主题后,我该怎么做才能看到我的所有更改?
drawrect ×4
ios ×4
objective-c ×4
iphone ×2
ibaction ×1
method-call ×1
uitableview ×1
xcode ×1