旋转UIImage以在iOS上调用方法

时间:2014-03-25 16:24:30

标签: ios uiimage core-animation

我正在拍摄图像并旋转它以调用方法。下面的图像里面有一个环。我要做的是在该图像上添加UIGestures以允许它自由旋转,同时保持锁定在x和y轴上的位置。旋转它会导致不同的文本出现在中间。有点像转动表盘来显示不同的选项。做这种事有什么好的教程吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

在Ray Wenderlich尝试the tutorial on creating a dial control

创建旋转效果的关键是捕捉视图上的触摸并将xy轴上的运动转换为旋转(container是要旋转的视图;你的图像视图,在这种情况下):

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event 
{
    // 1 - Get touch position
    CGPoint touchPoint = [touch locationInView:self];
    // 2 - Calculate distance from center
    float dx = touchPoint.x - container.center.x;
    float dy = touchPoint.y - container.center.y;
    // 3 - Calculate arctangent value
    deltaAngle = atan2(dy,dx); 
    // 4 - Save current transform
    startTransform = container.transform;
    return YES;
}

- (BOOL)continueTrackingWithTouch:(UITouch*)touch withEvent:(UIEvent*)event
{
    CGPoint pt = [touch locationInView:self];
    float dx = pt.x  - container.center.x;
    float dy = pt.y  - container.center.y;
    float ang = atan2(dy,dx);
    float angleDifference = deltaAngle - ang;
    container.transform = CGAffineTransformRotate(startTransform, -angleDifference);
    return YES;
}