I'm building a iOS Swift (Sprite kit) game. I'm using swipes to move the player. While testing I've found out that everything works just fine on most devices. However, a few devices do not recognise the swipe. Needless to say, the game doesn't work without the swipes. It's very odd that it works on almost all devices but a few.
My code is below. (Note: It's cleaned up to show only the code relevant for this case)
Edit
The app is currently being tested on actual devices. All devices run the latest iOS software. Mostly iPhone 5, a few 6 and 5S. Error occurs on iPhone 5S.
Am I missing something? If not, what else could I try?
override func didMoveToView(view: SKView) {
addSwipes()
}
func addSwipes() {
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)
}
func swipedRight(sender:UISwipeGestureRecognizer) {
// Do code here
}
func swipedLeft(sender:UISwipeGestureRecognizer) {
// Do code here
}
Related
I'm working on a small SpriteKit game for tvOS. I need to receive input when the user clicks the play/pause button on the remote. Looking at the docs, it appears that I should just have to add a UITapGestureRecognizer to my scene's view. I implemented the following code:
override func didMove(to view: SKView) {
let tapPlayPause = UITapGestureRecognizer(target: self, action: #selector(tapTesting))
tapPlayPause.allowedPressTypes = [NSNumber(value: UIPress.PressType.playPause.rawValue)]
view.addGestureRecognizer(tapPlayPause)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp))
swipeUp.direction = .up
view.addGestureRecognizer(swipeUp)
}
#objc func tapTesting(_ sender: UITapGestureRecognizer) {
print("TAP")
}
However, when I run the app on my Apple TV 4K (running tvOS 15), there is no output and from what I can tell no tap code is getting triggered.
I have tried the code with self.isUserInteractionEnabled = true as well but I still do not receive tap inputs.
The swipe gesture I implemented above works perfectly thought, and the tap gesture works on my iOS devices. Does anyone have any idea as to why this is the case?
Thanks!
For tvOS gestures, you also need to set allowedTouchTypes to .indirect
tapPlayPause.allowedTouchTypes = [NSNumber(touchType: .indirect)]
In my project I have tap gesture with following setup
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sceneViewTapped(gesture:)))
tapGesture.cancelsTouchesInView = true
tapGesture.delaysTouchesBegan = true
self.view.addGestureRecognizer(tapGesture)
And also has Touch method like override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
I have setup touch gesture with tapGesture.delaysTouchesBegan = true to work both gesture as well as Touch delegate methods
It is working fine till iOS12, But in iOS13 Touch delay has been increased so user must drag his finger and wait until drawing starts because touch method called delayed
Please refer below image , Sometime gesture lost , Delayed on starting of drawing.
If I remove delaysTouchesBegan it is smooth again.
Can anyone can help me to solve this ?
SAMPLE PROJECT
To quick test this stuff download raywenderlich project https://www.raywenderlich.com/5895-uikit-drawing-tutorial-how-to-make-a-simple-drawing-app
Add Following code in ViewController.swlft
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTapped(gesture:)))
tapGesture.delaysTouchesBegan = true
self.view.addGestureRecognizer(tapGesture)
}
#objc func viewTapped(gesture:UITapGestureRecognizer) {
print("View Tapped")
}
This was iOS 13.0 bug
After update to the iOS 13.1 Will fix this issue automatically
Hopefully helpful to someone :)
I am working on an application where I implement UIRefreshControl. All things going fine but when testing move on iPhone X or iPhone XR, pthen a weird problem raised.
The problem is UIRefreshControl Stucks sometimes when we try to refersh the list. Or we can say it occurring 1 out of 10 cases, If we uses it continuously. This issue is only generating in iPhone X or XR. In other device everything works fine.
If we talk about the code, I used the code which works perfectly. Here is a code snippet for UIRefreshControl.
Initialization:
lazy var refreshControl: UIRefreshControl? = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action:#selector(refreshAssignmentData(_:)), for: .valueChanged)
refreshControl.tintColor = CustomColors.themeColorGreen
return refreshControl
}()
I call this func in API, so when I get response it will add.
func addRefreshControl() {
/// Here adding refresh control
if #available(iOS 10.0, *) {
self.tblView?.refreshControl = self.refreshControl
} else {
self.tblView?.addSubview(self.refreshControl!)
}
refreshControl?.alpha = 1
}
And when we use pull to refresh this method calls
#objc func refreshAssignmentData(_ sender: Any) {
DispatchQueue.global(qos: .background).async {
self.performSelector(inBackground: #selector(self.getNewAssignmentListInBackground(_:)), with: nil)
}
DispatchQueue.main.async {
self.arrAllAssignment?.removeAll()
self.arrAllAssignment = [AssignmentModel]()
self.intPage = 1
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.refreshControl?.endRefreshing()
}
}
All these lines of code working fine in other device range except iPhone X series.
Any suggestion please...
I am adding a UITapGesture to some views. It is working fine only in iPhone 7 plus. The gesture method does not get called for other devices(iPhone 8, 6, 5s). Has anyone faced this kind of issue? I am just curious why is this happening? Has this something to do with Autolayout?
let tabGesture = UITapGestureRecognizer(target: self, action: #selector(handlePassTap(_:)))
tabGesture.numberOfTapsRequired = 1
self.passTabContainer.addGestureRecognizer(tabGesture)
let textGesture = UITapGestureRecognizer(target: self, action: #selector(handlePassTap(_:)))
textGesture.numberOfTapsRequired = 1
self.passTextContainer.addGestureRecognizer(textGesture)
#objc func handlePassTap(_ gesture:UITapGestureRecognizer)
{
performSegue(withIdentifier: "passSegue", sender: currentPass)
}
Yes. There was a view on top of those gesture views that was blocking the tap. Resolved it now.
In my tvOS app I have a collection view with cells that represent movies.
I attached 2 gesture recognizers to the view:
Go to movie details on selection tap
Play movie directly (with an Apple TV Remote dedicated Play button)
let posterTap = UITapGestureRecognizer(target: self, action: #selector(ListViewController.movieSelect(_:)))
posterTap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
self.view.addGestureRecognizer(posterTap)
let posterPlay = UITapGestureRecognizer(target: self, action: #selector(ListViewController.moviePlay(_:)))
posterPlay.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)]
self.view.addGestureRecognizer(posterPlay)
And the respected methods
func movieSelect(gesture: UITapGestureRecognizer) {
if let cell = UIScreen.mainScreen().focusedView as? ItemCollectionViewCell {
let item = ItemViewController(nibName: "ItemViewController", bundle: nil)
item.item = cell.data
self.presentViewController(item, animated: true, completion: nil)
}
}
func moviePlay(gesture: UITapGestureRecognizer) {
if let cell = UIScreen.mainScreen().focusedView as? ItemCollectionViewCell {
let data = cell.data
// TLDR;
// Passing data to AVPlayerViewController and presenting it + start playing the movie.
}
}
Everything seem to work, apart from the fact that when I stop playing the movie and coming back to the list (by closing the AVPlayerViewController), my second gesture recognizer (Play button) no longer works. It is still there if I check with print(self.view.gestureRecognizers) but moviePlay() is never called again no matter what.
Is there a way to debug this? What may cause this issue? I'm thinking this is caused by UIGestureRecognizerState being still in "use"? Or maybe something like that. At this point I have no clue.
I've experience weirdness with gesture recognizers on tvOS. In my case, for some reason, the app behaved as if gesture recognizers were lingering on after container view had been dismissed. Oddly enough, I've observed this weirdness when launching and closing AVPlayerViewController a few times as well.
What I did to fix this was to remove use of gesture recognizers and overriding pressesEnded method instead.
I hope this helps.