remove UIGestures when leaving scene - swift

I am having problems with my UIGestures in my game
At the gamescene, they work perfect, but when I move to another scene the UIgestures keep active and if I use them(swipe) than it crashes.
I know I have to use willmovefromview method in Gamescene file but have been working 2 days on this and still not working..
here is my viewcontroller file: (see comments for the gesture functions and gesture recognizers)
import UIKit
import SpriteKit
import AVFoundation
class GameViewController: UIViewController, UITextFieldDelegate{
//gesture functions
func swipedRight(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedRight1(sender)
}
func swipedLeft(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedLeft1(sender)
}
func swipedDown(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedDown1(sender)
}
func swipedUp(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedUp1(sender)
}
override func viewDidLoad() {
super.viewDidLoad()
let skView = view as SKView
skView.multipleTouchEnabled = false
scene = GameMenuScene(size: skView.bounds.size)
//scene.scaleMode = SKSceneScaleMode.ResizeFill
skView.showsFPS = true
skView.showsNodeCount = true
skView.presentScene(scene)
//Gesture recognizers
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
}
override func viewDidLayoutSubviews() {
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
PS: My app is starting on a menuscene, and the swipes must only be active at the gamescene, so I think I have to use didmovetoview method as well.
and here are the didmovetoview method and willmovefromview method on my gamescene file:
override func didMoveToView(view: SKView) {
}
override func willMoveFromView(view: SKView) {
}
Who is going to help me?
Thanks in advance

override func willMoveFromView(view: SKView) {
for recognizer in self.view.gestureRecognizers! {
self.view.removeGestureRecognizer(recognizer)
}
}

self.view.gestureRecognizers=nil;
It works for me.

Related

UITapGestureRecognizer not attaching action

I created a separate class for View.
I left all the functions in the Controller.
But when I add a click on the picture, it doesn't work for some reason.
import UIKit
class APOTDView: UIView {
var imageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(APOTDViewController.imageTapped(_:)))
imageView.addGestureRecognizer(tap)
return imageView
}()
}
import UIKit
class APOTDViewController: UIViewController {
let av = APOTDView()
override func viewDidLoad() {
super.viewDidLoad()
// ... add subview and constraint
}
#objc func imageTapped(_ sender: UITapGestureRecognizer) {
print("good job")
}
}
What's the matter? Please help me figure it out
Your selector in the UITapGestureRecognizer is wrong. You can not call the APOTDViewController directly.
APOTDViewController.imageTapped would be a static function, which is not available.
You can use a delegate instead.
Delegate Protocol and View.
protocol APOTDViewDelegate: AnyObject {
func viewDidTapImage()
}
class APOTDView: UIView {
weak var delegate: APOTDViewDelegate?
var imageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
imageView.addGestureRecognizer(tap)
return imageView
}()
#objc func imageTapped() {
delegate?.viewDidTapImage()
}
}
ViewController:
class APOTDViewController: UIViewController, APOTDViewDelegate {
let av = APOTDView()
override func viewDidLoad() {
super.viewDidLoad()
av.delegate = self
// ... add subview and constraint
}
#objc func viewDidTapImage() {
print("good job")
}
}
This will not work, because you are calling the UIViewController method directly without any class reference or object. The solution is to use protocol or clouser to get action from view to class.
class
class APOTDView: UIView {
#objc var imageViewAction: ((UITapGestureRecognizer) -> Void)? = nil
lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.backgroundColor = .blue
imageView.translatesAutoresizingMaskIntoConstraints = true
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector((imageTapped(_:))))
imageView.addGestureRecognizer(tap)
return imageView
}()
#objc private func imageTapped(_ sender: UITapGestureRecognizer) {
self.imageViewAction?(sender)
}
}
ViewController
class APOTDViewController: UIViewController {
let av = APOTDView()
override func viewDidLoad() {
super.viewDidLoad()
av.imageViewAction = { sender in
print("good job")
}
}
}

can not call selector and gesture delegate iOS 13.2

my code works in iOS 12 last version but after iOS 13 this code can't call the selector
I used UIGestureRecognizerDelegate for iOS 13
here is my code
let panGest = UIPanGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
panGest.delegate = self
txtVw.addGestureRecognizer(panGest)
here is my let txtVw = subVws as! UITextView , txtVw = subVws as! UITextView
I used this code:
let panGest = UIPanGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
self.view.addGestureRecognizer(panGest)
its work but I have one subview and for subview isn't work & call !!
isn't call self.handleTap or gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
Is this what you want?
class ViewController: UIViewController, UIGestureRecognizerDelegate {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
gestureRecognizer.delegate = self
textView.addGestureRecognizer(gestureRecognizer)
textView.isUserInteractionEnabled = true
textView.isMultipleTouchEnabled = true
}
#objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
//make sure the view is not nil
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
}

UIImageView transform loses swipe gesture

I have an image that I want to rotate every time the user swipes on it. It works the first time, and first time only! How is it that the view is losing the swipe gesture recognizer?
myCircle = UIImageView( ... )
myCircle.isUserInteractionEnabled = true
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiping(sender:)))
swipeLeft.direction = .left
myCircle.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiping(sender:)))
swipeRight.direction = .right
myCircle.addGestureRecognizer(swipeRight)
}
#objc func swiping(sender: UISwipeGestureRecognizer) {
// this function only called the first time!
if sender.direction == UISwipeGestureRecognizer.Direction.left {
myCircleRotation -= CGFloat(Double.pi/2)
} else if sender.direction == UISwipeGestureRecognizer.Direction.right {
myCircleRotation += CGFloat(Double.pi/2)
}
// if I comment out this next line, function is called (correctly) every swipe!
myCircle.transform = CGAffineTransform(rotationAngle: myCircleRotation)
}
You can add a UIView as a container view to your hierarchy, and then add the UIGestureRecognizers to the container view instead of to the UIImageView. This would allow your image to be rotated without impact to the gestures.
import UIKit
class ImageViewController: UIViewController {
#IBOutlet weak var myCircleView: UIImageView!
#IBOutlet weak var containerView: UIView!
var myCircleRotation:CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
setupImage()
}
private func setupImage() {
let image = UIImage(named: "your-image")
myCircleView.image = image
myCircleView.isUserInteractionEnabled = true
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiping(sender:)))
swipeLeft.direction = .left
containerView.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiping(sender:)))
swipeRight.direction = .right
containerView.addGestureRecognizer(swipeRight)
}
#objc func swiping(sender: UISwipeGestureRecognizer) {
// this function only called the first time!
if sender.direction == UISwipeGestureRecognizer.Direction.left {
myCircleRotation -= CGFloat(Double.pi/2)
} else if sender.direction == UISwipeGestureRecognizer.Direction.right {
myCircleRotation += CGFloat(Double.pi/2)
}
UIView.animate(withDuration: 0.5) {
self.myCircleView.transform = CGAffineTransform(rotationAngle: self.myCircleRotation)
}
}
}

SpriteKit - SpriteNode never shows

I am attempting to learn SpriteKit. I want to add a rectangle/square to the scene in code; but the item never shows;
All I want to do is add a white square to the screen, but find that it never adds.
In a breakpoint, I notice that didMove() never seems to get called.
What am I doing wrong?
class GameScene: SKScene {
override func didMove(to view: SKView) {
let item = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 200))
item.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(item)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
update
I did not change the view controller generated by xcode
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override var prefersStatusBarHidden: Bool {
return true
}
}
There is no GameScene.sks, I deleted this file as I do not want to use sks files
If a breakpoint in didMove(to view:) is never reached, then definitely your ViewController doesn't present the scene for some reason. Can you open your ViewController's file (e.g. GameViewController.swift) and see if you have any of those lines there (or anything to that effect):
let viewSize = UIScreen.main.bounds.size
let scene = GameScene(size: viewSize)
let skView = self.view as! SKView
skView.presentScene(scene)
Turns out view controller was not presenting the scene and changing it to this, following code posted by #Stoyan worked and I saw my sprite showing
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
let viewSize = UIScreen.main.bounds.size
let scene = GameScene(size: viewSize)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
view.presentScene(scene)
}

Trouble with SKScenes and Segueing to ViewControllers SpriteKit

In my GameViewController i have two scenes, one being the GameScene and another being a scene (EndScene) that is switched to when the player dies. In the first block of code below is my EndScene Scene that is supposed to switch to my main menu viewcontroller, however it wont for some reason. I'm not getting any errors or likewise, it just stays on EndScene. In the second block of code is my GameViewController.swift file. Can someone help point out what im doing wrong here and what i can do to fix it?
EndScene.swift
import SpriteKit
class EndScene : SKScene {
var viewController : UIViewController?
override func didMoveToView(view: SKView) {
self.segue()
}
func segue() {
self.viewController?.performSegueWithIdentifier("gameToMain", sender: viewController)
}
}
GameViewController.swift
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
scene.viewController = self
skView.presentScene(scene)
}
if let scene = EndScene(fileNamed: "EndScene"){
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve
rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
scene.viewController = self
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
try
let newScene: GameScene = GameScene(size: size)
let transition = SKTransition.fadeWithColor(SKColor.blackColor(), duration: 0.5)
view?.presentScene(newScene, transition: transition)
There is other transitions you can use, but try this code and see if it will switch your scenes