在panGesture发布后将UIView捕捉到设定位置

时间:2012-08-07 00:53:29

标签: objective-c uiview center uipangesturerecognizer xcode4.4

目前我正在玩 XCode ,但是没有Objective-C / Cocoa Touch等经验所以如果这是一个非常基本的问题请耐心等待。我只问你们,因为我几天来一直试图给谷歌一个答案,而且我感到厌倦了死胡同。

我正在尝试创建一个水平平移菜单。

我目前在主UILabels对象中持有3个UIView,该对象附加到UIPanGestureRecognizer

我使用handlePan方法根据用户的平移手势接收和翻译 * menuview UIView对象,但我需要找到UIView的中心释放 PanGesture 时的坐标,以计算将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轴上捕捉到三个点中的一个点,具体取决于用户释放UIViewPanGesture的中心位置。

目前,它的行为似乎忽略了“ recognizer.view.center.x ”if语句,因为无论它在何处发布它都会捕捉到 x = 122 (122 - menuview.center.x)

我已经尝试过这些if语句:

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

等,但也失败了。

当用户发布UIViews时,我需要在if PanGesture CURRENT位置创建操作时使用这些if语句?

谢谢,

克里斯

3 个答案:

答案 0 :(得分: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];
}

}

答案 1 :(得分: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];

答案 2 :(得分:0)

使用Interface Builder和Autolayout可以实现这一切。以下是我用来执行此操作的代码:

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

然后将那些IBOutlets连接到Interface Builder中的Horizo​​ntal Constraint(X Position)和Vertical Constraint(Y Position)。确保将约束连接到基本视图,而不是它最近的视图。

连接平移手势,然后使用以下代码拖动对象:

- (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){