Reputation: 608
I have a specific UIViewController for which I want to block the idleTimer processing done by iOS. I know that I can set: UIApplication.shared.isIdleTimerDisabled = true
However, I want to be able to set a timer to re-enable the normal system idle timer processing after a fixed amount of time, thus I can keep the screen on for a minimum amount of time. The nature of the app is such that you would have a view open and use it for reference/reading for a longish time.
The KEY POINT is that I want the timer to restart every time the user touches the screen or interacts with the device. So I need to be able to detect if the user does anything so I can rest the time.
I tried to override the touchesEnded method in the controller, however testing showed that the method is never called. Any ideas would be welcomed (in swift 3:)
Upvotes: 1
Views: 3718
Reputation: 608
In the end I solved this by doing a composite solution to what @RAJAMOHAN-S suggested.
I added a UIGestureRecognizer
to the controllers view. Using this I could pick up if there was a tap anywhere in the view and thus reset my timer.
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(viewTappedHandler)))
The viewTappedHandler
method set the timer I needed.
I made my UIController
a delegate for the UIScrollView
by adding the UIScrollViewDelegate
protocol to the controller's class declaration.
Then I implemented: scrollViewWillBeginDragging(_ scrollView: UIScrollView)
and again set the time. This picks up movements in the scroll view that the tap gesture doesn't pick up.
Finally, because my scroll view contained other views that can open the keyboard, I also needed to register for keyboard notifications and again set the timer for each occurrence.
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: .UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name: .UIKeyboardDidHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidChange), name: .UIKeyboardDidChangeFrame, object: nil)
I'm not completely happy with this as tapping on the keyboard keys is not detected, but I'm not expecting this to be a real issue for this app.
PS I also removed the observers on the keyboard and clear the timer on deinit
.
Upvotes: 0
Reputation: 7269
Swift 3.0 :-
Need to detect touch of the particular view without IBOutlet
, then select that particular UIView
then goto Attributes Inspector -> View -> tag
and set Integer
as tag whatever need.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let tag = touch?.view?.tag
if tag == 1{
//Do More....
}else{
//......
}
}
If you need to check with IBOutlet
, then do like as below.
Here, @IBOutlet var diamondView: UIView!
if touch?.view == self.diamondView{
//Do More....
}else{
//......
}
Update : Scroll View
If you need to detect touch of the scroll view then use UITapGestureRecognizer
. By story board drag and drop to to of the UIViewController
and set delegate
and gestureRecognizers
for scroll view
to UITapGestureRecognizer
. After that just create action for UITapGestureRecognizer
like as below screenshot.
Adding UITapGesture Programmatically :-
Also below code works fine without do like above screenshot.
@IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.actionTapOnScrollView(sender:)))
self.scrollView.addGestureRecognizer(tapGesture)
}
@objc private func actionTapOnScrollView(sender:UITapGestureRecognizer){
print("user Touched")
}
Upvotes: 1