In this blog we will learn about how to Stop App Touch Ability While Loading A Page In iOS.
Introduction
Disabling touch interactions while a page loads in iOS helps prevent user errors and enhances the overall experience. By implementing a loading overlay or spinner, users are visually informed that the page is still processing.
This is helpful for better user experince.
Implementation
1. Create New Project:
First of all you need to create a new project where you will implement Stop App Touch Ability While Loading A Page.
2. Create ViewController:
After that you need to create a UIViewController (ViewController).
1 2 3 4 5 6 |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() } } |
A ViewController is a fundamental building block of an app’s user interface. It manages a single screen of content and handles user interactions.
3. Create A TableViewCell:
After that you need to create a UITableViewCell, A UITableViewCell is a reusable component in iOS that represents a single row in a UITableView. It displays content such as text, images, and custom views.
And add the UIImageView in that particular cell.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class TableViewCell: UITableViewCell { @IBOutlet weak var imageview: UIImageView! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } |
4. Register TableView:
In this step we will register the tableview in ViewController.
1 2 3 4 |
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "tableview") tableView.delegate = self tableView.dataSource = self tableView.rowHeight = 80 |
The above code is helpful to register the tableview in ViewController, you need ti add above mention code in side viewDidLoad().
1 2 3 4 5 6 7 8 9 |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 30 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell:UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "tablecell") cell.imageView?.image = UIImage(named: "product") return cell } |
Above mention method is helpful to used to define the data source methods for a UITableView, specifically specifying how many rows it contains and how to configure each cell.
5. Disable touch ability:
1 |
UIApplication.shared.beginIgnoringInteractionEvents() |
To disable to touch ability of tab bar and navigation bar you have to use the beginIgnoringInteractionEvents function of the UIApplication class. above mention code is helpful to disable the touch ability.
6. Enable to touch ability:
1 |
UIApplication.shared.endIgnoringInteractionEvents() |
To enable touch ability you need to use above mention code.
7. Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.asyncAfter(deadline: .now() + 3) { UIApplication.shared.endIgnoringInteractionEvents() } UIApplication.shared.beginIgnoringInteractionEvents() tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "tableview") tableView.delegate = self tableView.dataSource = self tableView.rowHeight = 80 // Adjust as needed } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 30 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell:UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "tablecell") cell.imageView?.image = UIImage(named: "product") return cell } } |
In the above code, the app’s touch interactions are disabled for 3 seconds after the page loading.
For knowing more about tableview you can check here.
Output:
Stop App Touch Ability While Loading A Page.
Disabling touch interactions while a page loads in iOS helps prevent user errors and enhances the overall experience.
Conclusion
In this article we have discussed about Stop App Touch Ability While Loading A Page in iOS.
I hope this blog is helpful to understand this topic.
Thanks for reading this blog. You can also check other blogs from here for more knowledge.