0

我正在为 iPad 编写一个小型单词搜索游戏。我有一个UIViewController,我将字母随机平铺在UIView( lettersView) 中。我将标签的标签设置在一个 10x10 大小的矩阵中,UILabel每个字母使用一个。

这是我的问题:我不知道如何绘制字母(UILabels)。我已经研究过这个问题并且很困惑使用哪种方式(触摸,opengl,石英,hittest)。

  • 我只想从第一个点到最后一个点画一条直线(向下触摸)。
  • 当用户点击 时lettersView,我必须找到被点击的字母。如果我得到UILabel最先点击的那封信,我可以找到这封信。
  • 当用户移动他们的手指时,它应该画线,但是当用户移动它时,它必须删除旧的。
  • 当用户触摸时,我怎样才能得到最后一个UILabel?如果我得到第一个和最后一个 uilabel,我可以检查它是否可绘制。

(我发现了一个以 ARC 模式编写的小型绘图示例。但我无法将其转换为非 ARC。)


你说的我都做了。但我有一些问题。它在 drawRect 方法上画线。当我画第二行时,它会删除旧的。这是代码:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchStart = [[touches anyObject] locationInView:self];
    float xPos=touchStart.x;
    float yPos=touchStart.y;

    startIndexCol = floor(xPos / letterWidth);
    startIndexRow = floor(yPos / letterHeight);

    xPos = (startIndexCol * letterWidth) + letterWidth/2;
    yPos = (startIndexRow * letterHeight) + letterHeight/2;
    touchStart = CGPointMake(xPos, yPos);
    touchCurrent = touchStart;
    [self setNeedsDisplay];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchCurrent = [[touches anyObject] locationInView:self];
    //CGRect frame = CGRectMake(touchStart.x, touchStart.y, touchCurrent.x, touchCurrent.y);
    //[self setNeedsDisplayInRect:frame];
    [self setNeedsDisplay];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchCurrent = [[touches anyObject] locationInView:self];
    //[self hitTest:touchCurrent withEvent:event];
    float xPos=touchCurrent.x;
    float yPos=touchCurrent.y;

    endIndexCol = floor(xPos / letterWidth);
    endIndexRow = floor(yPos / letterHeight);

    xPos = (endIndexCol * letterWidth) + letterWidth/2;
    yPos = (endIndexRow * letterHeight) + letterHeight/2;
    touchCurrent = CGPointMake(xPos, yPos);
    //CGRect frame = CGRectMake(touchStart.x, touchStart.y, touchCurrent.x, touchCurrent.y);
    //[self setNeedsDisplayInRect:frame];


    //check if it can be drawn
    if ((startIndexCol == endIndexCol) && (startIndexRow == endIndexRow)) {
        //clear, do nothing
        touchStart = touchCurrent = (CGPoint) { -1, -1 };
        startIndexCol = startIndexRow = endIndexCol = endIndexRow = -1;
    }
    else{
        if (startIndexRow == endIndexRow) {//horizontal
            if (endIndexCol > startIndexCol) {
                [delegate checkWordInHorizontal:startIndexRow start:startIndexCol end:endIndexCol];
            }
            else{
                [delegate checkWordInHorizontalBack:startIndexRow start:startIndexCol end:endIndexCol];
            }
        }
        else if (startIndexCol == endIndexCol){ //vertical
            if (endIndexRow > startIndexRow) {
                [delegate checkWordInVertical:startIndexCol start:startIndexRow end:endIndexRow];
            }
            else{
                [delegate checkWordInVerticalBack:startIndexCol start:startIndexRow end:endIndexRow];
            }
        }
    }


    [self setNeedsDisplay];
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *subview = [super hitTest:point withEvent:event];
    if ([subview isEqual:self]) {
        NSLog(@"point:%f - %f", point.x, point.y);
        return self;
    }
    return nil;

}

-(void)drawLine{
    NSLog(@"draw it");
    drawIt=YES;
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if (drawIt) {
        NSLog(@"drawit");
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(ctx, 20.0);
        CGContextSetRGBStrokeColor(ctx, 1.0, 2.0, 0, 0.5);
        CGContextMoveToPoint(ctx, touchStart.x, touchStart.y);
        CGContextAddLineToPoint(ctx, touchCurrent.x, touchCurrent.y);

        CGContextStrokePath(ctx);
        drawIt=NO;
        touchStart = touchCurrent = (CGPoint) { -1, -1 };
        startIndexCol = startIndexRow = endIndexCol = endIndexRow = -1;
    }

        [[UIColor redColor] setStroke];
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:touchStart];
        [path addLineToPoint:touchCurrent];
        [path setLineWidth:20.0];
        [path strokeWithBlendMode:kCGBlendModeNormal alpha:0.5];
//        [path stroke];
}
4

1 回答 1

1

首先,UILabels 可能给您带来的麻烦多于好处。对于简单的网格,用手画字母drawRect:和连接线可能更容易。整体结构如下所示:

  • touchesBegan:withEvent:中,您将记下 ivar 中的起点。
  • touchesMoved:withEvent:中,您将更新 ivar 中的端点并调用[self setNeedsDisplayInRect:]. 通过你的两点定义的矩形。
  • touchesEnded:withEvent:中,您将进行命中测试,执行所需的任何处理,然后清除起点和终点并调用[self setNeedsDisplayInRect:].
    • 通过“进行命中测试”,我的意思是计算触摸下哪个字母。由于您将它们布置在网格中,因此这应该是非常简单的数学运算。
  • drawRect:中,您将使用[NSString drawAtPoint:withFont:]绘制每个字母。然后你会使用UIBezierPath来画线(你也可以使用CGPathRef,但我个人会使用 UIKit 对象)。
于 2013-01-03T22:56:08.907 回答