通过触摸绘制 CGRect

发布于 2024-10-03 16:09:40 字数 960 浏览 0 评论 0原文

我想知道如何将 CGRect 绘制到 UIImageView 上。这是我得到的代码,但它似乎不起作用。有什么建议吗? touch1 和 touch2 都是 CGPoint,point 是 CGRect。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch1 = [touch locationInView:self];
touch2 = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch2 = [touch locationInView:self];
point = CGRectMake(touch2.x, touch2.y, 50, 50);
[self setNeedsDisplay];
}

 - (void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, pointColor.CGColor);
CGContextAddEllipseInRect(context, point);
CGContextDrawPath(context, kCGPathFillStroke);
}

I was wondering how to draw a CGRect onto an UIImageView. Here is the code I've got but it doesn't seem to be working. Any suggestions? touch1 and touch2 are both CGPoints and point is a CGRect.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch1 = [touch locationInView:self];
touch2 = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch2 = [touch locationInView:self];
point = CGRectMake(touch2.x, touch2.y, 50, 50);
[self setNeedsDisplay];
}

 - (void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, pointColor.CGColor);
CGContextAddEllipseInRect(context, point);
CGContextDrawPath(context, kCGPathFillStroke);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北座城市 2024-10-10 16:09:40

您看到实际发生了什么?

一些注意事项:

  1. 您不应该使用 UIImageView 执行此操作 - 它们专门用于图像文件的容器。您应该为此使用常规 UIView 的子类。
  2. 在您的 touchesBegan 中,您知道 touch1touch2 将始终设置为相同的值,对吧?您似乎从未使用过 touch1。
  3. point 是一个误导性的变量名称,表示某个矩形。
  4. 你的drawRect并不是没有道理的。什么是pointColor?如果你画黑底黑字,这可能是问题的一部分。

What do you see actually happening?

A few notes:

  1. You shouldn't be doing this with a UIImageView-- those are specifically intended to be containers for image files. You should just use a subclass of a regular UIView for this.
  2. In your touchesBegan, you know that touch1 and touch2 will always be set to the same thing, right? You don't seem to ever be using touch1.
  3. point is a misleading variable name for something that is a rect.
  4. Your drawRect is not unreasonable. What is pointColor? If you're drawing black-on-black that might be part of the problem.
我纯我任性 2024-10-10 16:09:40

清单 3-1 通过对圆应用变换来创建椭圆的代码

CGContextScaleCTM(context, 1,2);
CGContextBeginPath(context);
CGContextAddArc(context, 0, 0, 25, 0, 2*M_PI, false);
CGContextStrokePath(context);

Quartz2d 示例

Listing 3-1 Code that creates an ellipse by applying a transform to a circle

CGContextScaleCTM(context, 1,2);
CGContextBeginPath(context);
CGContextAddArc(context, 0, 0, 25, 0, 2*M_PI, false);
CGContextStrokePath(context);

Lots more example code in the Quartz2d Example

朦胧时间 2024-10-10 16:09:40

我只拿了你的代码,它对我有用。看看吧。感谢您的代码。在此示例中,我绘制了一个矩形。

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();  
    CGContextSetLineWidth(context, 2.0);  
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);  
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
    CGContextAddRect(context, rectFrame);  
    CGContextDrawPath(context, kCGPathFillStroke);  

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self];
    rectFrame.origin.x = startPoint.x;
    rectFrame.origin.y = startPoint.y;
}  
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }

I took your code only, its working for me. Just have look at it. And thanks for your code. In this sample i am drawing a rect.

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();  
    CGContextSetLineWidth(context, 2.0);  
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);  
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
    CGContextAddRect(context, rectFrame);  
    CGContextDrawPath(context, kCGPathFillStroke);  

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self];
    rectFrame.origin.x = startPoint.x;
    rectFrame.origin.y = startPoint.y;
}  
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文