0

我正在尝试绘制一个跟随屏幕触摸的标尺。在第一次触摸时,我设置了第一个点,在所有其他点上,标尺跟随屏幕上的手指调整大小并根据手指的位置围绕第一个点旋转。

我试图这样做:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.view];

    self.regleFirstPoint = p;
    UIImageView* img = [[UIImageView alloc] initWithImage:self.regleImg];
    img.frame = CGRectMake(self.regleFirstPoint.x, self.regleFirstPoint.y, 0, self.regleImg.size.height);
    [self.view addSubview:img];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.view];

    // Width
    float deltaY = p.y - self.regleFirstPoint.y;
    float deltaX = p.x - self.regleFirstPoint.x;
    float width = sqrt((deltaX * deltaX) + (deltaY * deltaY));

    // Angle
    float angleInRadians = atanf(deltaY / deltaX);
    float angleInDegrees = angleInRadians * 180 / M_PI; // JUST FOR INFO
    NSLog(@"angle : %f / %f", angleInRadians, angleInDegrees);

    // Resizing image
    UIImageView* img = [self.regles lastObject];
    img.frame = CGRectMake(self.regleFirstPoint.x, self.regleFirstPoint.y, width, self.regleImg.size.height/2);
    img.center = self.regleFirstPoint;
    CGAffineTransform transform = CGAffineTransformIdentity;
    img.transform = CGAffineTransformRotate(transform, angleInRadians);
}

标尺没有正确跟随手指,我想我错过了一些东西。我的代码有什么问题?

编辑:经过一些研究,我也尝试了这个:

// Resizing images
img.frame = CGRectMake(self.regleFirstPoint.x, self.regleFirstPoint.y, largeur, self.regleImg.size.height/2);
[img.layer setAnchorPoint:CGPointMake(self.regleFirstPoint.x / img.bounds.size.width, self.regleFirstPoint.y / img.bounds.size.height)];
img.transform = CGAffineTransformRotate(img.transform, angleInRadians);
4

1 回答 1

0

这只是一个顺序问题:

  1. 将您的视图转换为身份
  2. 更改视图的框架
  3. 最后,应用您的转换

这是一段更新的代码,只是使用了 UIView 而不是您的图像:

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

    // Hold first touch
    self.regleFirstPoint = [touch locationInView:self.view];

    // Reset view / image
    _rulerView.transform = CGAffineTransformIdentity;
    _rulerView.frame = CGRectMake(self.regleFirstPoint.x, self.regleFirstPoint.y, 0, CGRectGetHeight(_rulerView.frame));
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.view];

    // Compute width
    float deltaX = p.x - self.regleFirstPoint.x;
    float deltaY = p.y - self.regleFirstPoint.y;
    float width = sqrt((deltaX * deltaX) + (deltaY * deltaY));

    // Compute angle
    float angleInRadians = atan2(deltaY, deltaX);
    NSLog(@"angle (rad) : %f", angleInRadians);
    NSLog(@"angle (deg) : %f", angleInRadians * 180 / M_PI);

    // First reset the transformation to identity
    _rulerView.transform = CGAffineTransformIdentity;

    // Set anchor point
    _rulerView.layer.anchorPoint = CGPointMake(0.0f, 0.5f);

    // Resizing view / image
    _rulerView.frame = CGRectMake(self.regleFirstPoint.x, self.regleFirstPoint.y, width, CGRectGetHeight(_rulerView.frame));

    // Reset the layer position to the first point
    _rulerView.layer.position = self.regleFirstPoint;

    // Apply rotation transformation
    _rulerView.transform = CGAffineTransformMakeRotation(angleInRadians);
}

希望有帮助。

西里尔

于 2013-09-05T13:14:57.107 回答