iOS-如何使对象一次只能拖动到一个方向

iOS-如何使对象一次只能拖动到一个方向,ios,objective-c,uiimageview,uigesturerecognizer,uiswipegesturerecognizer,Ios,Objective C,Uiimageview,Uigesturerecognizer,Uiswipegesturerecognizer,我正在尝试制作一个UIViewImage和一个UILabel可拖动,我遇到了一些问题,想知道如何制作视图,只能在一个方向上拖动,例如,我想将视图拖动到leftx或downy,但决不能同时向左和向下 我试着使用UISweepGestureRecognitor,查看我应该向哪个方向上、下或左、右移动,但事情并不像我想象的那样顺利 为了简化操作,这是viewController上的功能。h: 我将在viewDidLoad方法中添加滑动识别器,目前仅用于左侧: UISwipeGestureRecogn

我正在尝试制作一个UIViewImage和一个UILabel可拖动,我遇到了一些问题,想知道如何制作视图,只能在一个方向上拖动,例如,我想将视图拖动到leftx或downy,但决不能同时向左和向下

我试着使用UISweepGestureRecognitor,查看我应该向哪个方向上、下或左、右移动,但事情并不像我想象的那样顺利

为了简化操作,这是viewController上的功能。h:

我将在viewDidLoad方法中添加滑动识别器,目前仅用于左侧:

 UISwipeGestureRecognizer *detectSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(setSwipe:)];
[detectSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:detectSwipe];
这里是我设置swipeDirection属性的地方:

-(void)setSwipe:(UISwipeGestureRecognizer *)gesture {
    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) { // this is just for testing
    self.swipeDirection = gesture;
    NSLog(@"Gesture = %@", gesture);
    }


}
在这里,我实际上是想把东西移到左边:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches ) {
        CGPoint currentLocalation = [touch locationInView:touch.view];
        if (self.swipeDirection.direction == UISwipeGestureRecognizerDirectionLeft) {
            NSLog(@"Swipe Left");
            if (currentLocalation.x < startLocation.x) { // Startlocation is a global CGPoint.
                NSLog(@"Current Y = %f, starting Y = %f", currentLocalation.y, startLocation.y);

                CGFloat distance = startLocation.x - currentLocalation.x;
                CGPoint newPosition = CGPointMake(160 - distance, 170);
                CGPoint labelPosition = CGPointMake(160 - distance, 390);
                self.mainImage.center = newPosition;
                self.mainLabel.center = labelPosition;

            }

        } else {
            NSLog(@"Other direction");
        }

}

我该怎么办?我从错误的方向看这个?任何帮助都将不胜感激。

看起来您正试图使用滑动手势来完成不适合的操作,即跟踪手指移动和移动视图。这正是UIPangestureRecognitor的用途。下面是一个例子,说明如何使用平移手势:

首先,连接手势识别器:

 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPan:)];
[self.view addGestureRecognizer:panGesture];
然后,处理手指移动:

-(void)onPan:(UIPanGestureRecognizer*)recognizer
{
    // Find out how far the finger has moved (+x is right, -x is left, +y is down, -y is up)
    CGPoint movement = [recognizer translationInView:recognizer.view];

    if(movement.x < 0){
        // Finger moved left by movement.x pixels
    }

    if(movement.y > 0){
        // Finger moved down by movement.y pixels
    }

    [recognizer setTranslation:CGPointZero inView:recognizer.view];
}

在这个例子中,我同时接受向左和向下的移动。如果用户同时拖动并离开,我将让您决定如何决定哪个方向优先。一个建议是比较x和y方向的绝对值,并对较大的值采取行动。看起来您试图使用滑动手势来处理不适合的事情,即跟踪手指移动和移动视图。这正是UIPangestureRecognitor的用途。下面是一个例子,说明如何使用平移手势:

- (void)panDetected:(UIPanGestureRecognizer *)panGestureRecognizer {

    if(panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
        // record the point of the touch - decide whether the pan is moving an
        // object or misses everything and should be ignored
    } else if(panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        // animate the object that might be dragged in the direction you allow, and back,
        // if the gesture point moves around a bit
    } else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // if the gesture ends with a net movement in the allowed direction, finalize the
        // object's move else restore its original position with an animation back 
        // to its original place
    }
}
首先,连接手势识别器:

 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPan:)];
[self.view addGestureRecognizer:panGesture];
然后,处理手指移动:

-(void)onPan:(UIPanGestureRecognizer*)recognizer
{
    // Find out how far the finger has moved (+x is right, -x is left, +y is down, -y is up)
    CGPoint movement = [recognizer translationInView:recognizer.view];

    if(movement.x < 0){
        // Finger moved left by movement.x pixels
    }

    if(movement.y > 0){
        // Finger moved down by movement.y pixels
    }

    [recognizer setTranslation:CGPointZero inView:recognizer.view];
}
在这个例子中,我同时接受向左和向下的移动。如果用户同时拖动并离开,我将让您决定如何决定哪个方向优先。一个建议是比较x和y方向的绝对值,并对较大者采取行动

- (void)panDetected:(UIPanGestureRecognizer *)panGestureRecognizer {

    if(panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
        // record the point of the touch - decide whether the pan is moving an
        // object or misses everything and should be ignored
    } else if(panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        // animate the object that might be dragged in the direction you allow, and back,
        // if the gesture point moves around a bit
    } else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // if the gesture ends with a net movement in the allowed direction, finalize the
        // object's move else restore its original position with an animation back 
        // to its original place
    }
}
在手势识别器处理程序中,这正是您想要的


这就是你想要的,在手势识别器处理程序中。

我会使用平移手势识别器,并锁定方向。我为我的轴设置了一个简单的typedef。默认值表示尚未设置。未设置轴时,它会查找用户启动的方向。在这种情况下,当用户在一个方向上移动20个点时,它将锁定。您希望为此设置一个最小阈值以确保预期结果

 typedef enum{
    elAxisUpDown,elAxisLeftRight, elAxisDefault
}elAxis;

-(IBAction)handlePan:(UIPanGestureRecognizer *)sender
{

    CGPoint testPoint = [sender locationInView:self.view];
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:

            // @property elAxis
            self.axis = elAxisDefault;

            // @property CGPoints
            self.startingPoint = testPoint;
            self.startingCenter = sender.view.center;
            break;
        case UIGestureRecognizerStateChanged:

            switch (self.axis)
            {
                case elAxisDefault:
                {
                    if (fabsf(testPoint.x-self.startingPoint.x) > fabsf(testPoint.y-self.startingPoint.y) && fabs(testPoint.x-self.startingPoint.x)>20) {
                        self.axis = elAxisLeftRight;
                    }
                    else if (fabsf(testPoint.x-self.startingPoint.x) < fabsf(testPoint.y-self.startingPoint.y) && fabs(testPoint.y-self.startingPoint.y)>20) {
                        self.axis = elAxisUpDown;
                    }
                    break;
                }
                case elAxisLeftRight:
                    self.button7.center = CGPointMake(self.startingCenter.x + (testPoint.x - self.startingPoint.x), self.startingCenter.y);

                    break;
                case elAxisUpDown:
                    self.button7.center = CGPointMake(self.startingCenter.x, self.startingCenter.y + (testPoint.y - self.startingPoint.y));
                    break;
                default:
                    break;
            }
            break;
        case UIGestureRecognizerStateEnded:
            self.axis = elAxisDefault;
            break;
        default:
            break;
    }
}

我会使用平移手势识别器,并锁定方向。我为我的轴设置了一个简单的typedef。默认值表示尚未设置。未设置轴时,它会查找用户启动的方向。在这种情况下,当用户在一个方向上移动20个点时,它将锁定。您希望为此设置一个最小阈值以确保预期结果

 typedef enum{
    elAxisUpDown,elAxisLeftRight, elAxisDefault
}elAxis;

-(IBAction)handlePan:(UIPanGestureRecognizer *)sender
{

    CGPoint testPoint = [sender locationInView:self.view];
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:

            // @property elAxis
            self.axis = elAxisDefault;

            // @property CGPoints
            self.startingPoint = testPoint;
            self.startingCenter = sender.view.center;
            break;
        case UIGestureRecognizerStateChanged:

            switch (self.axis)
            {
                case elAxisDefault:
                {
                    if (fabsf(testPoint.x-self.startingPoint.x) > fabsf(testPoint.y-self.startingPoint.y) && fabs(testPoint.x-self.startingPoint.x)>20) {
                        self.axis = elAxisLeftRight;
                    }
                    else if (fabsf(testPoint.x-self.startingPoint.x) < fabsf(testPoint.y-self.startingPoint.y) && fabs(testPoint.y-self.startingPoint.y)>20) {
                        self.axis = elAxisUpDown;
                    }
                    break;
                }
                case elAxisLeftRight:
                    self.button7.center = CGPointMake(self.startingCenter.x + (testPoint.x - self.startingPoint.x), self.startingCenter.y);

                    break;
                case elAxisUpDown:
                    self.button7.center = CGPointMake(self.startingCenter.x, self.startingCenter.y + (testPoint.y - self.startingPoint.y));
                    break;
                default:
                    break;
            }
            break;
        case UIGestureRecognizerStateEnded:
            self.axis = elAxisDefault;
            break;
        default:
            break;
    }
}

谢谢,我不知道这门课!你肯定需要知道起始点——或者你怎么知道,在这个方法的每个条目上,对象从哪里开始?@AdamEberbach setTranslation:inView:将重置转换,因此下次你输入这个方法时,转换将表示自上次调用该方法以来手指移动了多远,但是,假设平底锅最终以一个你不允许的方向的净运动而失败-如何将对象移回其原始位置?问题中没有关于失败案例的内容,我不认为这是必需的或预期的行为Tanks,不知道这个类!你肯定需要知道起始点——或者你怎么知道,在这个方法的每个条目上,对象从哪里开始?@AdamEberbach setTranslation:inView:将重置转换,因此下次你输入这个方法时,转换将表示自上次调用该方法以来手指移动了多远,但是,假设平底锅最终以一个你不允许的方向的净运动而失败-如何将对象移回其原始位置?问题中没有关于失败案例的内容,我不认为这是必需的或预期的行为