【问题标题】:How can I draw one circle around each touch location on a device?如何在设备上的每个触摸位置周围画一个圆圈?
【发布时间】:2013-02-17 01:24:55
【问题描述】:

这里的新程序员试图一步一步来。我试图找到一种方法在设备上每个当前触摸的位置周围画一个圆圈。两个手指在屏幕上,每个手指下一个圆圈。

我目前有在一个触摸位置画一个圆圈的工作代码,但是一旦我将另一根手指放在屏幕上,圆圈就会移动到第二个触摸位置,而第一个触摸位置是空的。当我添加第三个时,它会移动到那里等等。

理想情况下,我希望屏幕上最多有 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 的 didFinishLaunchingWithOptions 方法中声明了 setMultipleTouchEnabled:YES

我曾尝试在 drawTouchCircle 方法中使用 if 语句,它基于 self.tapCountself.firstTouch.x 更改为 self.secondTouch.x 但这似乎破坏了整个事情,让我没有任何联系地点。

我很难找到我的问题,我知道这可能很简单。

【问题讨论】:

    标签: ios multi-touch touchesbegan


    【解决方案1】:

    我刚刚写了一些似乎可以工作的代码。我在视图中添加了一个名为circlesNSMutableArray 属性,其中包含每个圆圈的UIBezierPath。 在-awakeFromNib 中,我设置了数组并设置了self.multipleTouchEnabled = YES -(我认为您是使用对appDelegate.m 中的视图的引用来执行此操作的)。

    在视图中,我在-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];
    
    }
    

    然后我在-drawRect 中循环circles 并在每个上调用[circle stroke]

    【讨论】:

    • 请原谅我,因为我对此仍然很陌生,但我想我设法完成了你所说的工作,直到你说在 -drawRect 中循环遍历 circles。您的意思是遍历数组中的每个对象吗?我该怎么做呢?
    • 我的-drawRect 有循环:for(UIBezierPath *circle in _circles) { [circle stroke]; }。如果你想填充圆圈而不是绘制边界,你可以做[circle fill];。您还可以将圆形数据存储在CGRect 中并使用Core Graphics 函数进行绘制,就像您在上面所做的那样,但我认为这种方式更容易 - 因为您不能将CGRect 放入NSMutableArray,因为它是 typedef 而不是 Objective-C 对象。
    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多