像卡在swift中的火种

时间:2018-01-23 16:00:14

标签: ios swift cardview tinder

在我的项目中,我有一个类似于火种卡视图的视图。我的观点运作良好,但我不确定是什么导致它“崩溃”。当我向右滑动时,卡片开始抖动并在整个地方跳跃,但是当我向左滑动时,我没有任何问题而且它很平滑。

https://youtu.be/h0r5RuOPe9g - >这是错误的视频

以下是我的代码的一些片段。

@IBAction func panCard(_ sender: UIPanGestureRecognizer) {

    let card = sender.view!

    let point = sender.translation(in: view)

    let xFromCenter = card.center.x - view.center.x

    card.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)

    let scale = min(100/abs(xFromCenter),1)

    card.transform = CGAffineTransform(rotationAngle: xFromCenter/divisor).scaledBy(x: scale, y: scale) //Rotating the card and scaling the card as it swipes to the left or right.

    if xFromCenter > 0{
        //Sets image to thumbs up if the card is moved right

        thumbImage.image = #imageLiteral(resourceName: "thisAccept") // This seems to be the section of the code that is causing the issue. the "thisAccept" image is .png and is in my asset file.

        thumbImage.contentMode = .scaleAspectFit
        thumbImage.tintColor = UIColor.green
    }else{
        //Sets image to thumbs down if the card is moved left
        thumbImage.image = #imageLiteral(resourceName: "Decline")
        thumbImage.contentMode = .scaleAspectFit
        thumbImage.tintColor = UIColor.red
    }

   thumbImage.alpha = abs(xFromCenter) / (view.center.x) // Fading the image as it swipes left or right
}

0 个答案:

没有答案