2014-02-23 87 views
1

我有一個UIView,我可以拖動,一旦我停止拖動,並開始再次拖動UIViewUIView的座標設置回0甚至當我拖着UIView到特定位置。拖動的UIView設置原點座標回(0,0)

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"); 
} 

回答

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)]; 

基本上你是幀總是設置爲初始翻譯價值。修復方法是將翻譯值添加到視圖中的當前位置。

讓我知道它是否有幫助。

+0

:謝謝。現在完成它的工作。 – redoc01

+0

沒問題:)你也可以upvote我的答案? – RaffAl