需要帮助通过触摸将UIImageViews移动到圆圈中

时间:2011-07-18 20:33:11

标签: objective-c xcode

我正在设计一个涉及线性图的应用程序。我现在拥有的是一个有三个点的线:一个在中心位置,用于移动整条线,另外两个在线上,用于旋转它。我遇到的问题是当用户触摸其中任何一个时,弄清楚如何旋转另外两个点。

我实际上还没有对旋转点做太多,但到目前为止这是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:graph];
    if (CGRectContainsPoint(moveDot.frame, point)) {
        moveDotIsTouched = YES;
    }
    if (CGRectContainsPoint(rotateDot1.frame, point)) {
        rotateDot1IsTouched = YES;
    }
    if (CGRectContainsPoint(rotateDot2.frame, point)) {
        rotateDot2IsTouched = YES;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:graph];
    if (moveDotIsTouched) {
        if ((point.y > 0) && (point.y < 704))
        {
            if (point.y < 64) {rotateDot1.hidden = YES;}
            else {rotateDot1.hidden = NO;}
            if (point.y > 640) {rotateDot2.hidden = YES;}
            else {rotateDot2.hidden = NO;}
            moveDot.center = CGPointMake(moveDot.center.x, point.y);
            rotateDot1.center = CGPointMake(moveDot.center.x+64, moveDot.center.y-64);
            rotateDot2.center = CGPointMake(moveDot.center.x-64, moveDot.center.y+64);
            graph.base = -(point.y)/32 + 11;
            if (graph.base < 0) {[functionField setText:[NSString stringWithFormat:@"y = %.2fx %.2f", graph.slope, graph.base]];}
            else if (graph.base > 0) {[functionField setText:[NSString stringWithFormat:@"y = %.2fx + %.2f", graph.slope, graph.base]];}
            else {[functionField setText:[NSString stringWithFormat:@"y = %.2fx", graph.slope]];}
            [yintField setText:[NSString stringWithFormat:@"%.2f", graph.base]];
            [self changeTable];
        }
    }
    [graph setNeedsDisplay];
}

涉及点定位的数字以像素为单位测量。我真正想到旋转外部点的唯一方法是计算半径并确保点在半径范围内,但我想知道是否有更简单的东西。有没有人知道在一个圆圈中移动UIImageView的简单方法?

1 个答案:

答案 0 :(得分:1)

KTOneFingerRotationGestureRecognizer是用于在iOS应用中进行一次手指旋转的自定义UIGestureRecognizer。它跟踪手指围绕中心点的移动。我不知道这是否正是你想要的,但它可能会指出你正确的方向。