Reputation:
I have UIView A and UIView B. A is a subview of a scrollView along with siblings subviews C-Z. B has a UILongPressGestureRecognizer and can be dragged around. If B is dragged inside A or C-Z I have function that does something. However, I want that function to go off only when B has been inside A for 1 second. If B is not inside A after that second I don't want anything to happen. How can I achieve this? I've already tried using an NSTimer.
timer = NSTimer()
timer.scheduledTimerWithInterval(1.0,target:self, #selector(fireMethod),userInfo:nil, repeats:Yes)
It seemed to work once, but after that first time, it never worked again.
Upvotes: 0
Views: 63
Reputation: 6462
Try this Swift 3.0
func delayWithSeconds(_ seconds: Double, completion: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
completion()
}
}
Usage
delayWithSeconds(1) {
//Do something
}
Upvotes: 1
Reputation: 11
Try this code.
[self performSelector:@selector(yourAction:) withObject:nil afterDelay:2.3];//give delay asper your requirement.
-(void)yourAction
{
//your reuire code after delay.
}
Upvotes: 0
Reputation: 306
Create a delay function like so
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
Then in your UILongPressGestureRecognizer
call the function as so
self.delay(1)
{
//code here
}
Upvotes: 0