【问题标题】:Scratch Card effect in Swift 3Swift 3 中的刮刮卡效果
【发布时间】:2016-11-15 13:08:42
【问题描述】:

我正在尝试在 Swift 3 中为我的应用程序实现刮刮卡效果,我找到了一些示例,但它们位于 Objective C (show scratch-card effect with the help of using scratch and win example)

本质上,用户用手指在图像上滑动,并在滑动时显示手指下方的另一张图像。

任何人都知道如何在 Swift 3 中做到这一点?非常感谢任何帮助

【问题讨论】:

    标签: ios swift xcode core-graphics


    【解决方案1】:

    查看ScratchCardImageView这个Swift 3示例

    一个简单的UIImageView 子类,可让您的UIImageView 成为一张刮刮卡。

    【讨论】:

    • 非常感谢 - 我会检查一下
    【解决方案2】:

    您可以使用ScratchCard 库。它有简单的 API,但我不知道它是否兼容 Swift 3。无论如何,你可以重写它或检查它的实现。

    【讨论】:

    • 谢谢先生!我现在正在尝试
    • 所以我运行了它,XCODE 将它转换为 Swift 3,但是当我按下“播放”按钮时它不会运行(它说“构建成功,没有错误但没有任何反应/没有模拟器打开),它也缺少资产(之前/之后的图像)并且没有故事板文件。我是编程新手,任何关于我需要从头开始重写它的基本步骤的提示,使用哪些故事板工具和哪些类/functions?再次感谢
    • 其实这个库没有示例工程。您可以使用单视图应用程序模板创建新项目,并手动将ScratchUIView.swiftScratchView.swift 添加到您的项目中。然后你可以像这样添加临时视图:
    • 让 scratchCardView = ScratchUIView(frame: CGRectMake(50, 80, 320, 480),Coupon: "coupon image", MaskImage: "mask image", ScratchWidth: CGFloat(40)) self.view .addSubview(ScratchCardView)
    • 这个草稿视图只有getAlphaPixelPercent() 功能。您可以添加委托或阻止以捕获百分比变化。
    【解决方案3】:

    我找到了一个符合您要求的库。它与 Swift 3 兼容。 试试这个SRScratchView

    ScreenShot

     var lineType: CGLineCap = .round
     var lineWidth: CGFloat = 30.0
    
     func scrachBetween(fromPoint: CGPoint, currentPoint: CGPoint) {
    
            UIGraphicsBeginImageContext(self.frame.size)
    
            image?.draw(in: self.bounds)
    
            let path = CGMutablePath()
            path.move(to: fromPoint)
            path.addLine(to: currentPoint)
    
            let context = UIGraphicsGetCurrentContext()!
            context.setShouldAntialias(true)
            context.setLineCap(lineType)
            context.setLineWidth(lineWidth)
            context.setBlendMode(.clear)
            context.addPath(path)
    
            context.strokePath()
    
            image = UIGraphicsGetImageFromCurrentImageContext()
    
    
            UIGraphicsEndImageContext()
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      相关资源
      最近更新 更多