-1

ユーザーが画面上の任意の場所をクリックすると、円が描画されます。このコードに何が欠けている/間違っていますか?

- (void)drawRect:(CGRect)rect
{
if (UITouchPhaseBegan)
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0, 0, 225, 1);
    CGContextSetRGBFillColor(context, 0, 0, 255, 1);
    CGRect rectangle = CGRectMake(50, 50, 500, 500);
    CGContextStrokeEllipseInRect(context, rectangle);
}

}
4

1 に答える 1

1

あなたのコードは、あなたが思っていることをしません。UITouchPhaseBeganinの定義を見てくださいUITouch.h:

typedef NS_ENUM(NSInteger, UITouchPhase) {
    UITouchPhaseBegan,             // whenever a finger touches the surface.
    UITouchPhaseMoved,             // whenever a finger moves on the surface.
    UITouchPhaseStationary,        // whenever a finger is touching the surface but hasn't moved since the previous event.
    UITouchPhaseEnded,             // whenever a finger leaves the surface.
    UITouchPhaseCancelled,         // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face)
};

これは単なる列挙値であり、アプリで何が起こっているかを反映したものではありません。この場合、列挙型の最初の値であるため、おそらくコンパイラによって 0 に設定されているため、常に false と評価されていると思います。

おそらくやりたいことは、 ivar を のように設定することですBOOL _touchHasbegun;。次に、-touchesBegan:withEventジェスチャ レコグナイザー アクションで、タッチ処理の方法に応じて、必要に応じて_touchHasBegunYES または NO に設定します。

ビューを更新する必要があることがわかっている場合は、メソッドを呼び出して[self setNeedsDisplay](または[self setNeedsDisplayInRect:someRect]可能であれば、パフォーマンスを向上させるために)-drawRect:メソッドをトリガーします。次に、円を描画するかどうかを決定するかどうかを-drawRect:メソッドで確認します。_touchHasBegun

注: 自分自身に電話をかけないで-drawRect:ください。ビューをダーティとして設定すると、OS が適切なタイミングでビューを描画します。

于 2013-02-25T21:58:55.613 に答える