2

setNeedsDisplayInRect:いくつかの描画コードを最適化するために iOS5 で使用しようとしています。セットアップは簡単です。ボタンとして機能する CGRect 'ホットスポット' の配列があります。タッチが検出されると、それが発生した CGRect を見つけ、setNeedsDisplayInRect:その rect をパラメーターとしてビューを呼び出します。すべての CGRects はビューに対して有効です。それらを使用して最初の描画を行い、正しく表示されます。

私が見ているのは (以下のコンソール ダンプが示すように) への最初の呼び出しsetNeedsDisplayInRect:が、指定した rect ではなく、ビュー フレームを rect として渡すことです。後続の呼び出しは正しいです。

誰かがこれをバグとして確認したり、ここで何か間違ったことをしていることを確認したりできますか? すべてのコードは以下です。 - ありがとう!

WRONG -> drawRect with rect 0.000000  0.000000  70.000000  660.000000 
drawRect with rect 15.000000  260.000000  40.000000  40.000000 
drawRect with rect 15.000000  310.000000  40.000000  40.000000 
drawRect with rect 15.000000  360.000000  40.000000  40.000000 
drawRect with rect 15.000000  410.000000  40.000000  40.000000 
drawRect with rect 15.000000  460.000000  40.000000  40.000000 
drawRect with rect 15.000000  510.000000  40.000000  40.000000 
drawRect with rect 15.000000  410.000000  40.000000  40.000000 
drawRect with rect 15.000000  310.000000  40.000000  40.000000 
drawRect with rect 15.000000  260.000000  40.000000  40.000000 
drawRect with rect 15.000000  110.000000  40.000000  40.000000 
drawRect with rect 15.000000  610.000000  40.000000  40.000000 
drawRect with rect 15.000000  510.000000  40.000000  40.000000 



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view]; 

    CGRect rect;
    int cnt = self.barNoteArray.count;
    for (int i = 0; i < cnt; i++) {
        rect = [[self.barNoteArray objectAtIndex:i] cellRect];
        if (CGRectContainsPoint(rect, point)) {

            self.bar.highlightIndex = 1;
            [self.bar setNeedsDisplayInRect:rect];

            break;
        }
    }
}



- (void)drawRect:(CGRect)rect
{
if (highlightIndex){

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextAddRect(context, rect);
    CGContextFillPath(context);

    highlightIndex = 0;

    printf("drawRect with rect %f  %f  %f  %f \n", rect.origin.x,
          rect.origin.y,
          rect.size.width,
          rect.size.height);
} else {
  // other drawing code


}
4

1 に答える 1

2

私もこの問題を抱えています。以下の状況に絞り込みました。

これは、ある UIView から 2 番目の UIView に移行したときに発生しているようです。私の場合、ユーザーは 1 つの UIView でペイント ツールを選択し、その下にある UIView (キャンバス) に描画します。

最初のストロークは、drawRect で受け取った rect を、そこに入れたものからフル ビュー サイズに変更するようです。これらの 1 つまたは 2 つの drawRects が描画されると、そこに入力した内容は変更されません。

問題のある行をコメントアウトすると drawRect が呼び出されなくなるため、それが setNeedsDisplayInRect であることを証明できます。呼び出しの直前の四角形は、更新する適切なサブ四角形を示し、drawRect の四角形は別の四角形を示します。

繰り返しますが、これはある UIView から別の UIView への最初の移動でのみ発生します (らしい)。

于 2012-04-01T00:57:58.073 に答える