jimbob
jimbob

Reputation: 3298

Dragging an image flickering with touchesMoved

My image appears to be rendering itself underneath the position I touch every other update. The other time it does as expected.

I want the image to move by the offset amount from the last time it was moved without the flickering back to the touch position. How can I do that?

Here is the code:

int xposStamp;
int yposStamp;

int lastxposStamp;
int lastyposStamp;


- (void) touchesEnded: (NSSet *) touches
            withEvent: (UIEvent *) event
{
    lastxposStamp = xposStamp;
    lastyposStamp = yposStamp;
    NSLog(@"touchesEndedCalled");

}

- (void) touchesMoved: (NSSet *) touches
            withEvent: (UIEvent *) event
{
    if([touches count] == 1){

        CGPoint touchPoint = [[touches anyObject] locationInView:_photoImageView];

        CGRect rect = _draggableImageView.frame;

        int xOffset = touchPoint.x-rect.origin.x;
        int yOffset = touchPoint.y-rect.origin.y;

        rect.origin.x = lastxposStamp+xOffset;
        rect.origin.y = lastyposStamp+yOffset;

        _draggableImageView.frame = rect;

        //xpos and y pos of the stamp

        xposStamp = rect.origin.x;
        yposStamp = rect.origin.y;

        NSLog(@"xpos: %d, ypos: %d", xposStamp, yposStamp);
    }
}

Also here is an NSLog:

2013-08-19 14:25:52.898 JABImageMerge[2292:907] xpos: -11, ypos: 206
2013-08-19 14:25:52.914 JABImageMerge[2292:907] xpos: 133, ypos: 204
2013-08-19 14:25:52.930 JABImageMerge[2292:907] xpos: -16, ypos: 215
2013-08-19 14:25:52.946 JABImageMerge[2292:907] xpos: 128, ypos: 212
2013-08-19 14:25:52.962 JABImageMerge[2292:907] xpos: -19, ypos: 222
2013-08-19 14:25:52.978 JABImageMerge[2292:907] xpos: 124, ypos: 218
2013-08-19 14:25:52.994 JABImageMerge[2292:907] xpos: -21, ypos: 225
2013-08-19 14:25:53.010 JABImageMerge[2292:907] xpos: 121, ypos: 221
2013-08-19 14:25:53.026 JABImageMerge[2292:907] xpos: -22, ypos: 226
2013-08-19 14:25:53.042 JABImageMerge[2292:907] xpos: 120, ypos: 222
2013-08-19 14:25:53.058 JABImageMerge[2292:907] xpos: -21, ypos: 227
2013-08-19 14:25:53.074 JABImageMerge[2292:907] touchesEndedCalled

to setup a demo app that uses this code:

Hope someone can help me with this difficult problem. I am not the most mathematical programmer.

Upvotes: 2

Views: 762

Answers (2)

jimbob
jimbob

Reputation: 3298

The answer is as follows:

Add this in to your .m file.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Do not use TouchesMoved. Instead use the pan gesture to move around the view:

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{

    CGPoint translation = [panRecognizer translationInView:self.view];
    CGPoint imageViewPosition = self.draggableImage.center;
    imageViewPosition.x += translation.x;
    imageViewPosition.y += translation.y;

    self.draggableImage.center = imageViewPosition;
    [panRecognizer setTranslation:CGPointZero inView:self.view];

}

Upvotes: 2

Mikhail
Mikhail

Reputation: 4311

Try next code instead:

 CGPoint touchPoint = [[touches anyObject] locationInView:_photoImageView.superview];
 _photoImageView.center = touchPoint;

Or search for implementations of dragging views, there are many examples.

Upvotes: 0

Related Questions