Я работаю сканер кредитных карт, используя пользовательский вид. Я попробовал приведенный ниже код и не смог получить доступ к пользовательскому классу. Не могли бы вы, пожалуйста, кто-нибудь помочь мне решить эту проблему. Прикрепленный скриншот ошибки ниже. Спасибо ...
let BUTTON_SIZE = CGSize(width: 100, height: 20)
let BUTTON_BUFFER = 10
protocol GymCreditCardScannerViewDelegate: class {
func creditCardScannerDidCancel(_ scanner: Any?)
func creditCardScanner(_ scanner: Any?, scannedNumber number: String?)
}
import UIKit
class GymCreditCardScanner: UIView, CardIOViewDelegate {
weak var scannerdelegate: GymCreditCardScannerViewDelegate?
var cardIOView: CardIOView?
var cancelButton: UIButton?
var doneButton: UIButton?
var number = ""
var screenFrame = CGRect.zero
var innerFrame = CGRect.zero
func initIntoView(_ superView: UIView?, withDelegate delegate: Any?) -> Any? {
let frame = CGRect(x: 0, y: 0, width: superView?.frame.size.width ?? 0.0, height: superView?.frame.size.height ?? 0.0)
self.scannerdelegate = delegate as? GymCreditCardScannerViewDelegate
screenFrame = frame
let MARGIN: CGFloat = 0.05
innerFrame = CGRect(x: frame.size.width * MARGIN, y: frame.size.height * MARGIN, width: frame.size.width * (1 - 2 * MARGIN), height: frame.size.height * (1 - 2 * MARGIN))
cardIOView = CardIOView(frame: innerFrame)
cardIOView?.delegate = self as CardIOViewDelegate
cardIOView?.hideCardIOLogo = true
cardIOView?.scannedImageDuration = 0.1
addSubview(cardIOView!)
let buttonSize: CGSize = BUTTON_SIZE
cancelButton = UIButton(frame: CGRect(x: frame.size.width / 2 - buttonSize.width / 2, y: innerFrame.origin.y + innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height))
cancelButton?.setTitle("Cancel", for: .normal)
cancelButton?.addTarget(self, action: Selector(("handleCancelPress:")), for: .touchUpInside)
addSubview(cancelButton!)
superView?.addSubview(self)
return self
}
func remove() {
if (cardIOView != nil) {
cardIOView?.removeFromSuperview()
}
cardIOView = nil
removeFromSuperview()
}
func cardIOView(_ cardIOView: CardIOView!, didScanCard cardInfo: CardIOCreditCardInfo!) {
number = cardInfo.cardNumber
UIGraphicsBeginImageContextWithOptions(cardIOView.frame.size, true, 1)
if let aContext = UIGraphicsGetCurrentContext() {
cardIOView.layer.render(in: aContext)
}
let capture: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let captureView = UIImageView(image: capture)
captureView.frame = cardIOView.frame
addSubview(captureView)
bringSubview(toFront: cancelButton!)
cardIOView.removeFromSuperview()
let frame: CGRect = screenFrame
let buttonSize: CGSize = BUTTON_SIZE
doneButton = UIButton(frame: CGRect(x: frame.size.width / 2 - buttonSize.width / 2, y: innerFrame.origin.y + innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height))
doneButton?.setTitle("Done", for: .normal)
doneButton?.addTarget(self, action: Selector(("handleDonePress:")), for: .touchUpInside)
addSubview(doneButton!)
doneButton?.alpha = 0
UIView.animate(withDuration: 0.075, delay: 0, options: .curveEaseOut, animations: {
self.doneButton?.alpha = 1
self.doneButton?.frame = CGRect(x: frame.size.width / 2 - buttonSize.width / 2 + (buttonSize.width + CGFloat(BUTTON_BUFFER)) / 2, y: self.innerFrame.origin.y + self.innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height)
self.cancelButton?.frame = CGRect(x: frame.size.width / 2 - buttonSize.width / 2 - (buttonSize.width + CGFloat(BUTTON_BUFFER)) / 2, y: self.innerFrame.origin.y + self.innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height)
}, completion: nil)
}
func handleCancelPress(_ sender: Any?) {
scannerdelegate?.creditCardScannerDidCancel(self)
}
func handleDonePress(_ sender: Any?) {
scannerdelegate?.creditCardScanner(self, scannedNumber: number)
}
}
My ViewContoller
class PaymentDetailsVC: UIViewController {
var scanner: GymCreditCardScanner?
//MARK: Credit card scanning
@IBAction func scanCreditCard(_ sender: Any) {
cardField.resignFirstResponder()
scanner = GymCreditCardScanner.initIntoView(view, withDelegate: self)
}
func creditCardScannerDidCancel(_ scanner: Any?) {
scanner.remove()
}
func creditCardScanner(_ scanner: Any?, scannedNumber number: String?) {
scanner.remove()
scanner = nil
numberField.text = number
}
}