UIPanGesture没有移动个人观点

时间:2016-06-06 09:53:49

标签: ios swift uipangesturerecognizer tinder

我正在Swift中做一个iPhone项目,在我的homeScreen上,我在堆栈中添加了多个视图。

我的要求是能够将每个子视图移动到homeScreen之上,就像Tinder卡刷卡效果一样。我正在使用UIPanGesture来实现这一点,以便每个单独的视图都在屏幕上跟随我的手指。

但我的问题是,不是只移动一个屏幕所需的效果,而是所有视图堆叠在我的主屏幕上一起移动。 我在过去的4天里遇到了这个问题。请帮助我。

Xcode版本7.2.1和Swift 2.1

以下是移动UIView的代码:

//For creating each childView and adding UIPanGestureRecognizer to each childView

func configureInitialViewPlacement() -> Void {
    for var i:Int = cardsArray.count-1; 0 <= i; i--
    {
        let cardView = cardsArray[i]
       let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "beingDragged:")


        cardView.addGestureRecognizer(panGestureRecognizer)

        self.view.addSubview(cardView)

        var frame = CGRectZero
        var originalFrame = self.view.bounds
        originalFrame = CGRectMake(originalFrame.origin.x+10,originalFrame.origin.y+10 , originalFrame.size.width-20, originalFrame.size.height-20)

        frame.size.height = originalFrame.size.height
        frame.size.width = originalFrame.size.width - CGFloat((2*CGFloat(i)*paddingOffset))
        frame.origin.x = originalFrame.origin.x + CGFloat((CGFloat(i)*paddingOffset))
        frame.origin.y = originalFrame.origin.y + CGFloat((CGFloat(i)*paddingOffset))

        cardView.frame = frame

        cardView.setContentViewForCard(cardDataArray[i])

    }
}

// Method For gestureRecognizer

func beingDragged(gestureRecognizer: UIPanGestureRecognizer) -> Void {
    xFromCenter = Float(gestureRecognizer.translationInView(self.view).x)
    yFromCenter = Float(gestureRecognizer.translationInView(self.view).y)

    switch gestureRecognizer.state {
    case UIGestureRecognizerState.Began:
        self.originPoint = self.view.center
    case UIGestureRecognizerState.Changed:
        let rotationStrength: Float = min(xFromCenter/ROTATION_STRENGTH, ROTATION_MAX)
        let rotationAngle = ROTATION_ANGLE * rotationStrength
        let scale = max(1 - fabsf(rotationStrength) / SCALE_STRENGTH, SCALE_MAX)

        self.view.center = CGPointMake(self.originPoint.x + CGFloat(xFromCenter), self.originPoint.y + CGFloat(yFromCenter))

        let transform = CGAffineTransformMakeRotation(CGFloat(rotationAngle))
        let scaleTransform = CGAffineTransformScale(transform, CGFloat(scale), CGFloat(scale))
        self.view.transform = scaleTransform
        self.updateOverlay(CGFloat(xFromCenter))
    case UIGestureRecognizerState.Ended:
        self.afterSwipeAction()
    case UIGestureRecognizerState.Possible:
        fallthrough
    case UIGestureRecognizerState.Cancelled:
        fallthrough
    case UIGestureRecognizerState.Failed:
        fallthrough
    default:
        break
    }
}


func afterSwipeAction() -> Void {
    let floatXFromCenter = Float(xFromCenter)
    if floatXFromCenter > ACTION_MARGIN {
        self.rightAction()
    } else if floatXFromCenter < -ACTION_MARGIN {
        self.leftAction()
    } else {
        UIView.animateWithDuration(0.3, animations: {() -> Void in
            self.view.center = self.originPoint
            self.view.transform = CGAffineTransformMakeRotation(0)
        })
    }
}

// For Right Swipe

func rightAction() -> Void {
    let finishPoint: CGPoint = CGPointMake(500, 2 * CGFloat(yFromCenter) + self.originPoint.y)
    UIView.animateWithDuration(0.3,
        animations: {
            self.view.center = finishPoint
        }, completion: {
            (value: Bool) in
            self.cardsArray[0].removeFromSuperview()
    })
    delegateforcard.cardSwipedRight(self.cardsArray[0])
}

// For Left Swipe

func leftAction() -> Void {
    let finishPoint: CGPoint = CGPointMake(-500, 2 * CGFloat(yFromCenter) + self.originPoint.y)
    UIView.animateWithDuration(0.3,
        animations: {
            self.view.center = finishPoint
        }, completion: {
            (value: Bool) in
            self.cardsArray[0].removeFromSuperview()
    })
    delegateforcard.cardSwipedLeft(self.cardsArray[0])
}

如果您需要更多说明,请与我们联系。 感谢。

1 个答案:

答案 0 :(得分:1)

我也在目标c中做了同样的事情,你可以从这里得到一个想法, 这是我的代码

            UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(cardMoving:)];


-(void)cardMoving:(UIPanGestureRecognizer *)gestureRecognizer
{
    xValueFromCenter = [gestureRecognizer translationInView:self].x; // if right positive(+) value, negative for left
    yValueFromCenter = [gestureRecognizer translationInView:self].y; // if swipe up positive(+), negative for down

    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:{
            originalPoint = self.center;
            break;
        };
        case UIGestureRecognizerStateChanged:{
            CGFloat rotationStrength = MIN(xValueFromCenter / ROTATION_STRENGTH, ROTATION_MAX);
            CGFloat rotationAngel = (CGFloat) (ROTATION_ANGLE * rotationStrength);
            CGFloat scale = MAX(1 - fabs(rotationStrength) / SCALE_STRENGTH, SCALE_MAX);
            self.center = CGPointMake(originalPoint.x + xValueFromCenter, originalPoint.y + yValueFromCenter);
            CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAngel);
            CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
            self.transform = scaleTransform;
            [self updateOverlay:xValueFromCenter];
            break;
        };
        case UIGestureRecognizerStateEnded: {
            [self afterSwipeAction];
            break;
        };
        case UIGestureRecognizerStatePossible:break;
        case UIGestureRecognizerStateCancelled:break;
        case UIGestureRecognizerStateFailed:break;
    }
}

-(void)updateOverlay:(CGFloat)distance
{
    if (distance > 0) {
        overlayView.Direction = GGOverlayViewDirectionRight;
    } else {
        overlayView.Direction = GGOverlayViewDirectionLeft;
    }
    overlayView.alpha = MIN(fabs(distance)/100, 0.7);
}

- (void)afterSwipeAction
{
    if (xValueFromCenter > ACTION_MARGIN) {
        [self rightAction];
    } else if (xValueFromCenter < -ACTION_MARGIN) {
        [self leftAction];
    } else { //for reseting the card
        [UIView animateWithDuration:0.3
                         animations:^{
                             self.center = originalPoint;
                             self.transform = CGAffineTransformMakeRotation(0);
                             overlayView.alpha = 0;
                         }];
    }
}