I have a UIButton within an app that will allow the user to send a request to an API.
How can I prevent the user from pressing this button more than X times per second?
I assume you want to just ignore taps on the button if they are too frequent, but you don't need to set the button to appear disabled while taps are ignored. (If you want to change the button appearance, you'll need to use an NSTimer or other delayed action to re-enable the button, which is a significant complication.)
If you change the requirement to “at least 1/X seconds between taps”, it's a little simpler. For example, instead of allowing at most 4 taps per second, we'll ignore a tap if it comes less than 1/4 seconds after the prior tap.
To implement this requirement, store the time of the last tap. When a tap arrives, see if 1/X seconds have elapsed since the last tap. If not, ignore the tap.
private let minimumTapInterval = 1 / CFTimeInterval(4)
private var lastTapTime = CFAbsoluteTime(0)
#IBAction func buttonWasTapped(sender: AnyObject?) {
let now = CFAbsoluteTimeGetCurrent()
guard now >= lastTapTime + minimumTapInterval else { return }
lastTapTime = now
sendAPIRequest()
}
If you really want to implement a “no more than X taps per second” requirement, you can store the times of the accepted taps. When a new tap comes in, throw away any stored times older than one second. If there are still at least X stored times, ignore the new tap.
private let maxTapsPerSecond = 4
private var tapTimes = [CFAbsoluteTime]()
#IBAction func buttonWasTapped(sender: AnyObject?) {
let now = CFAbsoluteTimeGetCurrent()
let oneSecondAgo = now - 1
tapTimes = tapTimes.filter { $0 >= oneSecondAgo }
// All elements of tapTimes are now within the last second.
guard tapTimes.count < maxTapsPerSecond else { return }
tapTimes.append(now)
sendAPIRequest
}
You can use the isEnabled property of your UIButton.
For the target of your button, add the following at the end of your #objc selector function:
#objc func handleMyButtonPress() {
self.someOtherFunction()
myButton.isEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
myButton.isEnabled = true
}
}
The .now() + 1 deadline is basically saying, after 1 second the button will become enabled again and your users can interact with it.
For a 5 minute delay, you could use .now() + (60 * 5)
I use this myself and it works reliably in almost any situation.
Hopefully, this answer can help you or others with the same problem.
Related
I have the following function on iOS that performs UI work on a TableView, and the challenge is that if it begins to take longer then 0.5sec. a spinner should be displayed to the user so that the screen doesn't look like it froze.
func updateForm(with rowItems: [RowItem]) {
self.tableView.beginUpdates() // performance tweak.
let viewControllerName = String.init(describing: self.classForCoder) // id
var defaultSection = Form.createSectionWith(tag: viewControllerName, in: form)
// MARK: - update rows
let allRows = self.form.allRows
let startTime = CFAbsoluteTimeGetCurrent()
var showSpinner = false
for (index, item) in rowItems.enumerated() {
.
.
.
<TableView processing work on 100's of rows>
.
.
.
// Evaluate our running time for this process loop, and display spinner if we're past a threshold of seconds.
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
if timeElapsed > 0.5 && showSpinner == false {
showSpinner = true
self.showActivityIndicator(withStatus: "processing") // {NEVER GETS DISPLAYED}
}
} // for (index, item) ...
Of course, when I make the call to showActivityIndicator, it never actually gets displayed.
How can I interrupt and pause the UI work to have the showActivityIndicator spinner animation show-up, then let the loop continue?
You need to get off the main thread in order to allow the run loop to cycle and the redraw moment to occur. A simple delay after showing the spinner before proceeding with your work will solve the problem. But you will have to reorganize your code to allow for that. The easiest solution is to give up your 0.5 second idea and just show the spinner before starting in the first place.
I think there are some more important architectural questions that you need to be addressing with that code, but as a solution to the question asked...
before you start processing the data, create a timer with a delay that will load the spinner when it fires
let delayInSeconds = 5.0
let timer = Timer.scheduledTimer(withTimeInterval: delayInSeconds, repeats: false){
//closure that loads the spinner view on top of the current view
}
then when the processing activity is complete
timer?.invalidate() //cancels the timer if it hasn't fired
and then unload the spinner view
Recently I've been writing a game that requires the Pitch of the device in order to move the character. However, to make it so the user doesn't have to play the game with the device fixed in one starting point every time, every time the user presses play I want the app to acquire the initial tilt. This does not work however, I've added a test button that is suppose to run the code:
func recordTilt() {
InitialTilt = MovementManager.deviceMotion?.attitude
print(InitialTilt)
}
The problem with this is that whenever the button is pressed, InitialTilt will return nil. However, if InitialTilt is run in a the Loop it will return a value every time.
Movement Manager Loop:
func setup() {
MovementManager = CMMotionManager()
MovementManager.deviceMotionUpdateInterval = 0.1
MovementManager.startDeviceMotionUpdates()
InintialTilt = Movementmanager.deviceMotion?.attitude
}
func movementManaging() {
//...
InintialTilt = Movementmanager.deviceMotion?.attitude // returns every
time
//...
}
func update() {
movementManaging()
}
Can someone please help explain to me why the (InintialTilt = Movementmanager.deviceMotion?.attitude) only returns a value while it is in a loop.
(Note: The movementManaging is basically a loop that controls the player's movements, if one were to expand the ...'s all they would get are a bunch of if methods that keep the player on screen. + InintialTilt = Movementmanager.deviceMotion?.attitude)
So I've just started trying out coding in swift and I'm creating an extremely basic app (purely to experiment and learn) where you click a button and two playing cards appear on the screen.
I'm trying to get it so that when the two playing cards are the same, the button to play again disables, the program pauses for a few seconds, then the button re-enables (so later I can add some 'win' text during the pause time).
Now the button and pausing fully works besides one problem. When testing, the program pauses and then when it finishes pausing the display then updates to show the two cards being equal. But while it pauses, it shows two random non equal cards.
I'm not sure why, seeing as the cards update before I check if they're equal, but I'm new to swift (literally last few days) so not sure how it works.
Any ideas? :)
#IBAction func playRoundTapped(sender: UIButton) {
// Change the card image each time the play button is pressed using a random number generator
self.firstCardImageView.image = UIImage(named: String(format: "card%i", arc4random_uniform(13)+1))
self.secondCardImageView.image = UIImage(named: String(format: "card%i", arc4random_uniform(13)+1))
// Check if the cards are equal
if firstCardImageView.image == secondCardImageView.image && firstCardImageView.image != "card" {
playRoundButton.userInteractionEnabled=false;
NSThread.sleepForTimeInterval(4)
playRoundButton.userInteractionEnabled=true;
}
}
Don't sleep in the main thread as this will stop all interactions with your app. You need to replace:
NSThread.sleepForTimeInterval(4)
playRoundButton.userInteractionEnabled=true;
with:
let enableTime = dispatch_time(DISPATCH_TIME_NOW, Int64(4 * Double(NSEC_PER_SEC)))
dispatch_after(enableTime, dispatch_get_main_queue()) {
playRoundButton.userInteractionEnabled=true;
}
First off, a better solution is not pausing at all and using dispatch_after to change the playRoundButton button state after 4 seconds.
If you want to stick with pausing, then you should give time for the UI to update itself before pausing. E.g.,
dispatch_async(dispatch_get_main_thread(), {
//Check if both cards are equal
if firstCardImageView.image == secondCardImageView.image && firstCardImageView.image != "card" {
playRoundButton.userInteractionEnabled=false;
NSThread.sleepForTimeInterval(4)
playRoundButton.userInteractionEnabled=true;
}
});
The fact is, when you assign a new image to your buttons, the button is actually redrawn on screen only at the next run-loop cycle, so if you pause before that is run, no visual change can be seen...
Keep in mind that pausing on the main thread will make your app unresponsive during that timeframe.
You're comparing two UIImage instances, which doesn't work with == because it will only compare the pointers. In your case, it would be much easier to compare the numbers that generated those images.
Other than that, you're pausing the main thread, which takes care of updating the user interface, so it doesn't actually get a chance to do so. One way to solve this problem is by using NSTimer.
#IBAction func playRoundTapped(sender: UIButton) {
let firstNumber = arc4random_uniform(13) + 1
let secondNumber = arc4random_uniform(13) + 1
firstCardImageView.image = UIImage(named: "card\(firstNumber)")
secondCardImageView.image = UIImage(named: "card\(secondNumber)")
if firstNumber == secondNumber {
playRoundButton.userInteractionEnabled = false;
NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "enableButton", userInfo: nil, repeats: false)
}
}
func enableButton() {
playRoundButton.userInteractionEnabled = true;
}
I am making an app where people can request two orders per hour maximum. I would like to disable the "Order" UIButton for a full 30 minutes once it is pressed. Can anybody suggest the most efficient way to do this but that fully prevents the user from ordering twice even if the app is killed? Thanks.
At a high level you need to do two things:
Calculate the NSDate for when the button should be enabled again and store that date in NSUserDefaults.
Start an NSTimer that goes off in 30 minutes. Enable the disabled button when the timer goes off and remove the date from NSUserDefaults.
More than likely the app will go into the background and the timer will stop long before the 30 minutes. This means that your app needs to stop the timer when it goes into the background. And when it returns to the foreground, you look at the date in NSUserDefaults and see how much time is left. If the time is already past, enable the button and delete the date from NSUserDefaults. If not, start another timer to go off after the needed amount of time as in step 2 above.
Here's the approach I thought of earlier to your problem. The three things you'll use are NSDate, NSTimeInterval, and NSUserDefaults
// I threw this in Xcode to aide me in typing this solution.
// You probably dragged a button from storyboard...leave it like that.
let orderButton: UIButton?
// Put toggleOrderButtonAvailability() in viewDidLoad and viewDidAppear
func toggleOrderButtonAvailability() {
// get current date
let currentDate = NSDate()
// we're not sure if there's a value for this, but we're creating a variable for it
// it will nil if the user hasn't placed an order
var lastOrderDate: NSDate?
// we're creating a variable to check
var timeSinceLastOrder: NSTimeInterval?
// if a value for the lastOrderDate saved in NSUserDefaults, then...
if NSUserDefaults.standardUserDefaults().objectForKey("lastOrderDate") != nil {
lastOrderDate = NSUserDefaults.standardUserDefaults().valueForKey("lastOrderDate") as? NSDate
// calculate minutes since last order
// 1800 seconds = 60 seconds per minute X 30 minutes
timeSinceLastOrder = (currentDate.timeIntervalSinceDate(lastOrderDate!)) / 1800
if timeSinceLastOrder < 30 {
orderButton?.enabled = false
// Some alert to let the user know they can't order for another X minutes
// TODO: you could create a class variable like "timeUntilButtonReenabled"
// and set it here, then the timer would run and call this method when it's
// done to re-enable the button. Set the timer in viewDidAppear()
} else {
orderButton?.enabled = true
}
}
}
You'll also want to set the lastOrderDate when you place an order and you can call the method we just created to disable the button when you place an order.
#IBAction func orderButtonAction(sender: UIButton) {
// Whatever you do when you send an order
// Set the lastOrderDate & disable the button
let currentDate = NSDate()
NSUserDefaults.standardUserDefaults().setObject(currentDate, forKey: "lastOrderDate")
toggleOrderButtonAvailability()
}
You should save the order date in NSUserdefaults.Once app launched,check the last order date and make an count down timer for that.
When the button is pressed, disable the button and log the current time using an NSDate object. To ensure it persists even if the app is killed, make sure you write it-- if you're app isn't already using a data system, NSUserDefaults is probably the easiest way to get about this.
Next, you need to create a mechanism for the button to enable again. The easiest reliable method to do so is by creating an NSTimer that checks whether or not the logged date is over 30 minutes ago, and if so, enable the button.
Here's an example of how to do this in Swift:
class ViewController: UIViewController {
#IBOutlet weak var btn: UIButton!
var enableTimer: NSTimer!
let defaults = NSUserDefaults.standardUserDefaults()
//Because this is a computed property, it auto-saves and persists
var lastPushed: NSDate {
get {
if let unwrappedDate = defaults.objectForKey("lastPushed") as? NSDate {
return unwrappedDate
} else { //If date not yet set
return NSDate(timeIntervalSince1970: 0)
}
} set { //NSDate is already compatible with NSUserDefaults
defaults.setObject(newValue, forKey: "lastPushed")
}
}
override func viewDidLoad() {
startTimer()
}
func startTimer() {
enableTimer = NSTimer.scheduledTimerWithTimeInterval(30, self, Selector("enableTim:"), nil, true)
}
#IBAction btnPressed() {
lastPushed = NSDate() //NSDate with current time
startTimer()
btn.enabled = false
}
func enableTim(timer: NSTimer) {
if (lastPushed.timeIntervalSinceNow < -1800) { //If last pressed more than 30 minutes ago
btn.enabled = true
enableTimer.stop()
}
}
}
I have a strange problem with my countdown timer. It fires off normally when my start button is hit, and is reinstantiated correctly when I close the app and relaunch it again. However, when I select a different tab and stay there for a while, it stops counting down, then resumes counting down from where it left off when I show the countdown tab again.
For example, if the timer is now at 00:28:00 (format is HH:MM:SS), select some other tab, stay there for 5 minutes, and then go back to the timer tab, it's only at the 27:52 mark. When I close the app (double tap the home button, swipe up my app) and reopen it, it starts off at a more reasonable 22:50 mark.
I've posted the relevant code from the class to show how I'm setting up the timer, but a summary of what it does:
I have plus (+) and minus (-) buttons somewhere that, when tapped, call recalculate().
recalculate() fires off a CalculateOperation.
A CalculateOperation computes for the starting HH:MM:ss based on the addition/removal of a new record. The successBlock of a CalculateOperation executes in the main thread.
A CalculateOperation creates the NSTimer in the successBlock if the countdownTimer hasn't been created yet.
The NSTimer executes decayCalculation() every 1 second. It reduces the calculation.timer by 1 second by calling tick().
Code:
class CalculatorViewController: MQLoadableViewController {
let calculationQueue: NSOperationQueue // Initialized in init()
var calculation: Calculation?
var countdownTimer: NSTimer?
func recalculate() {
if let profile = AppState.sharedState.currentProfile {
// Cancel all calculation operations.
self.calculationQueue.cancelAllOperations()
let calculateOperation = self.createCalculateOperation(profile)
self.calculationQueue.addOperation(calculateOperation)
}
}
func decayCalculation() {
if let calculation = self.calculation {
// tick() subtracts 1 second from the timer and adjusts the
// hours and minutes accordingly. Returns true when the timer
// goes down to 00:00:00.
let timerFinished = calculation.timer.tick()
// Pass the calculation object to update the timer label
// and other things.
if let mainView = self.primaryView as? CalculatorView {
mainView.calculation = calculation
}
// Invalidate the timer when it hits 00:00:00.
if timerFinished == true {
if let countdownTimer = self.countdownTimer {
countdownTimer.invalidate()
}
}
}
}
func createCalculateOperation(profile: Profile) -> CalculateOperation {
let calculateOperation = CalculateOperation(profile: profile)
calculateOperation.successBlock = {[unowned self] result in
if let calculation = result as? Calculation {
self.calculation = calculation
/* Hide the loading screen, show the calculation results, etc. */
// Create the NSTimer.
if self.countdownTimer == nil {
self.countdownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("decayCalculation"), userInfo: nil, repeats: true)
}
}
}
return calculateOperation
}
}
Well, if I leave the app in some other tab and not touch the phone for a while, it eventually goes to sleep, the app resigns active, and enters the background, which stops the timer.
The solution was to set my view controller as a listener to the UIApplicationWillEnterForegroundNotification and call recalculate to correct my timer's countdown value.