show scratch-card effect with the help of using sc

2020-06-04 13:00发布

问题:

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

回答1:

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.

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.



回答2:

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

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



回答3:

You can also check this github tutorial for scratch:

  1. https://github.com/SebastienThiebaud/STScratchView
  2. https://github.com/moqod/iOS-Scratch-n-See