2011-01-16 7 views
1

私はinitメソッドで背景色をクリアするように設定したUIViewを持っています。 drawRectの2つの点の間に青い線を描きます。最初のポイントはビューの最初のタッチで、touchesMovedが呼び出されると2番目のポイントが変更されます。透明なUIViewでのQuartzCoreのパフォーマンス

線の一端が固定され、他端がユーザーの指で動くようにします。しかし、背景色がはっきりしているときは、ラインにはかなりの遅延があり、スキップします(滑らかではありません)。

バックグラウンドカラーを黒に変更すると(背景にsetgroundcolor行のコメントを外すだけで)、動きがはるかにスムーズになり、大きく見えます。背後のビューは表示されません。

どうすればこの問題を解決できますか? (iPad上では、私はシミュレータしか持っておらず、現在デバイスにアクセスできない)パフォーマンスがスムーズだが、バックグラウンドはクリアなので、バックグラウンドビューを見ることができる。

-(id) initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     //self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

-(void) drawRect:(CGRect)rect { 
if (!CGPointEqualToPoint(touchPoint, CGPointZero)) { 
    if (touchEnded) { 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGContextClearRect(context, rect); 
    } else { 

CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(context, rect); 
    CGContextSetLineWidth(context, 3.0f); 
    CGFloat blue[4] = {0.0f, 0.659f, 1.0f, 1.0f}; 
    CGContextSetAllowsAntialiasing(context, YES); 
    CGContextSetStrokeColor(context, blue); 
    CGContextMoveToPoint(context, startPoint.x, startPoint.y); 
    CGContextAddLineToPoint(context, touchPoint.x, touchPoint.y); 
    CGContextStrokePath(context); 

    CGContextSetFillColor(context, blue); 

    CGContextFillEllipseInRect(context, CGRectMakeForSizeAroundCenter(CGSizeMake(10, 10), touchPoint)); 
    CGContextFillEllipseInRect(context, CGRectMakeForSizeAroundCenter(CGSizeMake(10, 10), startPoint)); 
    } 
    } 
} 



-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.touchEnded = NO; 
    if ([[touches allObjects] count] > 0) 
     startPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self]; 
     touchPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self]; 
    [self setNeedsDisplay]; 
} 

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.touchEnded = NO; 
    if ([[touches allObjects] count] > 0) 
     touchPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self]; 
    [self setNeedsDisplay]; 
} 
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    self.touchEnded = YES; 
    [self setNeedsDisplay]; 
    [self removeFromSuperview]; 
} 
- (void)dealloc { 
    [super dealloc]; 
} 

答えて

1

まず、Simulatorではなくデバイスでパフォーマンスをテストする必要があります。グラフィック関連のもののほとんどは、のようにSimulatorでは異なっていますが、いくつかの方がはるかに高速で、あるものは驚くほどゆっくりです。

つまり、透明な背景がデバイス上でパフォーマンスの問題であることが判明した場合は、あまりにも多くのピクセルをCGContextClearRect()で埋めるために起こりそうです。私は私の頭の上から2つの解決策を考えることができます:あなたはCALayerののrenderInContext:メソッドを使用して基本的なビュー(秒)のスナップショットを作成し、それをクリアするのではなく、背景としてスナップショットを描くことができます

  • touchesBegan:withEvent:で。そうすることで、ビューを不透明にして、ビューの下にビューを合成する時間を節約できます。
  • 行を表す1ポイントの高さのCALayerを作成して、末尾が正しい位置になるようにtouchesMoved:withEvent:に変換することができます。行末を表すドットも同じです(CAShapeLayerはそれに最適です)。いくつかの醜い三角法が関与しますが、その性能は優れています。
関連する問題