0

I programmatically create an object in my storyboard. I wanna have a dynamic app which can add and delete cards (objects of a PlayingCardView class) in real time. I need to have a possibility of touching these cards.

Does anyone know how to add a touch event to this object? In viewcontroller, you have the method that I used but it doesn't work.

import UIKit

class ViewController: UIViewController {


    var game = Concentration()

    var gesture: UITapGestureRecognizer { let t = UITapGestureRecognizer(target: self, action:  #selector(someAction(_:)))
        return t
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func CreateGame(_ sender: UIButton) {
        newGame()
    }

    func newGame() {
        game.height = heightView
        game.width = widthView
        game.addCards()

        view.addSubview(game.cards[1])
        game.cards[1].addGestureRecognizer(gesture)
        print("New Game started")
    }
    @objc func someAction(_ sender:UITapGestureRecognizer){
        if sender.state == .ended
        {
        print("Choose")
        }
    }

    func displayCards (){


    }


}

extension ViewController{

    private var widthView: CGFloat {
        return view.bounds.size.width}
    private var heightView: CGFloat {
        return view.bounds.size.height}

}

And there is a class that I use as a card.

import UIKit
@IBDesignable

class PlayingCardView: UIView {



    var identifier:Int = 0
    var sign:Int = 1 {didSet{updateView()}}
    var color: UIColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1) {didSet{updateView()}}
    var rank: Int = 3 {didSet{updateView()}}
    private static var identifierFactory = 0

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    private static func getUniqueIdentifier () -> Int {
        PlayingCardView.identifierFactory += 1
        return identifierFactory
    }

    func commonInit()
    {
        identifier = PlayingCardView.getUniqueIdentifier()
    }


    override func draw(_ rect: CGRect) {

        let roundedRect = UIBezierPath(roundedRect: bounds, cornerRadius: cornerSize)
        UIColor.white.setFill()
        roundedRect.fill()

        let shape = oval(howMany: rank)
        UIColor.red.setStroke()
        shape.lineWidth = 3.0
        shape.stroke()

    }

    private func oval (howMany: Int) ->UIBezierPath{

        let oval = UIBezierPath()

        switch howMany {
        case 1:
                oval.move(to: CGPoint(x: bounds.size.width/5, y: bounds.size.height*(1/2)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y+(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x-bounds.size.width*(3/10), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.close()

        case 2:
            oval.move(to: CGPoint(x: bounds.size.width/5, y: bounds.size.height*(3/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y+(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x-bounds.size.width*(3/10), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.close()
                oval.move(to: CGPoint(x: bounds.size.width/5, y: bounds.size.height*(7/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y+(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x-bounds.size.width*(3/10), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.close()
        case 3:

            oval.move(to: CGPoint(x: bounds.size.width/5, y: bounds.size.height*(1/5)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y+(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x-bounds.size.width*(3/10), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.close()

            oval.move(to: CGPoint(x: bounds.size.width/5, y: bounds.size.height*(1/2)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y+(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x-bounds.size.width*(3/10), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.close()

            oval.move(to: CGPoint(x: bounds.size.width/5, y: bounds.size.height*(4/5)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y+(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x+(bounds.size.width*(3/10)), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.addLine(to: CGPoint(x: oval.currentPoint.x-bounds.size.width*(3/10), y: oval.currentPoint.y-(bounds.size.height/10)))
            oval.close()

        default:
            break
        }
        return oval


    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    private func updateView(){
        setNeedsLayout()
        setNeedsDisplay()
    }

}


extension PlayingCardView {
    private struct Ratio {
        static let corner: CGFloat = 0.04
    }

    private var cornerSize: CGFloat { return Ratio.corner * bounds.size.height}
}

0 Answers0