【问题标题】:Problem drawing in custom UIView class自定义 UIView 类中的绘图问题
【发布时间】:2010-02-04 02:57:06
【问题描述】:

我有一个 UIView 子类,它在 drawRect 中有以下内容:

for(int j=0; j<[myArray count]; j++){
    if([myArray objectAtIndex:j]!=@""){    
        [[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1] set];
        CGContextFillRect(context, CGRectMake(55+(20*j),10,15,5));
        [[UIColor blackColor] set];
        [[myArray objectAtIndex:j] drawAtPoint:CGPointMake(55+(20*j),3+) withFont:myFont];
    }
}

我已经在 if 语句中登录并且它可以工作,但由于某种原因,我仍然在循环的每次迭代中绘制 CGContextFillRects,即使我当前的数组对象是 @""。

我对绘画还很陌生,所以如果我遗漏了一些重要而琐碎的东西,请见谅。

【问题讨论】:

    标签: iphone core-graphics


    【解决方案1】:

    在使用对象时,您不能使用 == 或 != 来测试相等性;你需要使用 -isEqualTo:。例如:

    for(int j=0; j<[myArray count]; j++){
        if(![[myArray objectAtIndex:j] isEqualTo: @""]){    
            [[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1] set];
            CGContextFillRect(context, CGRectMake(55+(20*j),10,15,5));
            [[UIColor blackColor] set];
            [[myArray objectAtIndex:j] drawAtPoint:CGPointMake(55+(20*j),3+) withFont:myFont];
        }
    }
    

    数组中的每个对象都是指向内存的指针。即使两个对象具有相同的逻辑内容(例如空字符串),它们也很可能指向不同的内存块。您要做的是比较这些内存块的内容,这就是 -isEqualTo: 所做的。

    【讨论】:

    【解决方案2】:

    if (![[myArray objectAtIndex:j] isEqualToString:@""]) {

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多