Я программно создаю объект в своей раскадровке. Я хочу иметь приложение Dynami c, которое может добавлять и удалять карты (объекты класса PlayingCardView) в режиме реального времени. Мне нужно иметь возможность прикоснуться к этим картам.
Кто-нибудь знает, как добавить событие касания к этому объекту? В viewcontroller у вас есть метод, который я использовал, но он не работает.
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}
}
И есть класс, который я использую в качестве карты.
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}
}