Swift - Tinder effect

前端 未结 4 1837
醉酒成梦
醉酒成梦 2020-12-29 00:02

How can I achieve Tinder effect in Swift?

I mean, I have an image and want to accept if I swipe to right and reject if I swipe to left.

I can do it with the

4条回答
  •  清酒与你
    2020-12-29 00:18

    Try this:

    https://github.com/cwRichardKim/TinderSimpleSwipeCards

    You can find a better solution here with rotation. See DraggableView.m

    -(void)beingDragged:(UIPanGestureRecognizer *)gestureRecognizer
    {
        //%%% this extracts the coordinate data from your swipe movement. (i.e. How much did you move?)
        xFromCenter = [gestureRecognizer translationInView:self].x; //%%% positive for right swipe, negative for left
        yFromCenter = [gestureRecognizer translationInView:self].y; //%%% positive for up, negative for down
    
        //%%% checks what state the gesture is in. (are you just starting, letting go, or in the middle of a swipe?)
        switch (gestureRecognizer.state) {
                //%%% just started swiping
            case UIGestureRecognizerStateBegan:{
                self.originalPoint = self.center;
                break;
            };
                //%%% in the middle of a swipe
            case UIGestureRecognizerStateChanged:{
                //%%% dictates rotation (see ROTATION_MAX and ROTATION_STRENGTH for details)
                CGFloat rotationStrength = MIN(xFromCenter / ROTATION_STRENGTH, ROTATION_MAX);
    
                //%%% degree change in radians
                CGFloat rotationAngel = (CGFloat) (ROTATION_ANGLE * rotationStrength);
    
                //%%% amount the height changes when you move the card up to a certain point
                CGFloat scale = MAX(1 - fabsf(rotationStrength) / SCALE_STRENGTH, SCALE_MAX);
    
                //%%% move the object's center by center + gesture coordinate
                self.center = CGPointMake(self.originalPoint.x + xFromCenter, self.originalPoint.y + yFromCenter);
    
                //%%% rotate by certain amount
                CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAngel);
    
                //%%% scale by certain amount
                CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
    
                //%%% apply transformations
                self.transform = scaleTransform;
                [self updateOverlay:xFromCenter];
    
                break;
            };
                //%%% let go of the card
            case UIGestureRecognizerStateEnded: {
                [self afterSwipeAction];
                break;
            };
            case UIGestureRecognizerStatePossible:break;
            case UIGestureRecognizerStateCancelled:break;
            case UIGestureRecognizerStateFailed:break;
        }
    }
    

提交回复
热议问题