2012-08-07 29 views
1

後我玩的XCode的那一刻,但具有的Objective-C /可可觸摸等沒有經歷如此忍受我,如果這是一個非常基本的問題。我只問你們,因爲我一直在努力向谷歌提供一個答案,並對無聊的事情感到無聊。捕捉一個UIView到設定位置panGesture釋放

我想創建一個水平平移菜單。

我目前保持3 UILabelsUIView對象,其被附接至UIPanGestureRecognizer內部。

我使用了一個handlePan方法根據用戶的平移手勢來接收和翻譯* menuviewUIView對象,但我需要找到當PanGesture被釋放來計算,其中的UIView的中心座標將UIView元素向後/向前對齊。

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer { 
CGPoint translation = [recognizer translationInView:self.view]; 
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 150); 
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; 

if(recognizer.state == UIGestureRecognizerStateEnded) 
{ 
    if(recognizer.view.center.x >= 576);{ 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     menuview.transform = CGAffineTransformMakeTranslation((764 - menuview.center.x), 0); 
     [UIView commitAnimations]; 

    } 

    if(recognizer.view.center.x && menuview.center.x >= 264);{ 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     menuview.transform = CGAffineTransformMakeTranslation((406 - menuview.center.x), 0); 
     [UIView commitAnimations]; 

    } 

    if(recognizer.view.center.x < 264);{ 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     menuview.transform = CGAffineTransformMakeTranslation((122 - menuview.center.x), 0); 
     [UIView commitAnimations]; 

    } 

} 

}

你看,我試圖捕捉到一個三個點在x軸,不同的地方,當用戶發佈他們PanGestureUIView的中心。

目前,它充當如果它無視「recognizer.view.center.x」 if語句,因爲不管它被釋放它捕捉到X = 122(122 - menuview.center .x)

if語句與我嘗試了這些:

if(menuview.center.x >= 576);{... 

等,但也失敗。

如果用戶發佈PanGesture時當前的位置是UIViews

感謝,

克里斯

回答

2

這是我使用的東西,希望它會幫助你:

.H

#import <QuartzCore/QuartzCore.h> 

delcare這兩種漂浮

float firstX; 
float firstY; 

.m

-(void)moveHorizontallyAndVertically:(id)sender { 
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations]; 

// [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]]; 
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view]; 
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 
    firstX = [myView center].x; 
    firstY = [myView center].y; 


} 

translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); 

[myView setCenter:translatedPoint]; 

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) { 
    CGFloat finalX = translatedPoint.x + (0.0*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x); 
    CGFloat finalY = translatedPoint.y + (0.0*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y); 
    if(finalX < 0) { 
     finalX = 0; 
    } 
    else if(finalX > 320) { 
     finalX = 320; 
    } 
    if(finalY < 0) { 
     finalY = 0; 
    } 
    else if(finalY > 480) { 
     finalY = 480; 
    } 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
    [myView setCenter:CGPointMake(finalX, finalY)]; 
    [UIView commitAnimations]; 
} 

}

+0

嗨夥計,感謝回答! 這段代碼似乎不適合我,恐怕。這是說我正在使用一個未識別的'發件人',所以我將它換成了我在handlePan方法中使用的'識別器'。這擺脫了紅色的錯誤,但沒有奏效。 它也不喜歡「translatedPoint.x」,給了我一個紅色的失敗錯誤,並問我是否想將它改爲「translation.x」。但即使在改變之後,它似乎並沒有奏效。 UIView滑動罰款,但dones't沒有回到現在的任何位置... – 2012-08-07 11:44:34

+0

對不起,請參閱我的編輯。 – 2012-08-07 11:49:18

+0

啊,你是英雄!我對這件事很陌生,以至於把所有的元素都綁在一起,在商店裏的.h和.m的所有元素都需要一段時間才能沉入。非常感謝你的幫助。 – 2012-08-07 22:58:54

0

我這樣做也只是在動畫塊設置一個新的幀與CGRectMake。也在UIGestureRecognizerStateEnded中完成

[UIView animateWithDuration:0.1f 
         delay:0.0f 
        options:UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
           NSLog(@"before: %@",NSStringFromCGPoint(piece.center)); 
           piece.frame = CGRectMake(targImgView.frame.origin.x, 
           targImgView.frame.origin.y,                      
           targImgView.frame.size.width, 
           targImgView.frame.size.height);                  
           NSLog(@"after: %@",NSStringFromCGPoint(piece.center)); 
           } 
        completion:nil]; 
0

這是使用Interface Builder和Autolayout所有可能的。下面是我用來做這個代碼:

@property (weak,nonatomic) IBOutlet NSLayoutConstraint *buttonXConstraint; 
@property (weak,nonatomic) IBOutlet NSLayoutConstraint *buttonYConstraint; 

然後掛鉤這些IBOutlets到水平約束(X位置)和垂直方向制約(Y位置),在界面生成器。確保約束連接到基本視圖,而不是最接近的視圖。

胡克全景手勢,然後用這個下面的代碼來拖動你的對象:

- (IBAction)panPlayButton:(UIPanGestureRecognizer *)sender 
{ 
    if(sender.state == UIGestureRecognizerStateBegan){ 

    } else if(sender.state == UIGestureRecognizerStateChanged){ 
    CGPoint translation = [sender translationInView:self.view]; 

    //Update the constraint's constant 
    self.buttonXConstraint.constant += translation.x; 
    self.buttonYConstraint.constant += translation.y; 

    // Assign the frame's position only for checking it's fully on the screen 
    CGRect recognizerFrame = sender.view.frame; 
    recognizerFrame.origin.x = self.buttonXConstraint.constant; 
    recognizerFrame.origin.y = self.buttonYConstraint.constant; 

    // Check if UIImageView is completely inside its superView 
    if(!CGRectContainsRect(self.view.bounds, recognizerFrame)) { 
     if (self.buttonYConstraint.constant < CGRectGetMinY(self.view.bounds)) { 
      self.buttonYConstraint.constant = 0; 
     } else if (self.buttonYConstraint.constant + CGRectGetHeight(recognizerFrame) > CGRectGetHeight(self.view.bounds)) { 
      self.buttonYConstraint.constant = CGRectGetHeight(self.view.bounds) - CGRectGetHeight(recognizerFrame); 
     } 

     if (self.buttonXConstraint.constant < CGRectGetMinX(self.view.bounds)) { 
      self.buttonXConstraint.constant = 0; 
     } else if (self.buttonXConstraint.constant + CGRectGetWidth(recognizerFrame) > CGRectGetWidth(self.view.bounds)) { 
      self.buttonXConstraint.constant = CGRectGetWidth(self.view.bounds) - CGRectGetWidth(recognizerFrame); 
     } 
    } 

    //Layout the View 
    [self.view layoutIfNeeded]; 
} else if(sender.state == UIGestureRecognizerStateEnded){