1

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

我的要求是能够像 Tinder 刷卡效果一样在 homeScreen 顶部移动每个单独的子视图。我正在使用 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])
}

如果您需要更多说明,请告诉我。谢谢。

4

1 回答 1

1

我在objective-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;
                         }];
    }
}
于 2016-06-06T10:20:23.013 回答