Iturfly
Iturfly

Reputation: 21

UIGestureRecognizer in Swift Playgrounds

I am trying to integrate UIGestureRecognizer into my swift playground so that wherever I tap in my playground, my sprite goes to that point. I've tried everything! Watching countless youtube videos, and going on stack overflow for answers but everyone starts with making a class called view that is a UIView and in their code they keep referring to "self". I instead made a variable named "view" that was a SKView. I try to get parts of their code, put it in mine, and change it, but it just doesn't work. Here's the work that I got so far.

view.addGestureRecognizer(UIGestureRecognizer(target: view, action:  #selector(handleTap(sender:))))

func handleTap(sender: UITapGestureRecognizer) {
        player.position = sender.location(in: view)
}

My playground keeps telling me that i'm using an unresolved identifier 'handleTap(sender:)'

Upvotes: 0

Views: 1422

Answers (3)

Rizwan Mehboob
Rizwan Mehboob

Reputation: 1373

UIGestureRecognizer example with different states of Gesture recogniser in Playground

swift3+

import UIKit
import PlaygroundSupport

class ViewController : UIViewController {

    var yellowView: UIView!
    var redView: UIView!
    var yellowViewOrigin: CGPoint!

    override func loadView() {

        // UI

        let view = UIView()
        view.backgroundColor = .white

        yellowView = UIView()
        yellowView.backgroundColor = .yellow
        view.addSubview(yellowView)

        redView = UIView()
        redView.backgroundColor = .red
        view.addSubview(redView)

        // Layout
        redView.translatesAutoresizingMaskIntoConstraints = false
        yellowView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            yellowView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20.0),
            yellowView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0),
            yellowView.heightAnchor.constraint(equalToConstant: 80.0),
            yellowView.widthAnchor.constraint(equalToConstant: 80.0),

            redView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20.0),
            redView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0),
            redView.heightAnchor.constraint(equalToConstant: 80.0),
            redView.widthAnchor.constraint(equalToConstant: 80.0)

            ])

        self.view = view
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(_:)))
        yellowView.addGestureRecognizer(pan)
        yellowViewOrigin = yellowView.frame.origin
        view.bringSubview(toFront: yellowView)
    }

    @objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
        let targetView = sender.view!
        let translation = sender.translation(in: view)

        switch sender.state {
        case .began,.changed:
            targetView.center = CGPoint(x: targetView.center.x + translation.x
                ,y: targetView.center.y + translation.y)
            sender.setTranslation(CGPoint.zero, in: view)
        case .ended:
            if targetView.frame.intersects(redView.frame){
                UIView.animate(withDuration: 0.3) {
                    targetView.alpha = 0.0
                }

            }
            else{
            UIView.animate(withDuration: 0.3) {
                targetView.frame.origin = self.yellowViewOrigin
            }
            }
            break
        default:
            break
        }

    }
}

PlaygroundPage.current.liveView = ViewController()

Preview

Upvotes: 4

Federico Zanetello
Federico Zanetello

Reputation: 3451

func handleTap has to be called from your view type.

Please try to add it inside an SKView extension:

    extension SKView {
        func handleTap(sender: UITapGestureRecognizer) {
                player.position = sender.location(in: view)
        }
    }

Upvotes: 0

jraufeisen
jraufeisen

Reputation: 3877

I think you need to add the @objc mark in front of the function declaration, so that handleTap will be visible to #selector.

@objc func handleTap(sender: UITapGestureRecognizer) {
    player.position = sender.location(in: view) 
}

Upvotes: 0

Related Questions