iPhone编程setNeedsDisplay不起作用 [英] iPhone Programming setNeedsDisplay not working

查看:45
本文介绍了iPhone编程setNeedsDisplay不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现了我自己的drawRect方法,我试图从Controller类重绘形状,但我不知道如何正确实现setNeedsDisplay以重绘UIView.请帮忙!

Implemented my own drawRect method and I'm trying to redraw the shape from a Controller class and I can't figure out how to correctly implement setNeedsDisplay to redraw the UIView. Please help!!

我知道代码很丑陋,但这是自定义方法:

I know the code is ugly, but here is the custom method:

- (void)drawRect:(CGRect)rect{
// Drawing code
NSArray *kyle = [self pointsForPolygonInRect:rect numberOfSides:[pshape numberOfSides]];
CGContextRef c = UIGraphicsGetCurrentContext();

int counter = [kyle count];
NSLog(@"counter: %d",counter);
int i = 0;
BOOL first = YES;

NSValue *kylevalue;
CGPoint thePoint;
for (i = 0; i < counter; i++) {
    kylevalue = [kyle objectAtIndex:i];
    thePoint = [kylevalue CGPointValue];
    if (first) { //start.
        CGContextMoveToPoint(c, thePoint.x, thePoint.y+5.0);
        first = NO;
    } else { //do the rest
        CGContextAddLineToPoint(c,  thePoint.x, thePoint.y+5.0);
    }
}
CGContextClosePath(c); //solid color
CGContextDrawPath(c, kCGPathFillStroke);    

}

推荐答案

我也遇到了类似的问题,即重绘无法按我的预期进行.对于那些需要调用其setNeedsDisplay方法的用户来说,setNeedsDisplay似乎并没有强迫子级重新绘制.

I've had similar problems with redrawing not working as I expected either. It seems as though setNeedsDisplay does not force children to redraw, for those you need to call their setNeedsDisplay method.

为满足我的需要,我编写了一个类别以通过在每个单个视图上调用setNeedsDisplay重绘整个屏幕.当然,可以轻松地将其修改为从特定视图开始,而不是从所有窗口开始.

For my needs I wrote a category to redraw the entire screen by calling setNeedsDisplay on every single view. This can of course easily be modified to start from a specific view as opposed to all windows.

@implementation UIApplication (Extensions)

+ (void) redrawScreen {
 NSMutableSet* todo = [NSMutableSet set];
 NSMutableSet* done = [NSMutableSet set];
 [todo addObjectsFromArray:[[UIApplication sharedApplication] windows]];

 while ([todo count] > 0) {
  UIView* view = [todo anyObject];
  [view setNeedsDisplay];

  [done addObject:view];
  [todo removeObject:view];

  if (view.subviews) {
   NSMutableSet* subviews = [NSMutableSet setWithArray:view.subviews];

   [subviews minusSet:done];
   [todo unionSet:subviews];
  }
 }
}

@end

希望这对某人有帮助

这篇关于iPhone编程setNeedsDisplay不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆