Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show scratch-card effect with the help of using scratch and win example

I am new in ios developement.i need to show scratch-card effect for an iPhone app after scratches if coupon number is visible i need to show alert message How can i do this?.i have download sample code from iPhone - Scratch and Win Example and also i have done showing below screen and scratches also works fine

if UILabel text is visible i want to show alert message How can i do this?

i have attached screen shot for your refference enter image description here

enter image description here

enter image description here

like image 345
Ravindhiran Avatar asked Dec 02 '13 08:12

Ravindhiran


2 Answers

i Found very nice Github example please take a look this and impliment as par you need hope this helps to you my Frnd.

CGScratch This is same thing that you want to apply.

enter image description here

review the code and check the visible area of Number else check che scration overImage is tatally removed of not if remove then show Alert.

like image 164
Nitin Gohel Avatar answered Oct 11 '22 15:10

Nitin Gohel


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

In your storyboard or xib set custom class of your UIImageView that represents your scratch image to ScratchCardImageView. Change lineType or lineWidth to change the appearance of the scratch lines.

Download example

enter image description here

Swift 3:

import UIKit


class ScratchCardImageView: UIImageView {

    private var lastPoint: CGPoint?

    var lineType: CGLineCap = .square
    var lineWidth: CGFloat = 20.0

    override func awakeFromNib() {
        super.awakeFromNib()

        isUserInteractionEnabled = true
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard  let touch = touches.first else {

            return
        }

        lastPoint = touch.location(in: self)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard  let touch = touches.first, let point = lastPoint else {

            return
        }

        let currentLocation = touch.location(in: self)
        eraseBetween(fromPoint: point, currentPoint: currentLocation)

        lastPoint = currentLocation
    }

    func eraseBetween(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()
    }
}

Updated

With this solution touch events will be tracked only inside the UIImageView bounds. If you need touch events to start already outside your scratchcard, see ScratchCardTouchContainer example

like image 24
sash Avatar answered Oct 11 '22 17:10

sash