本文介绍了在使用scratch和win示例的帮助下显示刮刮卡效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ios developpement的新手。如果优惠券号码可见,我需要在划痕后为iPhone应用程序显示刮刮卡效果我需要显示提醒信息我该怎么做?.i从





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
}

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

guard让touch = touches.first else {

返回
}

lastPoint = touch.location(in:self)
}

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

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

返回
}

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

已更新



使用此解决方案触控仅在 UIImageView 边界内跟踪事件。如果您需要在刮刮卡之外启动触摸事件,请参阅示例


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

解决方案

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

这篇关于在使用scratch和win示例的帮助下显示刮刮卡效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:43