1

我目前正在使用XCode,但没有使用 Objective-C / Cocoa Touch 等的经验,所以如果这是一个非常初级的问题,请耐心等待。我只问你们,因为我几天来一直在尝试用谷歌搜索答案,并且厌倦了死胡同。

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

我目前将 3UILabels放在一个主UIView对象中,该对象附加到UIPanGestureRecognizer.

我已经使用了一个handlePan 方法来根据用户的平移手势接收和翻译*menuview对象,但是我需要在 PanGesture被释放时找到它的中心坐标来计算将元素向后/向前捕捉到的位置。UIViewUIViewUIView

- (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 轴上的三个点之一,这取决于UIView用户释放PanGesture.

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

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

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

等,但这也失败了。

UIViews当用户释放他们的 CURRENT 位置时,我需要在这些 if 语句中添加什么来创建操作PanGesture

谢谢,

克里斯

4

3 回答 3

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

}

于 2012-08-07T01:14:56.527 回答
0

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

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

然后将它们IBOutlets连接到 Interface Builder 中的水平约束(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){
于 2014-03-20T16:05:11.743 回答
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];
于 2013-12-29T05:33:17.377 回答