0

I am trying to implement a Scratch Card effect in Swift 3 for my app, I found some examples but they're on Objective C (show scratch-card effect with the help of using scratch and win example)

In essence user swipes with finger on an image and reveals another image below their finger as they swipe.

Any one knows how to do this in Swift 3? Any help greatly appreciated

Community
  • 1
  • 1
Apneist
  • 396
  • 7
  • 17

3 Answers3

2

Check this Swift 3 example of ScratchCardImageView

A simple UIImageView subclass that allows your UIImageView become a scratch card.

enter image description here

sash
  • 8,423
  • 5
  • 63
  • 74
1

You can use ScratchCard library. It has simple API, but I don't know is it Swift 3 compatible. Anyway, you can rewrite it or check its implementation.

Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
  • Thank you sir! I am trying it out now – Apneist Nov 15 '16 at 13:15
  • So i ran it, XCODE translates it to Swift 3, but then it will not run when i press the "play" button(it says "build succeeded with no errors but nothing happens/no simulator opens), it is also missing assets (image before/after) and has no storyboard file. I am a newbie to programming, any hints on the basic steps i need to take to rewrite it from scratch, which storyboard instruments to use and which classes/functions? Thanks again – Apneist Nov 15 '16 at 13:25
  • 1
    Actually, this library doesn't have example project. You can create new project with Single View Application template, and manually add `ScratchUIView.swift` and `ScratchView.swift` into your project. Then you can add scratch view like that: – Artem Novichkov Nov 15 '16 at 13:32
  • 1
    let scratchCardView = ScratchUIView(frame: CGRectMake(50, 80, 320, 480),Coupon: "coupon image", MaskImage: "mask image", ScratchWidth: CGFloat(40)) self.view.addSubview(ScratchCardView) – Artem Novichkov Nov 15 '16 at 13:33
  • Much appreciated - I will try this – Apneist Nov 15 '16 at 13:42
  • Worked like a charm! Thanks a bunch @Artem Novichkov – Apneist Nov 15 '16 at 15:36
  • quick question - how can i extract the % of the image that has been scratched so far- say to display it on a label in the same view? I tried calling scratchCardView.getScratchPercent() on the label at TouchesBegan but it does nothing. Sorry if it's a noob question! – Apneist Nov 22 '16 at 17:46
  • One question - do you want to show scratch percent in real time? – Artem Novichkov Nov 23 '16 at 15:10
  • Yep preferably I would! – Apneist Nov 23 '16 at 15:12
  • 1
    This scratch view have only `getAlphaPixelPercent()` function. You can add delegate or block to catch percent changing. – Artem Novichkov Nov 23 '16 at 15:21
-1

I found a library that suits your requirement. It is Swift 3 compatible. Try this 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()
    }