• No results found

Chapter 5 Ghosts of the Horseshoe iOS Application

5.2 Objective-C Classes

We have made use of only a small subset of the many classes made available in Xcode, Objective-C, and the iOS libraries. Ghosts of the Horseshoe makes extensive use of

UIViewcontroller, UIView, UIButton, UIScrollView, and UIGestureRecognizer. In conjunction with the navigation controller, a UIViewcontroller helps to control the flow of the application. It is an intermediary between one or more application views whose function it is to interpret user interactions with a view’s content features and to request actions from the operating system so the device knows what actions to perform. TheUIView is where all visible elements of the application are presented, including images, buttons, text fields, etc. AUIView can also be trained to recognize gestures and relay them to the view controller for action. TheUIViewcontrollerand

UIVieware part of a Model-View-Controller design pattern that specifically separates user interface (UI) display and background control to simplify coding.

Similar to the app delegate’s control of the schedule for the puppeteers and the navigation stack’s control of the foreground, the UIViewController and the

UIView control the setting of the engagement. The navigation stack sets the loca- tion, but it does not set neither the style of the stage nor the background props.

UIViewControllers and UIViews both inherit from the NSResponder NSObject in the UIKit framework, but both have different functions. UIViewControllers are built for navigation and have many functions designed for interacting with the stack.

UIViews are built for drawing to the screen and commonly have functions designed for modifying the visual display of the view. Only UIViewControllers can interact with the navigation stack. This means that neither buttons nor gesture recognizers created in a UIView can directly transition to a new view; such transitions must be done by invoking a delegate or passing through the Notification Center. The assets in the UIView are implemented in the UIViewcontroller. The UIView assets are free- standing and can be transferred from one view to another, but a UIView cannot be displayed to a screen without a view controller. Conversely, theUIViewControlleris not as flexible in its display capability as a UIView. While aUIViewController has a primitive UIView present within it, creating an independent UIView object helps

Figure 5.2 Views and Subviews

to modularize large parts of code and simplifies structure overall. Each UIView can contain a single idea and loads all related displayable content. Stacking each UIView

layer on top of each other allows for a multi-part dynamic display that separates all complicated parts into modules but creates the illusion of single image. A good example of this dynamic inGhostsis the participant icon trail that is drawn onto the map in real time. The map and participant icon are all stored in separate UIViews that are connected to theUIViewController. The trail that follows the participant icon around the map can only be done in theUIView with thedrawrecmethod. (See Figure 5.2.)

Other class objects such as UIButton, UIScrollView and UILabel add special functionalities common to interactive applications. UIButton is derived from the base class UIControl, which is used to convey user intent to an application. While

UIControl cannot be used directly, its subclasses are used by many objects to es- tablish a common behavioral structure. UIButton allows for “button” events, such

as pressing inside or outside a button, holding one’s finger on a button, etc. Ghosts uses buttons for loading content, switching to and returning from views, and for di- recting the application to play video and audio assets. UIScrollView is a special view with a built-in gesture recognizer that implements scrolling. Gesture proper- ties like multi-touch swiping, tapping, pinching, rotating, multi-finger actions, etc., are implemented in iOS applications using the UIGestureRecognizer. Unlike the

UIbutton or UIScrollView, which are both implemented with a visual cue on the screen, theUIGestureRecognizer’s presence is invisible. TheUIGestureRecognizer

records and waits for actions. We have used both buttons and gesture recognizers in Ghosts; they provide transitions of content views and cause actions via the navigation controller.

Within Class objects, there are two types of functions: “class functions” and “instance functions”. Class functions are usually signified by a “+” in Xcode and are global functions of a class that can be called at any time. Instance functions, signified with a “-” in Xcode, can only be accessed by creating an instance of the class and calling the function from the object. This is important because class functions can be called globally without having to create an object, while the instance functions are perfect for maintaining specific individual objects. One issue that we have had to consider while codingGhostsis that class functions and instance functions cannot mix (e.g., a class function cannot call an instance function and vice versa).

All constructed classes have an associated header file (filename.h) and an associ- ated implementation file (filename.m). The header file contains forward declarations of all methods that need to be accessed by other class objects and defined properties of the class. All required imports for the class are also contained in the header file. The implementation file contains an interface similar to what is seen in the header file, but only contains global variables. The implementation section in the implementation file contains any synthesized variables at the top, followed by the standard initialization

functions, defined initialization functions, and functions used to maintain the class.