2012-08-07 30 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){