How to identify iPhone SHAKE in Swift? [duplicate] - ios

This question already has answers here:
Detect shake gesture IOS Swift
(7 answers)
Closed 5 years ago.
Hi I want to identify the iPhone SHAKE when user shakes their phone, either in background mode or in foreground mode of the app.
Please assist me.
Thanks in advance.

Try something like this:
override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
print("Device was shaken!")
}

The main trick is that you need to have some UIView (not UIViewController) that you want as firstResponder to receive the shake event messages. Here's the code that you can use in any UIView to get shake events:
class ShakingView: UIView {
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if event?.subtype == .motionShake {
// Put in code here to handle shake
}
if super.responds(to: #selector(UIResponder.motionEnded(_:with:))) {
super.motionEnded(motion, with: event)
}
}
override var canBecomeFirstResponder: Bool { return true }
You can easily transform any UIView (even system views) into a view that can get the shake event simply by subclassing the view with only these methods (and then selecting this new type instead of the base type in IB, or using it when allocating a view).
In the view controller, you want to set this view to become first responder:
override func viewWillAppear(_ animated: Bool) {
shakeView.becomeFirstResponder()
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
shakeView.resignFirstResponder()
super.viewWillDisappear(animated)
}
Don't forget that if you have other views that become first responder from user actions (like a search bar or text entry field) you'll also need to restore the shaking view first responder status when the other view resigns!
This method works even if you set applicationSupportsShakeToEdit to NO.
For objective c version refer link How do I detect when someone shakes an iPhone?

Related

How to detect shake in UITableViewCell

I want to detect a shake gesture in a UITableViewCell, but no shake is being registered. Is there a way I can detect a shake in a UITableView cell?
This is the code I'm using now:
extension MyTableViewCell {
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
print("shake started")
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
print("shake stopped")
}
}
I would suggest adding your motionBegan/motionEnded methods to your view controller, and then updating the model that backs your table view to reflect the shake event and tell the table view to reload. (For example, you might add an isShaking property to an array of structs that holds the settings for your table view cells.) In your cellForRowAt() method, check the model to see if your isShaking property for that IndexPath is true, and if it is, make whatever change to your cell animation that you need.

Alert with Shake to Undo

I want to implement a shake to undo on my iPhone App, in Swift.
For now it works, but it didn't display an Alert asking to validate the undo gesture (with buttons "Cancel" and "Undo"). I can add this alert in the right place myself, but I'm not certain I should. Some articles make me think that the alert should appear automatically, in the undo/redo process, so there's something I missed, perhaps...
Here are the relevant bits of code
In the viewController
override func becomeFirstResponder() -> Bool {
return true
}
and
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
snowflake.undoLastAction()
}
}
and in my snowflake class, the action is inserting or modifying point in an array, so I store the value before the change in oldPathPoints, then
// Action du undo
undoManager?.registerUndo(withTarget: self, handler: { (targetSelf) in
snowflake.pathPoints = oldPathPoints
})
and finally the undo method
func undoLastAction() {
undoManager?.undo()
createPath()
}
So,
I succeeded to do it (with the help of a 5-years Book from Matt Neuburg!)
So, there's nothing to make in the viewController, I deleted becomeFirstResponder and motionEnded.
All is in the SnowFlake class: I added
let undoer = UndoManager()
override var undoManager : UndoManager? {
return self.undoer
}
override var canBecomeFirstResponder: Bool { return true }
So I communicate the undoManager I use and the class (a view here) can become firstResponder
after the manipulation of the view that can be undone (in my case in touchesEnded)
, I call
self.becomeFirstResponder()
I deleted the undoLastAction function (useless now), and
changed the undo action to a fonction
changePathUndoable(newPathPoints: finalPathPoints)
the function is like that
func changePathUndoable(newPathPoints: [CGPoint]) {
let oldPathPoints = Snowflake.sharedInstance.pathPoints
Snowflake.sharedInstance.pathPoints = newPathPoints
// Action du undo
undoer.setActionName("cutting")
undoer.registerUndo(withTarget: self, handler: { (targetSelf) in
targetSelf.changePathUndoable(newPathPoints: oldPathPoints)
})
Snowflake.sharedInstance.createPath()
self.setNeedsDisplay()
}
I had to create the function changePathUndoable instead of calling undoer.registerUndo directly just for having the redo action to work (I didn't test redo before).
I think that all the steps are necessary to do what I wanted: make the view receive the shake event, displays this dialog
and be able to undo/redo the modifications.

Shake gesture not working iOS

I have used collectionView and also implemented pull to sync (pull_Down_To_Get_Data_From_Server) in my ViewController(named : "DashboardViewController"). I tried the shake gesture
code(given below) in my DashboardViewController and its not working but similar code is working in another viewController of same app.
I used the canBecomeFirstResponder in viewDidLoad in first try and in viewDidAppear secondly but it was also useless.
Requirement:
Actualy i want to know the cases(if any) in which ".motionShake" event do not trigger directly and we have to implement another way? Also that i did not find anything fruitful
on google.
Image of storyboard may help in understanding all the utilities used
override func viewDidLoad() {
// call super
super.viewDidLoad()
self.becomeFirstResponder() // for shake gesture detection
}
// it will make first responder to detect shake motion event
override var canBecomeFirstResponder: Bool {
get {
return true
}
}
// get detection of shake motion event
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake
{
print("Shake detected")
}
}
We ran into something similar while working on Yoshi
We ended up creating an extension on UIWindow, that contained the motionBegan event and forwarded the event. Not sure what caused it, but it seemingly broke with swift 3.0.

Avoid keyboard closing when iPhone rotated XCode Swift

I have in left navigation item UITextField. When I type something in there and rotate device, keyboard hides everytime.
I tried to handle UIKeyboardWillHideNotification, but all what I got is: keyboard closes and shows again after that. It's not good, I need to rotate keyboard along with view...
Please help in Swift 2.
Okay, I found the solution!
First, need to implement delegate:
ViewController: UIViewController, UITextFieldDelegate
Next, add to viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
To the end, realise textFieldShouldEndEditing function:
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
//here we can add some if-block for orientation change or smth else
return false
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
textField.becomeFirstResponder()
}
The method above here gets called when the device will rotate, as you don't care about the orientation we don't check it and simple instruct the textfield you are editing to become first responder.

Touch events are delayed near left screen edge on iOS 9 only. How to fix it?

I am developing a keybaord extension for iOS. On iOS 9 the keys react imediatelly except for keys along left edge of the keyboard. Those react with around 0.2 second delay. The reason is that the touches are simply delivered with this delay to the UIView that is root view of my keyboard. On iOS 8 there is no such delay.
My guess is that this delay is cause by some logic that is supposed to recognize gesture for opening "running apps screen". That is fine but the delay on a keyboard is unacceptable. Is there any way how to get those events without delay? Perhaps just setting delaysTouchesBegan to false on some UIGestureRecognizer?
This is for anyone using later versions of iOS (this is working on iOS 9 and 10 for me). My issue was caused by the swipe to go back gesture interfering with my touchesBegan method by preventing it from firing on the very left edge of the screen until either the touch was ended, or the system recognised the movement to not be that of the swipe to go back gesture.
In your viewDidLoad function in your controller, simply put:
self.navigationController?.interactivePopGestureRecognizer?.delaysTouchesBegan = false
The official solution since iOS11 is overriding preferredScreenEdgesDeferringSystemGestures of your UIInputViewController.
https://developer.apple.com/documentation/uikit/uiviewcontroller/2887512-preferredscreenedgesdeferringsys
However, it doesn't seem to work on iOS 13 at least. As far as I understand, that happens due to preferredScreenEdgesDeferringSystemGestures not working properly when overridden inside UIInputViewController, at least on iOS 13.
When you override this property in a regular view controller, it works as expected:
override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge {
return [.left, .bottom, .right]
}
That' not the case for UIInputViewController, though.
UPD: It appears, gesture recognizers will still get .began state update, without the delay. So, instead of following the rather messy solution below, you can add a custom gesture recognizer to handle touch events.
You can quickly test this adding UILongPressGestureRecognizer with minimumPressDuration = 0 to your control view.
Another solution:
My original workaround was calling touch down effects inside hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?, which is called even when the touches are delayed for the view.
You have to ignore the "real" touch down event, when it fires about 0.4s later or simultaneously with touch up inside event. Also, it's probably better to apply this hack only in case the tested point is inside ~20pt lateral margins.
So for example, for a view with equal to screen width, the implementation may look like:
let edgeProtectedZoneWidth: CGFloat = 20
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let result = super.hitTest(point, with: event)
guard result == self else {
return result
}
if point.x < edgeProtectedZoneWidth || point.x > bounds.width-edgeProtectedZoneWidth
{
if !alreadyTriggeredFocus {
isHighlighted = true
}
triggerFocus()
}
return result
}
private var alreadyTriggeredFocus: Bool = false
#objc override func triggerFocus() {
guard !alreadyTriggeredFocus else { return }
super.triggerFocus()
alreadyTriggeredFocus = true
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
alreadyTriggeredFocus = false
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
alreadyTriggeredFocus = false
}
...where triggerFocus() is the method you call on touch down event. Alternatively, you may override touchesBegan(_:with:).
If you have access to the view's window property, you can access these system gesture recognizers and set delaysTouchesBegan to false.
Here's a sample code in swift that does that
if let window = view.window,
let recognizers = window.gestureRecognizers {
recognizers.forEach { r in
// add condition here to only affect recognizers that you need to
r.delaysTouchesBegan = false
}
}
Also relevant: UISystemGateGestureRecognizer and delayed taps near bottom of screen

Resources