Question

I want to make sure that the motion moved with the button held down picture (as in some trategiyah). But the image shake when I move it. Here's the code:

// PROPERTY
@property (nonatomic, retain) UIView *gameView;

@property GameState gameState; // for current state == Game
@property CGPoint touchPoint;
@property CGPoint screenOffset;

// INICIALIZE
_gameView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UIGraphicsBeginImageContext(_gameView.frame.size);
[[UIImage imageNamed:iname] drawInRect:_gameView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

_gameView.backgroundColor = [UIColor colorWithPatternImage:image];
_gameView.userInteractionEnabled = YES;

[self.view addSubview:_gameView];

// METHODS
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    switch (_gameState) {
        case Start:
            break;

        case Game:
            if ([touch view] == _gameView) {
                _touchPoint = [touch locationInView:touch.view];
                NSLog(@"startTouch");
            }
            break;

        default:
            break;
    }
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint currentTouchPoint = [touch locationInView:touch.view];
    switch (_gameState) {
        case Start:
            break;

        case Game:
            if ([touch view] == _gameView) {
                NSLog(@"%f%f", [touch locationInView:touch.view].x, [touch locationInView:touch.view].y);

                currentTouchPoint.x += _screenOffset.x - _touchPoint.x;
                currentTouchPoint.y += _screenOffset.y - _touchPoint.y;

                _gameView.center = currentTouchPoint;
            }
            break;

        default:
            break;
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint endedTouchPoint = [touch locationInView:touch.view];
    switch (_gameState) {
        case Start:
            break;

        case Game:
            if ([touch view] == _gameView) {
                _screenOffset.x += endedTouchPoint.x - _touchPoint.x;
                _screenOffset.y += endedTouchPoint.y - _touchPoint.y;

                _gameView.center = _screenOffset;
                NSLog(@"endTouch");
            }
            break;

        default:
            break;
    }
}

Even if slightly moving the mouse - the image jumps. Here is what appears in the log:

startTouch
206.000000    319.000000
206.000000    338.000000
209.000000    342.000000
211.000000    367.000000
217.000000    393.000000
228.000000    435.000000
291.000000    580.000000
244.000000    470.000000
316.000000    620.000000
250.000000    470.000000
330.000000    620.000000
263.000000    468.000000
...
endTouch

At the same time I moved the pointer one direction. Where mistake?

I not want to move all objects. I find reason. If write:

_gameView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"field1.png"]];
_gameView.frame = [[UIScreen mainScreen] bounds]; 
//_gameView.userInteractionEnabled = YES; 

that's work, but if uncomment last string problem return. But it needs for my program =(

Was it helpful?

Solution

You can make sure the location of the touch is given in coordinates of AddView's parent view with the line:

CGPoint location = [touch locationInView:AddView.superview];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top