UIImage跟随触摸 [英] UIImage which follow touches

查看:185
本文介绍了UIImage跟随触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图画一个ruller,它跟随屏幕上的触摸。在第一次触摸时,我设置了第一个点,在所有的另一个,ruller根据手指在屏幕上调整大小和旋转的第一点,根据手指的地方。

I'm trying to draw a ruller which follow the touches on the screen. On the first touch, I set the first point and on all the other, the ruller follow the finger on ther screen resizing and rotating around the first point depending on where the finger is.

我试图这样做:

- (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);
}

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

The ruller doesn't follow the finger correctly, I think I missed something. What's wrong with my code ?

EDIT :我也经过一些研究尝试过:

EDIT : I also tried this after some researches :

// 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);


推荐答案

这只是一个顺序问题:


  1. 设置视图转换为身份

  2. 更改视图框架

  3. 最后,应用您的变​​换

这是一段更新的代码,只是使用了UIView, / p>

Here is an updated piece of code, just used an UIView instead of your image:

- (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);
}

希望有帮助。

Cyril

这篇关于UIImage跟随触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆