redoc01
redoc01

Reputation: 2307

Dragging UIView sets origin coordinates back to (0, 0)

I have a UIView which i can drag, once i stop dragging and start to drag the UIView again the coordinates of the UIView are set back to 0 even when i dragged the UIView to a specific location.

How would i get its proper location when the UIView is set lets say (x,y) to (100,100)?

The code below just allow you to drag the UIView

-(void)move:(id)sender {

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

CGPoint translation = [sender translationInView:self.view.superview];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];


[self.view setFrame:CGRectMake(translation.y*4, translation.y*10, 320, 480)];
[uiWebView setFrame:CGRectMake(0, 0, 320-self.view.frame.origin.x, (320-self.view.frame.origin.x)/2)];
[uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"ResizeVideo('down', %0.0f, %0.0f)",320-self.view.frame.origin.x,uiWebView.frame.size.height]];

if(self.view.frame.origin.y > 0 && self.view.frame.origin.y < 400){

}
else{
    CGPoint localPoint = [self.view bounds].origin;
    CGPoint basePoint = [self.view convertPoint:localPoint toView:nil];
    if(basePoint.y < 0){
        NSLog(@"%0.0f",self.view.frame.origin.y);
        [self.view setFrame:CGRectMake(0, 0, 320, 480)];
        [uiWebView setFrame:CGRectMake(0, 0, 320-self.view.frame.origin.x, (320-self.view.frame.origin.x)/2)];
        [uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"ResizeVideo('down', %0.0f, %0.0f)",320-self.view.frame.origin.x,uiWebView.frame.size.height]];
    }
    if(self.view.frame.origin.y > 400){
        [self.view setFrame:CGRectMake(160, 400, 320, 480)];
        [uiWebView setFrame:CGRectMake(0, 0, 160, 80)];
        [uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"ResizeVideo('down', %0.0f, %0.0f)",320-self.view.frame.origin.x,uiWebView.frame.size.height]];
    }
}

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

    if(self.view.frame.origin.y > 200){

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.35];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        //[[sender view] setCenter:CGPointMake(200, 100)];
        [UIView commitAnimations];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [self.view setFrame:CGRectMake(160, 400, 320, 480)];
        [uiWebView setFrame:CGRectMake(0, 0, 160, 80)];         [UIView commitAnimations];
        [uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"ResizeVideo('down', %0.0f, %0.0f)",160.0,80.0]];
        outOfBounds1 = false;
        outOfBounds2 = false;

    }

    if(self.view.frame.origin.y < 200){

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.35];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        //[[sender view] setCenter:CGPointMake(160, 72)];
        [UIView commitAnimations];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [self.view setFrame:(CGRectMake(0, 0, 320, 480))];
        [uiWebView setFrame:CGRectMake(0, 0, 320, 144)];            [UIView commitAnimations];
        [uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"ResizeVideo('down', %0.0f, %0.0f)",320.0,144.0]];
        outOfBounds1 = false;
        outOfBounds2 = false;
    }
}


NSLog(outOfBounds1 ? @"Yes" : @"No");
}

Upvotes: 1

Views: 427

Answers (1)

Rafał Sroka
Rafał Sroka

Reputation: 40030

Replace line:

[self.view setFrame:CGRectMake(translation.y*4, translation.y*10, 320, 480)];

with:

[self.view setFrame:CGRectMake(self.view.frame.origin.x + translation.y*4,
                               self.view.frame.origin.y + translation.y*10, 
                               320, 
                               480)];

Basically you are always setting the frame to the initial translation values. The fix is to add the translation values to the current position og the view.

Let me know if it helped.

Upvotes: 3

Related Questions