gpt4 book ai didi

iphone - 在uiview上画直线

转载 作者:行者123 更新时间:2023-11-29 04:12:55 24 4
gpt4 key购买 nike

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

这是我的问题:我不知道如何绘制字母(UILabel)。我研究了这个问题,但很困惑该使用哪种方式(touch、opengl、quartz、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];
}

最佳答案

首先,UILabels 可能给您带来的麻烦多于好处。对于简单的网格,在 drawRect: 中手动绘制字母以及连接线可能会更容易。整体结构如下所示:

  • touchesBegan:withEvent: 中,您可以记下 ivar 中的起点。
  • touchesMoved:withEvent: 中,您将更新 ivar 中的端点并调用 [self setNeedsDisplayInRect:]。传递由两点定义的矩形。
  • touchesEnded:withEvent: 中,您将进行 HitTest ,执行所需的任何处理,然后清除起点和终点并调用 [self setNeedsDisplayInRect:].
    • “进行 HitTest ”是指计算哪个字母处于触摸状态。由于您将它们放在网格中,因此这应该是非常简单的数学计算。
  • drawRect: 中,您可以使用 [NSString drawAtPoint:withFont:] 绘制每个字母。然后,您将使用 UIBezierPath 来绘制线条(您也可以使用 CGPathRef,但我个人会使用 UIKit 对象)。

关于iphone - 在uiview上画直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14145688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com