用手指触摸在中心旋转图像 [英] Rotate image on center using one finger touch

查看:116
本文介绍了用手指触摸在中心旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一根手指触摸在中心点上旋转下面的图像...

I want to rotate the below image on a center point using one finger touch...

我想显示带有标签的图像的值我使用触摸旋转图像。

And i want to display the value of the image with the label when I rotate the image using touch.

我完成了图像旋转,但问题是如何根据旋转设置图像的值。

I have done the image rotation but the problem is that how to set the value of the image according to rotation.

旋转角度增加,所以我无法设置值。

The angle of the rotation is increase so i can not set the value.

任何人都可以帮助我吗?

Can any one help me?

代码低于

float fromAngle = atan2(firstLoc.y-imageView.center.y, 
                            firstLoc.x-imageView.center.x);

    NSLog(@"From angle:%f",fromAngle);
    float toAngle = atan2( currentLoc.y-imageView.center.y, 
                          currentLoc.x-imageView.center.x);

    NSLog(@"to Angle:%f",toAngle);
    // So the angle to rotate to is relative to our current angle and the
    // angle through which our finger moved (to-from)
    float newAngle =angle+(toAngle-fromAngle);


    NSLog(@"new angle:%.2f",newAngle);


 CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);

 imageView.transform=cgaRotate;


angle = newAngle;

任何人都可以帮助我吗?

Can any one help me ?

推荐答案

不完全确定你追求的是什么;但是试试这段代码。

Wasn't totally sure what you were after; but try out this code.

如果你创建一个名为'Rotation'的新的基于视图的应用程序项目并替换RotationViewController.h和.m中的代码以供下面你使用我会得到一个绿色块,您可以使用您的计算旋转。您可以使用UIImageView或其他任何想要旋转的内容替换绿色块UIView。

If you create a new View-based Application project called 'Rotation' and replace the code in RotationViewController.h and .m for the following you'll get a green block that you can rotate using your calculations. You can replace the green block UIView with your UIImageView, or anything else you want to spin.

#import <UIKit/UIKit.h>

@interface RotationViewController : UIViewController
{
    UIView* m_block;
    UILabel* m_label;
    CGPoint m_locationBegan;
    float m_currentAngle;
}

- (float) updateRotation:(CGPoint)_location;

@end






RotationViewController.m




RotationViewController.m

#import "RotationViewController.h"

double wrapd(double _val, double _min, double _max)
{
    if(_val < _min) return _max - (_min - _val);
    if(_val > _max) return _min - (_max - _val);
    return _val;
}

@implementation RotationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect blockFrame = CGRectMake(0, 0, 200, 200);
    m_block = [[UIView alloc] initWithFrame:blockFrame];
    m_block.backgroundColor = [UIColor greenColor];
    m_block.center = self.view.center;
    [self.view addSubview:m_block];
    [m_block release];

    CGRect labelFrame = CGRectMake(0, 0, 320, 30);
    m_label = [[UILabel alloc] initWithFrame:labelFrame];
    m_label.text = @"Loaded";
    [self.view addSubview:m_label];
}

- (void) touchesBegan:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    m_locationBegan = location;
}

- (void) touchesMoved:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    [self updateRotation:location];
}

- (void) touchesEnded:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    m_currentAngle = [self updateRotation:location];
}

- (float) updateRotation:(CGPoint)_location
{
    float fromAngle = atan2(m_locationBegan.y-m_block.center.y, m_locationBegan.x-m_block.center.x);
    float toAngle = atan2(_location.y-m_block.center.y, _location.x-m_block.center.x);
    float newAngle = wrapd(m_currentAngle + (toAngle - fromAngle), 0, 2*3.14);

    CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
    m_block.transform = cgaRotate;

    int oneInFifty = (newAngle*50)/(2*3.14);

    m_label.text = [NSString stringWithFormat:@"Angle: %f 1in50: %i", newAngle, oneInFifty];

    return newAngle;
}

@end

这篇关于用手指触摸在中心旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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