0

基本上我想触摸一个图像以将其围绕中心旋转 90 度。拖动图像以移动它(不旋转)。

- (void) onImageTouched:(SPTouchEvent *)event
{
    SPImage *img = (SPImage *)event.target;
    SPTouch *drag = [[event touchesWithTarget:self andPhase:SPTouchPhaseMoved] anyObject];
    SPTouch *touch = [[event touchesWithTarget:self andPhase:SPTouchPhaseBegan] anyObject];
    float offsetX, offsetY;
    if(touch){
        SPPoint *initial = [touch locationInSpace:self];
        offsetX = initial.x - img.x;
        offsetY = initial.y - img.y;
    }
    if (drag) {
        SPPoint *dragPosition = [drag locationInSpace:self];
        NSLog(@"Touched (%f %f)",dragPosition.x, dragPosition.y);
        //img.x = dragPosition.x - offsetX;
        //img.y = dragPosition.y - offsetY;

    }
    else{
        img.pivotX = img.width / 2.0f;
        img.pivotY = img.height / 2.0f;
        NSLog(@"Rotated aboout(%f %f)",img.pivotX,img.pivotY);
        //img.rotation = SP_D2R(90);
    }

}

以上是我的代码。

当我拖动它时,它确实会移动,但图像的位置离我的指针很远。此外,在拖动的开始和结束时,图像会旋转。

当我点击它时,它会消失。(可能移动或旋转到屏幕外的某个地方)

有什么建议么?

4

1 回答 1

0

好的,我会做这样的事情:

- (void) onImageTouched:(SPTouchEvent *)event
{
    SPImage *img = (SPImage *)event.target;
    SPTouch *drag = [[event touchesWithTarget:self andPhase:SPTouchPhaseMoved] anyObject];
    SPTouch *touch = [[event touchesWithTarget:self andPhase:SPTouchPhaseBegan] anyObject];
    SPTouch *endTouch = [[event touchesWithTarget:self andPhase:SPTouchPhaseEnded] anyObject];
    float offsetX, offsetY;    
    static BOOL dragging = NO;
    if(touch){
        dragging = NO;
    } else if (drag) {
        dragging = YES;
        SPPoint *dragPosition = [drag locationInSpace:self];
        NSLog(@"Touched (%f %f)",dragPosition.x, dragPosition.y);
        img.x = dragPosition.x;
        img.y = dragPosition.y;
    }
    else if (endTouch) {
        if (!dragging) {
            img.pivotX = img.width / 2.0f;
            img.pivotY = img.height / 2.0f;
            NSLog(@"Rotated aboout(%f %f)",img.pivotX,img.pivotY);
            img.rotation = SP_D2R(90);
        }
    }

}

此变体应解决拖动时偏移的问题以及拖动时无论如何都会旋转的事实。

请注意方法中的静态变量(拖动),如果将其作为私有变量放在实现中会更好(我只是通过这样做节省了时间;))。

我没有测试这段代码,但这个想法应该很清楚(我希望)。

问候

于 2013-09-25T15:45:32.533 回答