ios - Swift - 火种效果

标签 ios cocoa-touch swift

如何在 Swift 中实现 Tinder 效果?

我的意思是,我有一张图片,如果我向右滑动则接受,如果向左滑动则拒绝。

我可以用下面的代码来做:

@IBAction func SwipeRight(sender: UISwipeGestureRecognizer) {
    UIView.animateWithDuration(1) {
        self.Imagem.center = CGPointMake(self.Imagem.center.x - 150, self.Imagem.center.y )
    }
    //other things after acception
}

@IBAction func SwipeLeft(sender: UISwipeGestureRecognizer) {
    UIView.animateWithDuration(1) {
        self.Imagem.center = CGPointMake(self.Imagem.center.x + 150, self.Imagem.center.y )
    }
    //other things after rejection
}

但这样用户就无法取消操作。我希望如果用户滑动到距离边缘(左或右)的增量距离,它会出现一个图像让用户知道,如果他结束移动, Action 就会发生。否则,用户可以在不结束运动的情况下返回大于delta的距离,并且该 Action 将被取消。

最佳答案

我要感谢提出解决方案的人。遵循我在 Stack Overflow 的很多人的大力帮助下开发的解决方案:

@IBAction func Arrastei(sender: UIPanGestureRecognizer) {
    var origem =  CGPoint(x: 0, y: 0)
    var translation : CGPoint = sender.translationInView(Imagem)

    var txy : CGAffineTransform = CGAffineTransformMakeTranslation(translation.x, -abs(translation.x) / 15)
    var rot : CGAffineTransform = CGAffineTransformMakeRotation(-translation.x / 1500)
    var t : CGAffineTransform = CGAffineTransformConcat(rot, txy);
    Imagem.transform = t

    if (translation.x > 100) {
        LbResultado.textColor = btVerdadeiro.textColor
        LbResultado.text = btVerdadeiro.text
        LbResultado.hidden = false
    } else {
        if (translation.x < -100) {
            LbResultado.textColor = btFalso.textColor
            LbResultado.text = btFalso.text
            LbResultado.hidden = false
        } else {
            LbResultado.hidden = true
        }
    }


    if sender.state == UIGestureRecognizerState.Ended {
        if (translation.x > 100) {
            objJogo.Rodada_Vigente!.Responder(true)
        } else {

            if (translation.x < -100) {
                objJogo.Rodada_Vigente!.Responder(false)
            } else {
                sender.view.transform = CGAffineTransformMakeTranslation(origem.x, origem.y)
                sender.view.transform = CGAffineTransformMakeRotation(0)
            }
        }
    }
}

此解决方案使用:

Imagem --> UIImageView - 接受或拒绝

LbResultado --> UITextView - 向用户显示他处于接受或拒绝区域

没有数学计算来设置旋转和平移。我使用的值给我带来了良好的视觉效果。

操作(接受和拒绝)区域是当用户将图像向左(拒绝)或向右(接受)拖动超过 100 像素时。如果用户在操作区域外结束移动,图像将返回到其原始位置。

如果有人建议改进此代码,我将很高兴。

关于ios - Swift - 火种效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582255/

相关文章:

objective-c - DropBox 应用内登录

ios - 如何在 UIScrollView 中捏缩放多个 UIImageViews

iphone - 我的 UIViewController 的中心属性是 "lying"

objective-c - NSKeyedArchiver 保存的是什么格式?

arrays - 从 Firebase 字典创建数组

ios - 实例成员不能用于类型 - Swift 错误

ios - iOS/解析查询未在数组中找到字符串匹配

ios - 将单词添加到 UITextField 时如何播放声音

ios - 你能继承 UITabBarItem 并用 xib 文件设置它吗?

ios - 返回 Nil 的文本字段