iOS Development – Integrating Maps

iOS Development Swift 2 – Integrating Maps ”; Previous Next Maps have become a part of everyone’s daily life. They have become so useful when we travel to places or search for some place. Integrating Maps and Locating India Gate We will make maps in our application, which will be showing us the India Gate in the center. We will learn maps by implementing in a project. So, create a single view iOS application and name it whatever you want. Adding Map Kit View Go to the object library and search for map kit view, click drag and bring it to your view, stretch it so that it fills the complete view. Adding Constraints Create an outlet for mapViewKit by control + drag to view the controller.swift file. It might show an error right now, but we will handle it. On top of the file, below the import UIKIT, add import MapKit, this will remove the error. After that, add MKMapViewDelegate after class ViewController: UIViewController. Now, the file should look like as follows − Now, we will create Latitude and Longitude, Delta, Span, Location and Region for our Map. Before that, we will tell you how to get latitude and longitude of a place. Go to maps.google.com and search for some location. On the top, we will see its latitude and longitude in the URL. For example: Let us search for India Gate. Setting Latitude and Longitude After getting the latitude and longitude, we will make variables for them. let latitude: CLLocationDegrees = 28.610 let longitude: CLLocationDegrees = 77.230 Setting Delta for Latitude and Longitude After adding latitude and longitude, we will add delta for them, which is the value that can verify our latitude and longitude. They should be kept minimum for more locations that are exact. let latDelta: CLLocationDegrees = 0.04 let lonDelta: CLLocationDegrees = 0.04 Setting Span, Location and Region for Map Then we will create a Span, Location and Region for our map. let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta) let location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) let region: MKCoordinateRegion = MKCoordinateRegion(center: location, span: span) Setting the Map We will set the map with the following command. mapView.setRegion(region, animated: true) Our final Application should look like the screenshot shown below. We should take care that our location set is exactly in the center of our application. This is all we will do with maps right now. Print Page Previous Next Advertisements ”;

iOS Development – Resources

iOS Development Swift 2 – Useful Resources ”; Previous Next The following resources contain additional information on iOS Development with Swift 2. Please use them to get more in-depth knowledge on this. Useful Video Courses iOS App Development Course 24 Lectures 1.5 hours Tutorialspoint More Detail The Complete XMPP Course: Android/iOS Apps Chat Server Setup 10 Lectures 1 hours Abhilash Nelson More Detail Setup Own VPN Server with Android, iOS, Win & Linux Clients 15 Lectures 1.5 hours Abhilash Nelson More Detail Flutter and Dart Development Course for Building IOS and Android Apps Most Popular 118 Lectures 10 hours Frahaan Hussain More Detail iOS Native XCUITEST (UITest) automation using Swift & Xcode 13 Lectures 39 mins Devasena Rajendran More Detail Basic Swift Code for iOS Apps 41 Lectures 2.5 hours Grant Klimaytys More Detail Print Page Previous Next Advertisements ”;

iOS Development – Advanced iOS

iOS Development with Swift2 – Advanced iOS ”; Previous Next In this chapter, we will cover some advance features like Creating Multiple Views on our Application, Adding Navigation Bars, Adding Table Views, Storing Data in the Application, Making Web Applications, etc. Please go through every section carefully, as this chapter contains most of the things, which we need while developing applications. Multiple View Controller In our previous applications, we only gave a single view / view controller. However, we can have multiple views in our application and we can perform on any of those independently. Therefore, we will start by creating a new project; the name of this project has multiple views. As every other project, this project also has a View Controller and a Swift File for that controller. (This you can see by selecting view & seeing its properties in the Identity Inspector.) The following screenshot shows how our current view looks like − On the right-hand side (Identity inspector), we can see the class that is related to our view controller. This arrow on the left is the entry point. This is the first view of our application that will show after the application starts running. Adding Second Multiple View Controller To add other view controllers to our application, we will search view controller in our object library. Once we find it, we will drag the view controller to our main.stroryboard, just outside any other views. This is how your application should look like. Now, we have added a view controller, but now we will also need to create a view controller class for our newly added view. Right click on your project → new File → cocoa Touch Class → Name it anything you want to, we will name it “SecondViewController”. This is how you create a class file for your view controller. Now, go back to your “main.storyboard”, click on your second view controller and see its Identity Inspector. The class field must be empty right now, so click on that field and start typing your class name that you added in last step. If it appears, click enter. We have now created a multiple view controller and added the controller class file for that View. However, if you run the application, it will not show your second view still. Why? Because we have not added a function, which will take us to that view. In short, we have not yet added Navigation to our Application. Do not worry; we will cover it in the following section. Adding Navigation to the Application The process of transition from one view to another is called Segueing, i.e. done by creating segue between the two views. To do this, add a button in the first view controller, and control drag from it to your second view. When you release the button, you will see some options as shown in the screenshot below. Select the Show option from the Action Segue. Now run your application, you will see that on the click of a button, you second view appears (To see more clearly add something in your second view, so that you can identify). However, now you cannot go back to your first view. For that, we have Navigation controllers. Adding a Navigation Controller Select your first view controller and in the top bar, click Editor → Embed in → Navigation controller. Now, our application should look like the following screenshot. We should see to it that, there is a little light grey row on top of the view. Now, when we Run the application, we can see that there is a navigation bar on top of the view. When we click on the button, we will go to second view, where we will see a back button in that navigation bar. Click on this and we will come back to the Initial View. Adding Title & Back Button to the Navigation Bar To add a title to your navigation bar, click on the navigation bar, and see its attribute inspector. There we will see − Title − This will be the title of Navigation bar, which appears in center. Prompt − This appears on top of title bar, in center. Back Button − Here you can modify the Text that appears in back button. Currently the button that is passing the view is located on our view, which might not suit if we want something else to appear on the screen. Therefore, we will add a Bar Button item in the navigation bar, which will take us to our second view. However, for this we should first delete the last button we added. Adding a Bar Button Item Search for bar button item in the object library and drag drop to the right hand side of the navigation bar. Name it as – “Next >”, control drag from it to the second view, select Show as we did with the last button we added. Now run the application, it will look cleaner and better. This is all we will do with Navigation right now. In the subsequent chapters, we will modify the navigation bar using the Swift Code, when required. Table Views A table presents data as a single column list containing multiple rows, which can be further divided into sections. Tables should be used to present data in a clean and efficient way. In this section, we will understand how to add table views, adding prototype cells, adding data source and delegates for a table view, changing properties of a table and setting dynamic data for table view cells. Adding a Table View To add a table view, we will first create a new project and name it as – “tableView”. Then, go to the object library and search for Table View, we will see the table view, the table view controller and many other options. However, we should select table view, drag it and add to the default view Controller. Adding a

iOS Development – Quick Guide

iOS Development with Swift 2 – Quick Guide ”; Previous Next iOS Development with Swift 2 – Xcode IDE To develop iOS applications, you need to have an Apple device like MacBook Pro, Mac Mini, or any Apple device with OS X operating system, and the following − Xcode − It can be downloaded from https://developer.apple.com/downloads/ You need an Apple developer account, which is free of cost. Simulator − This is your virtual iPhone/iPad (iOS Device), installed on your Computer, so you do not need to install this separately. About Xcode Xcode is the default IDE (Integrated Development Environment) provided by apple for iOS/OS X Application Development. It is an IDE that includes everything you need for developing iOS, OS X, Watch OS, TV OS Applications, etc. To Run Xcode, you must have − A Mac Device Running OS X, i.e., Apple’s official Operating System. Apple ID (Free of Cost): To download the IDE. Installation To install Xcode in your device, follow the subsequent steps. Skip if you have already installed. Go to the App Store, Login if you have not already, search for Xcode. Click on Get and Install. Once Downloaded, go to Applications or Launchpad and Run your application. On the First Run, it might ask for some additional Downloads, let it download everything, enter password if it asks for. Once all this is done, the Welcome Screen will appear as shown below. iOS Development Swift 2 – First Application In this tutorial, we will be learning some of the elementary concepts of our iOS development, which include − Making a New Project Features of our IDE Navigating through the IDE Adding a Label to your View Running the application Adjusting the simulator according to your comfort If you are a beginner, then this tutorial is going to be of immense help. Creating a New Xcode Project To create a new Xcode Project, we should follow the steps given below. Step 1 − Click on the Xcode icon in your Launchpad, then select Create a new Xcode project. Step 2 − Select iOS and then select Single View Application, click on Next. Step 3 − The subsequent screen that comes up will have a few fields to fill. The following points explain how to fill each of these fields. Enter the Project Name − it can be a name resembling your project. The Team Field can be left empty for now. It is used when we make an application in the team. The organization name is the name of your organization or if it is your personal project, you can name it anything. It does not matter until you want to publish your app on the app store. Identifier is generally a unique identifier for your application, which must not match any other app on the app store (only when you choose to upload your app on app store). Language will be Swift, device will be universal, and all other options will be unchecked for now. Once all the details are filled, click the Next button. Step 4 − Select the location where you want to store the project. Let “Create Git Repository” checkbox be unchecked for now, as we do not need it right now. Click on Create. Congratulations! Your project has been created. Navigation through Xcode Environment Click on the Main.storyboard option inside your navigator panel. This will bring up the main view, which will appear when the application runs. Adding Labels In the right bottom corner of your screen there is a search bar. Type label in that search bar and press return. After searching the Label drag and drop the Label to your main view. Double click on the label text and Type “Hello World”. Drag the label to the center of the view, when the label is exactly in the center, two lines intersecting at the center will appear. Now your view should look like the following screenshot. Running the Application Select your device, click on the Play button at the top right corner. This is our final application, running on the iPhone 7 simulator. Adjusting Simulator Properties When we run our application for the first time, the screen of your simulator might not be fit for your desktop or laptop screen. So, while your simulator is running in the foreground, click on Window → Scale, and choose a Simulator Screen size percentage that will suit your display. We will continue discussing about the simulator features, as and when we use them in this tutorial. Well done, this was the First Application, which you completed successfully. Cheers! Making the App Interactive In this chapter, we will introduce some new things and UI features that iOS provides for interaction with the user. We will be adding − Text Fields Labels Buttons and their actions Additionally, we will be writing the code in swift for a Dynamic Label, which will show the calculated result of the input entered by the user. By the title “Making our app interactive”, we mean making our application interact with the user. Therefore, here we give user the power to interact and control the application. Adding Text Fields Here, we will again make a new project. It should be easily manageable, as we have already discussed how to create a new project in Xcode. Okay, so we will now create a New Project Called “My Dog’s Age.” After creating this project, we will click on our “Main.storyboard” File and follow the steps given below. In the utility Pane’s Search bar (located in the bottom right corner of Xcode), search for Label. Click and Drag that Label to your main.storyboard / (View). Then, double click on the label and rename it as − “My Dog’s Age”. Search for “text field”, click and drag that text field to your View. While this text field is selected, go to attribute inspector and change the keyboard type to Number pad, so that only number can be entered as shown in

iOS Development – Auto Layouts

iOS Development with Swift 2 – Auto Layouts ”; Previous Next When we make iOS applications and we add UI Elements in that application, they might seem perfect on one device. Nevertheless, now we should try the same application on some other device. We will certainly see drastic changes in the UI and some elements might not appear as well. Auto layout is the technique that we will use to resolve this issue. In this chapter, we will understand how to make Auto Layout, Apply Constraints and Stack View to make your application look perfect and best on every Device. We will start by making a new single view application. Adding Elements Add a label in the top center of the view and one in the bottom right of the view as shown below − Now, try to change the orientation and we will see that the right bottom does not appear, while the Center is not in the center. (Tip − You do not need to run the simulator to see layouts, just click on View as − iPhone x, in the bottom of the screen as shown in the following screenshot.) Select the iPhone version and the orientation. We will see that the UI elements are not arranged properly. Therefore, when we change our orientation, device, or both, the right bottom label will disappear and the center will not be in the center. This happens because we have not specified the fixed position for elements. To solve this, we will use Constraints. Applying Constraints to UI Elements Click on the Center Label, press control and drag anywhere inside the view, release. Now you must be seeing − Select Center Horizontally in Container. Again, repeat the above step and choose, Vertical Spacing to Top Layout Guide. Now, click on Add new constraint button and select height and width, and click Add 2 Constraints. Click on the Right bottom label, control drag from label to anywhere inside the view, and choose “Trailing space to container Margin”. Similarly choose, Vertical spacing to Bottom layout Guide. (Tip − To select multiple options at once, press shift and choose the options. Ensure that you do not release shift until you have selected everything.) After applying all the constraints, the view should look as follows − Stack View Stack view works by arranging elements in stacks. After arranging, we define the constraints only once, and all the elements are arranged accordingly. To start with stack view, create the following view, which will not look better in other devices. However, we will make it suitable for other devices in this section. Now, select the top two buttons – select one button, press command and then select the second one. To embed them in a stack view, go to editor → embed In → stack view. OR At the bottom right-hand corner, there is an option Select this option and this will embed the views into the stack view. Horizontal Stack View The Horizontal Stack View would look as shown in the following screenshot. While this stack view is selected, go to attribute inspector. Change Distribution to Fill Equally and Spacing to 10. Now, select this stack view and the bottom button and again embed into the stack view. This time the stack view axis will be vertical, while in the previous stack view, it was horizontal. Vertical Stack Views Now your view will look like − While this stack view is selected, go to its attribute inspector and make sure it matches the screenshot below. Now, your view should look as follows − The last step is to make constraints for this stack view. Select the stack view → Click on add new constraint button. This will open a new window in which we have to follow the steps shown in the next screenshot. Adding Constraints to Stack View The following screenshot will describe how to add constraints to a stack view. That is all we will do with auto layouts. In the next chapter, we will discuss about Animations. That is all we will do with auto layouts. In the next chapter, we will discuss about Animations. Print Page Previous Next Advertisements ”;

Interview Questions

Interview Questions ”; Previous Next What is Xcode? Xcode is Apple’s integrated development environment (IDE) that you use to design apps for Apple products. It provides various tools to manage your entire development workflow from creating your app, to testing, submitting and optimizing it to the App store. How multiple line comments can be written in swift? Multiple line comments can be written as forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). What are the collection types available in Swift? Multiple line comments can be written as forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). What are the control transfer statements used in Swift? Control transfer statements used in Swift include − Break Continue Fallthrough Return How base-class is defined in Swift? In Swift, the classes are not inherited from the base class and the classes that you define without specifying its superclass, automatically becomes the base-class. What is the characteristics of Switch in Swift? It supports any kind of data and not only Synchronizes it, but also checks for equality. The Switch statement must be exhaustive, which means that you have to cover all possible values for your variable. What is the question mark (?) in Swift? The question mark (?) is used during the declaration of a property. It tells the compiler that this property is optional and whether the property may hold a value or not. What is the use of double question marks (??)? To provide a default value for a variable. What is the difference between let and var in Swift? The let keyword is used to declare constants, while var is used for declaring variables. What is a guard statement in Swift? The guard statement evaluates a Boolean condition and proceeds with program execution if the evaluation is true. What is GCD? How is it used? GCD is the most commonly used API to manage concurrent code and execute operations asynchronously at the UNIX level of the system. GCD provides and manages queues of tasks. What is Synchronous vs. Asynchronous in GCD? A synchronous function returns only after the completion of a task that it orders. An asynchronous function, on the other hand, returns immediately, ordering the task to be done, but not waiting for it. What is MVC? MVC is a design pattern that stands for Model View Controller. This design pattern separates the data from its display, mediated by a View Controller. What are delegates? Delegates are a design pattern. A delegate is just an object that another object sends messages to when certain things happen. What is Core Data? Core Data is an object graph manager, which also has the ability to persist object graphs to a persistent store, on a disk. What is the purpose of the reuseIdentifier? Reusability of an already allocated object. Print Page Previous Next Advertisements ”;

Making Applications with Swift

Making Applications with Swift ”; Previous Next In this chapter, we will create two new Applications using Swift. First Application – “Guess the Number” In this section, we will create an Application called “Guess the number”. To make this application, create a new iOS Single View Application and name it whatever you want. Click on the main.storyboard and select your main view. Add a text label → Change the text to “Guess the number”. Change the color, size properties and make it as per your requirement. Add an Input field, stretch to full view. Add a button and name it “Guess. Add one more label, stretch it, and clear the text box. This is how your view should look like after adding all the elements. Now Switch to assistant editor and Click on drag from your UI element to view controller file, then connect the text field as an outlet and name it userInput. Similarly, Connect empty label as outlet and name it as resultLabel. Connect Guess button as action and name it as guessButtonPressed. What is the logic? The logic is simple, we will generate random numbers between 0-9 and see if that is equal to the number the user entered, or not. If it is equal, we will show “you are right”, else we will show “you are wrong!”. Applying the logic To generate a random number between 0-9, we will use the following command. let rollIt = String(arc4random_uniform(10)) Then we will use the following logic to check whether it is same as the user input or not. if userInput.text == rollIt { resultLabel.text = “You”re right!” } else { resultLabel.text = “Wrong! It was a ” + rollIt + “.” } This is how your final logic in button action function will look like. @IBAction func guessButtonPressed(_ sender: Any) { let rollIt = String(arc4random_uniform(10)) if userInput.text == rollIt { resultLabel.text = “You”re right!” } else { resultLabel.text = “Wrong! It was a ” + rollIt + “.” } } Your final application should look like this now. Let us now run our Application and check its output. The opening screen should look as follows − Next, feed a number in the input area. Let’s feed another number and check its output − We have completed one more Application. Try to run this application, and enter different inputs. Second Application – “Is It Prime” In this application, we will be taking an input from the user and we will check whether that number is prime or not − Layout − Similar to the previous application, we need an input, a button and an output label. Challenges − Create the UI and connect the elements to code. Also, try if you can create the complete project yourself. If you managed to create it by yourself, then it is great and you are doing excellent with iOS Development. If you could not manage, do not worry. Look at the following image and try to do the same. Try to create a view like this, if you are not able to do it yet, please read the previous section where we have developed a Guess Game. What is the Logic? Prime numbers are the numbers that cannot be divided by any other number except 1 and the number itself. Example − 7 is a prime number as any other number except 1 and 7 cannot divide it. How to Implement? Try to write a code for checking prime numbers. Then take the user input and see if that is a prime or not. If yes, then show prime; otherwise show not prime in your result label. Here is the code to check whether a supplied number is “prime” or not − @IBAction func isItPrimeButtonPressed(_ sender: Any) { if let userEnteredString = userInput.text { let userEnteredInteger = Int(userEnteredString) if let number = userEnteredInteger { var isPrime = true if number == 1 { isPrime = false } var i = 2 while i < number { if number % i == 0 { isPrime = false } i += 1 } if isPrime { resultLabel.text = “yes. (number) is prime!” } else { resultLabel.text = “No. (number) is not prime” } } else { resultLabel.text = “Please enter a positive whole number” } } } This is how your button action should look like. Following is the image of the final code and view − This is how your running application should look like if you followed the procedure. Now, let us test our Application by supplying input values − Print Page Previous Next Advertisements ”;

iOS Development – First Application

iOS Development Swift 2 – First Application ”; Previous Next In this tutorial, we will be learning some of the elementary concepts of our iOS development, which include − Making a New Project Features of our IDE Navigating through the IDE Adding a Label to your View Running the application Adjusting the simulator according to your comfort If you are a beginner, then this tutorial is going to be of immense help. Creating a New Xcode Project To create a new Xcode Project, we should follow the steps given below. Step 1 − Click on the Xcode icon in your Launchpad, then select Create a new Xcode project. Step 2 − Select iOS and then select Single View Application, click on Next. Step 3 − The subsequent screen that comes up will have a few fields to fill. The following points explain how to fill each of these fields. Enter the Project Name − it can be a name resembling your project. The Team Field can be left empty for now. It is used when we make an application in the team. The organization name is the name of your organization or if it is your personal project, you can name it anything. It does not matter until you want to publish your app on the app store. Identifier is generally a unique identifier for your application, which must not match any other app on the app store (only when you choose to upload your app on app store). Language will be Swift, device will be universal, and all other options will be unchecked for now. Once all the details are filled, click the Next button. Step 4 − Select the location where you want to store the project. Let “Create Git Repository” checkbox be unchecked for now, as we do not need it right now. Click on Create. Congratulations! Your project has been created. Navigation through Xcode Environment Click on the Main.storyboard option inside your navigator panel. This will bring up the main view, which will appear when the application runs. Adding Labels In the right bottom corner of your screen there is a search bar. Type label in that search bar and press return. After searching the Label drag and drop the Label to your main view. Double click on the label text and Type “Hello World”. Drag the label to the center of the view, when the label is exactly in the center, two lines intersecting at the center will appear. Now your view should look like the following screenshot. Running the Application Select your device, click on the Play button at the top right corner. This is our final application, running on the iPhone 7 simulator. Adjusting Simulator Properties When we run our application for the first time, the screen of your simulator might not be fit for your desktop or laptop screen. So, while your simulator is running in the foreground, click on Window → Scale, and choose a Simulator Screen size percentage that will suit your display. We will continue discussing about the simulator features, as and when we use them in this tutorial. Well done, this was the First Application, which you completed successfully. Cheers! Print Page Previous Next Advertisements ”;

iOS Development – Swift Playground

iOS Development with Swift 2 – Playground ”; Previous Next In this chapter, we will introduce a new environment where we can write and execute a swift code. We will also cover the following aspects of the swift playground − Variables Dictionaries Arrays Loops Classes & Objects Note − We will look only through those basic concepts, which we will be using in this tutorial, if you want to learn swift deeply, you can check our Swift Tutorial. Playground is a tool provided with Xcode for executing the swift code. We will start by creating a new playground. Starting Swift Playground To create a swift playground, click on Xcode icon, and choose the first option, get started with a swift playground. Give the name to your playground and select Platform as iOS. Let us name our playground as the Demo Playground. Click on Next. These are the only steps you need to follow to make a Playground. The following screenshot shows the Playground. Sr.No Basic Concepts & Description 1 Variables A variable is a memory / storage that our program can use to store and manipulate data. Each variable has a specific data type, which determines the size that a variable will occupy in memory. 2 Dictionaries A dictionary is a collection that stores values in a key value pair, i.e. the data stored in a dictionary is stored in a method where each value is related to a key. Here, every key is unique and cannot appear twice in the same dictionary. 3 Arrays Arrays are the data types that store the same type of data in an ordered list. The same value can appear at multiple indexes/Places in an array. 4 Loops (Control Flow) Swift provides a variety of Control Flow Statements. Loops are generally used to iterate over a condition or statement multiple times, until a Condition / Programmer’s need from that loop is satisfied. 5 Classes and Objects Classes are the general-purpose flexible constructs that are the building blocks of your program’s code. Object is the term that is generally used to refer to instance of a class, so we can call it instance instead of objects. Print Page Previous Next Advertisements ”;

iOS Development – Animations

iOS Development with Swift 2 – Animations ”; Previous Next Animation is an important part of any Application as it draws user attention to the application. Animation is just a collection of images that repeat at a fast rate. It also makes your application stand different from others. Making an Animation Project − Kitty Animation This will be a simple project, wherein we will run an animation when a button is clicked. We will use multiple images to create a GIF, so download any GIF and convert it to images, which will give you multiple frames of that GIF. In this section, we will use the following images. These images, when played together, create an animation. Therefore, we will make a single view application. We will then drag an image view option, a label and a button inside the main view controller. Once this is done, we will connect the image view and the button to our swift file. (If you do not want to use these images, search for any gif and convert it to image online by using some gif to image converter.) Inside the button action, we will insert the following command to show the image when a button is pressed. imageView.image = UIImage(named: “frame_0_delay-0.1s.gif”) // frame_0_delay-0.1s.gif, is the name of image This is how we programmatically assign image to an Image View. The view should now look as follows − The first view will appear when we run the application. When we click on the Animate Button, the image will appear. This is not an animation, but just an image. We will create the animation now − Add a variable below the image outlet: var counter = 1. See that our images have a common name and only one character is different. Change your animate button’s code as shown below − @IBAction func animatePressed(_ sender: Any) { imageView.image = UIImage(named: “frame_(counter)_delay-0.1s.gif”) counter += 1 if counter == 9 { counter = 0 } } Now, when you press the animate button, the image changes every time. The next step is to create the following − Create a variable − isAnimating and assign False to it. Create a timer variable and assign a Timer() function to it. Once the above two steps are done, then create a function animate and paste the following code. func animate() { imageView.image = UIImage(named: “frame_(counter)_delay-s.gif”) counter += 1 if counter == 9 { counter = 0 } } Where, counter is our counter variable we made in the previous file. Now, inside the animate button function, add the following code − if isAnimating { timer.invalidate() isAnimating = false } else { timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.animate), userInfo: nil, repeats: true) isAnimating = true } Try to run the application and we will see that an animation is being run on your device. Challenge − Add a stop button that will stop the animation. Print Page Previous Next Advertisements ”;