【问题标题】:Objective C Resizing the Drawing AreaObjective C 调整绘图区域的大小
【发布时间】:2012-02-16 06:50:32
【问题描述】:

我正在尝试将我的绘图区域调整为更小的尺寸,但每当我这样做时,我都会得到这种输出(参见照片)。

当我开始写作时,它不会画一条线,只是拉伸一切。

我试图找出问题所在,但我知道的还不够。

这是我的代码:

viewDidLoad:

touchDraw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"opaque.png"]];
    touchDraw.frame = CGRectMake(0, 0, 768, 1024);
    [self.view addSubview:touchDraw];
    [self.view sendSubviewToBack:touchDraw];
    touchMoved = 0;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchSwiped = NO;
    UITouch *touch = [touches anyObject];

    endingPoint = [touch locationInView:self.view];
    endingPoint.y -= 35;

}

触摸:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchSwiped = YES;
    UITouch *touch = [touches anyObject];   
    currentTouch = [touch locationInView:self.view];
    currentTouch.y -= 35;
    UIGraphicsBeginImageContext(self.view.frame.size);
    [touchDraw.image drawInRect:CGRectMake(0, 0, 768, 1024)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    UIImage * textureColor = [UIImage imageNamed:@"Brush_Black.png"];

    CGPoint vector = CGPointMake(currentTouch.x - endingPoint.x, currentTouch.y - endingPoint.y);
    CGFloat distance = hypotf(vector.x, vector.y);
    vector.x /= distance;
    vector.y /= distance;
    for (CGFloat i = 0; i < distance; i += 1.0f) {
        CGPoint p = CGPointMake(endingPoint.x + i * vector.x, endingPoint.y + i * vector.y);
        [textureColor drawAtPoint:p blendMode:blendMode alpha:0.5f];
    }
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    endingPoint = currentTouch;

    touchMoved++;

    if (touchMoved == 1) {
        touchMoved = 0;
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(!touchSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.frame.size.width, touchDraw.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
        UIImage * textureColor = [UIImage imageNamed:@"Brush_Black.png"];
        [textureColor drawAtPoint:CGPointMake(endingPoint.x, endingPoint.y) blendMode:blendMode alpha:1.0f];
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

}

【问题讨论】:

  • 您是否在此项目中实施了撤消操作?如果是,请发布您的代码

标签: objective-c ios core-graphics uitouch cgpoint


【解决方案1】:

改变这一行

UIGraphicsBeginImageContext(self.view.frame.size);

这样

UIGraphicsBeginImageContext(touchDraw.frame.size);

这可能会有所帮助。

【讨论】:

  • 它起作用了,但是当我试图改变它的位置时,它再次给出了拉伸输出
  • @Crisn。改变什么是位置?请澄清。
猜你喜欢
  • 2023-03-21
  • 2012-08-20
  • 1970-01-01
  • 2011-11-26
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
相关资源
最近更新 更多