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

后端 未结 3 521
终归单人心
终归单人心 2021-02-10 05:56

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

相关标签:
3条回答
  • 2021-02-10 06:39

    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

    0 讨论(0)
  • 2021-02-10 06:40

    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.

    0 讨论(0)
  • 2021-02-10 06:46

    You can also check this github tutorial for scratch:

    1. https://github.com/SebastienThiebaud/STScratchView
    2. https://github.com/moqod/iOS-Scratch-n-See
    0 讨论(0)
提交回复
热议问题