Ios 拖动UIView将原点坐标设置回(0,0)
我有一个Ios 拖动UIView将原点坐标设置回(0,0),ios,objective-c,Ios,Objective C,我有一个UIView可以拖动,一旦我停止拖动并开始再次拖动UIView,UIView的坐标会被设置回0,即使我将UIView拖动到特定位置 当UIView设置为(x,y)到(100100)时,我如何获得它的正确位置 下面的代码只允许您拖动ui视图 -(void)move:(id)sender { [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations]; CGPoint translation = [sender
UIView
可以拖动,一旦我停止拖动并开始再次拖动UIView
,UIView
的坐标会被设置回0
,即使我将UIView
拖动到特定位置
当UIView
设置为(x,y)
到(100100)
时,我如何获得它的正确位置
下面的代码只允许您拖动ui视图
-(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");
}
-(无效)移动:(id)发送方{
[[(UITapgestureRecognitizer*)发送方视图]层]删除配置];
CGPoint translation=[sender translationView:self.view.superview];
CGPoint translatedPoint=[(UIPangestureRecognitor*)发送方翻译视图:self.view];
[self.view setFrame:CGRectMake(translation.y*4,translation.y*10320480)];
[uiWebView setFrame:CGRectMake(0,0320 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){
}
否则{
CGPoint localPoint=[self.view bounds]。原点;
CGPoint-basePoint=[self.view-convertPoint:localPoint-toView:nil];
if(基点y<0){
NSLog(@“%0.0f”,self.view.frame.origin.y);
[self.view setFrame:CGRectMake(0,0320480)];
[uiWebView setFrame:CGRectMake(0,0320 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(160400320480)];
[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([(UIPangestureRecognitizer*)发送方状态]==UIgestureRecognitizerStateEnded){
if(self.view.frame.origin.y>200){
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDuration:.35];
[UIView设置动画曲线:UIViewAnimationCurveEaseOut];
//[[sender view]设置中心:CGPointMake(200100)];
[UIView委员会];
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDuration:1.0];
[self.view setFrame:CGRectMake(160400320480)];
[uiWebView setFrame:CGRectMake(0,0,160,80)];[UIView committeanimations];
[uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@“ResizeVideo('down',%0.0f,%0.0f)”,160.0,80.0];
边界外1=假;
outOfBounds2=假;
}
if(self.view.frame.origin.y<200){
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDuration:.35];
[UIView设置动画曲线:UIViewAnimationCurveEaseOut];
//[[sender view]设置中心:CGPointMake(160,72)];
[UIView委员会];
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDuration:1.0];
[self.view setFrame:(CGRectMake(0,0320480));
[uiWebView setFrame:CGRectMake(0,03203214)];[UIView committeanimations];
[uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@“ResizeVideo('down',%0.0f,%0.0f)”,320.0144.0];
边界外1=假;
outOfBounds2=假;
}
}
NSLog(边界外1?@“是”:“否”);
}
替换行:
[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)];