拖动UIView将原点坐标设置回(0,0)

时间:2014-02-23 15:57:12

标签: ios objective-c

我有一个UIView我可以拖动,一旦我停止拖动并开始再次拖动UIViewUIView的坐标就会重新设置为0当我将UIView拖到特定位置时。

如果UIView设置为(x,y),我可以说(100,100)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"); }

{{1}}

1 个答案:

答案 0 :(得分:3)

替换行:

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

使用:

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

基本上,您总是将框架设置为初始翻译值。修复方法是将翻译值添加到视图的当前位置。

如果有帮助,请告诉我。