3

ここにいる新しいプログラマーは、一歩一歩物事を進めようとしています。デバイスで現在触れている各場所の周りに円を描く方法を見つけようとしています。画面上の 2 本の指、各指の下に 1 つの円。

現在、1 つのタッチ位置に円を描く作業コードがありますが、画面に別の指を置くと、円はその 2 番目のタッチ位置に移動し、最初のタッチ位置は空のままになります。3分の1を追加すると、そこに移動します。

理想的には、各指に 1 つずつ、画面上に最大 5 つのアクティブな円を表示できるようにしたいと考えています。

これが私の現在のコードです。

@interface TapView ()
@property (nonatomic) BOOL touched;
@property (nonatomic) CGPoint firstTouch;
@property (nonatomic) CGPoint secondTouch;
@property (nonatomic) int tapCount;
@end

@implementation TapView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];

NSArray *twoTouch = [touches allObjects];
    if(touches.count == 1)
    {
        self.tapCount = 1;
        UITouch *tOne = [twoTouch objectAtIndex:0];
        self.firstTouch = [tOne locationInView:[tOne view]];
        self.touched = YES;
        [self setNeedsDisplay];
    }
    if(touches.count > 1 && touches.count < 3)
    {
        self.tapCount = 2;
        UITouch *tTwo = [twoTouch objectAtIndex:1];
        self.secondTouch = [tTwo locationInView:[tTwo view]];
        [self setNeedsDisplay];
    } 
}
-(void)drawRect:(CGRect)rect
{
        if(self.touched && self.tapCount == 1)
        {
            [self drawTouchCircle:self.firstTouch :self.secondTouch];
        }
}
-(void)drawTouchCircle:(CGPoint)firstTouch :(CGPoint)secondTouch
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx,0.1,0.1,0.1,1.0);
    CGContextSetLineWidth(ctx,10);
    CGContextAddArc(ctx,self.firstTouch.x,self.firstTouch.y,30,0.0,M_PI*2,YES);
    CGContextStrokePath(ctx);
}

appDelegate.mのメソッドでsetMultipleTouchEnabled:YES宣言しました。didFinishLaunchingWithOptions

に基づいてdrawTouchCircleを変更するメソッドでif ステートメントを使用しようとしましたが、すべてが壊れているようで、タッチした場所に円がありません。self.firstTouch.xself.secondTouch.xself.tapCount

私は自分の問題を見つけようとして非常に苦労しています.

4

1 に答える 1

3

動作するように見えるコードを書きました。ビューにNSMutableArray呼び出されるプロパティを追加しました。これには、各円が含まれています。では、配列とセットをセットアップします-(appDelegate.mのビューへの参照を使用してこれを行ったと思います)。circlesUIBezierPath-awakeFromNibself.multipleTouchEnabled = YES

-touchesBeganビューでは、メソッドとメソッドでこのメソッドを呼び出し-touchesMovedます。

-(void)setCircles:(NSSet*)touches
{   
    [_circles removeAllObjects]; //clear circles from previous touch

    for(UITouch *t in touches)
    {
        CGPoint pt= [t locationInView:self];
        CGFloat circSize = 200; //or whatever you need
        pt = CGPointMake(pt.x - circSize/2.0, pt.y - circSize/2.0);
        CGRect circOutline = CGRectMake(pt.x, pt.y, circSize, circSize);
        UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circOutline];
        [_circles addObject:circle];
    }
    [self setNeedsDisplay];
}

終了したタッチは次のとおりです。

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{ 
    [_circles removeAllObjects];
    [self setNeedsDisplay];

}

次に、ループしcirclesて、それぞれ-drawRectを呼び出します[circle stroke]

于 2013-02-17T02:17:10.493 に答える