archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • An Example iOS 9 Touch, Multitouch and Tap Application - Techotopia
    Implementing the touchesBegan Method When the user touches the screen the touchesBegan method of the first responder is called In order to capture these event types we need to implement this method in our view controller In the Xcode project navigator select the ViewController swift file and add the touchesBegan method as follows override func touchesBegan touches Set UITouch withEvent event UIEvent let touchCount touches count let touch touches first let tapCount touch tapCount methodStatus text touchesBegan touchStatus text touchCount touches tapStatus text tapCount taps This method obtains a count of the number of touch objects contained in the touches set essentially the number of fingers touching the screen and assigns it to a variable It then gets the tap count from one of the touch objects The code then updates the methodStatus label to indicate that the touchesBegan method has been triggered constructs a string indicating the number of touches and taps detected and displays the information on the touchStatus and tapStatus labels accordingly Implementing the touchesMoved Method When the user moves one or more fingers currently in contact with the surface of the touch screen the touchesMoved method is called repeatedly until the movement ceases In order to capture these events it is necessary to implement the touchesMoved method in our view controller class override func touchesMoved touches Set UITouch withEvent event UIEvent let touchCount touches count let touch touches first let tapCount touch tapCount methodStatus text touchesMoved touchStatus text touchCount touches tapStatus text tapCount taps Once again we report the number of touches and taps detected and indicate to the user that this time the touchesMoved method is being triggered eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Implementing the touchesEnded Method When the user removes a finger from the screen the touchesEnded method is called We can therefore implement this method as follows override func touchesEnded touches Set UITouch withEvent event UIEvent let touchCount touches count let touch touches first let tapCount touch tapCount methodStatus text touchesEnded touchStatus text touchCount touches tapStatus text tapCount taps Getting the Coordinates of a Touch Although not part of this particular example it is worth knowing that the coordinates of the location on the screen where a touch has been detected may be obtained in the form of a CGPoint structure by calling the locationInView method of the touch object For example let touch touches first let point touch locationInView self view The X and Y coordinates may subsequently be extracted from the CGPoint structure by accessing the corresponding elements let pointX point x let pointY point y Building and Running the Touch Example Application Build and run the application on a physical iOS device by clicking on the run button located in the toolbar of the main Xcode project window With each tap and touch on the device screen

    Original URL path: http://www.techotopia.com/index.php/An_Example_iOS_9_Touch,_Multitouch_and_Tap_Application (2016-02-13)
    Open archived version from archive


  • Detecting iOS 9 Touch Screen Gesture Motions - Techotopia
    the programming language Designing the Application User Interface The application will display the X and Y coordinates of the touch and update these values in real time as the finger moves across the screen When the finger is lifted from the screen the start and end coordinates of the gesture will then be displayed on two label objects in the user interface Select the Main storyboard file and using Interface Builder create a user interface such that it resembles the layout in Figure 54 1 Be sure to stretch the labels so that they both extend to cover a little over half of the width of the view layout Select the top label object in the view canvas display the Assistant Editor panel and verify that the editor is displaying the contents of the ViewController swift file Ctrl click on the same label object and drag to a position just below the class declaration line in the Assistant Editor Release the line and in the resulting connection dialog establish an outlet connection named xCoord Repeat this step to establish an outlet connection to the second label object named yCoord Figure 54 1 Next review the ViewController swift file to verify that the outlets are correct then declare a property in which to store coordinates of the start location on the screen import UIKit class ViewController UIViewController IBOutlet weak var xCoord UILabel IBOutlet weak var yCoord UILabel var startPoint CGPoint eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Implementing the touchesBegan Method When the user first touches the screen the location coordinates need to be saved in the startPoint instance variable and those coordinates reported to the user This can be achieved by implementing the touchesBegan method in the ViewController swift file as follows override func touchesBegan touches Set UITouch withEvent event UIEvent if let theTouch touches first startPoint theTouch locationInView self view let x startPoint x let y startPoint y xCoord text x x yCoord text y y Implementing the touchesMoved Method When the user s finger moves across the screen the touchesMoved event will be called repeatedly until the motion stops By implementing the touchesMoved method in our view controller therefore we can detect the motion and display the revised coordinates to the user override func touchesMoved touches Set UITouch withEvent event UIEvent if let theTouch touches first let touchLocation theTouch locationInView self view let x touchLocation x let y touchLocation y xCoord text x x yCoord text y y Implementing the touchesEnded Method When the user s finger lifts from the screen the touchesEnded method of the first responder is called The final task therefore is to implement this method in our view controller such that it displays the end point of the gesture override func touchesEnded touches Set UITouch withEvent event UIEvent if let theTouch touches first

    Original URL path: http://www.techotopia.com/index.php/Detecting_iOS_9_Touch_Screen_Gesture_Motions (2016-02-13)
    Open archived version from archive

  • Identifying Gestures using iOS 9 Gesture Recognizers - Techotopia
    more fingers for a specified period of time also referred to as touch and hold These gesture recognizers must be attached to the view on which the gesture will be performed via a call to the view object s addGestureRecognizer method Recognizers must also be assigned an action method that is to be called when the specified gesture is detected Gesture recognizers may subsequently be removed from a view via a call to the view s removeGestureRecognizer method passing through as an argument the recognizer to be removed Recognizer Action Messages The iOS gesture recognizers use the target action model to notify the application of the detection of a specific gesture When an instance of a gesture recognizer is created it is provided with the reference to the method to be called in the event that the corresponding gesture is detected Discrete and Continuous Gestures Gestures fall into two distinct categories discrete and continuous A discrete gesture results in only a single call being made to the corresponding action method Tap gestures including multiple taps are considered to be discrete because they only trigger the action method once Gestures such as swipes pans rotations and pinches are deemed to be continuous in that they trigger a constant stream of calls to the corresponding action methods until the gesture ends Obtaining Data from a Gesture Each gesture action method is passed as an argument a UIGestureRecognizer sender object which may be used to extract information about the gesture For example information about the scale factor and speed of a pinch gesture may be obtained by the action method Similarly the action method assigned to a rotation gesture recognizer may ascertain the amount of rotation performed by the user and the corresponding velocity eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Recognizing Tap Gestures Tap gestures are detected using the UITapGestureRecognizer class This must be allocated and initialized with an action selector referencing the method to be called when the gesture is detected The number of taps that must be performed to constitute the full gesture may be defined by setting the numberOfTapsRequired property of the recognizer instance The following code for example will result in a call to the tapsDetected method when two consecutive taps are detected on the corresponding view let doubleTap UITapGestureRecognizer target self action tapDetected doubleTap numberOfTapsRequired 2 self view addGestureRecognizer doubleTap A template method for the action method for this and other gesture recognizers is as follows func tapDetected Code to respond to gesture here Recognizing Pinch Gestures Pinch gestures are detected using the UIPinchGestureRecognizer class For example let pinchRecognizer UIPinchGestureRecognizer target self action pinchDetected self view addGestureRecognizer pinchRecognizer Detecting Rotation Gestures Rotation gestures are recognized by the UIRotationGestureRecognizer the sample code for which is as follows let rotationRecognizer UIRotationGestureRecognizer target self action rotationDetected self view addGestureRecognizer

    Original URL path: http://www.techotopia.com/index.php/Identifying_Gestures_using_iOS_9_Gesture_Recognizers (2016-02-13)
    Open archived version from archive

  • An iOS 9 Gesture Recognition Tutorial - Techotopia
    to Suggested Constraints menu option listed in the All Views in View Controller section of the menu Select the label object in the view canvas display the Assistant Editor panel and verify that the editor is displaying the contents of the ViewController swift file Ctrl click on the same label object and drag to a position just below the class declaration line in the Assistant Editor Release the line and in the resulting connection dialog establish an outlet connection named statusLabel Next the non visual gesture recognizer objects need to be added to the design Scroll down the list of objects in the Object Library panel until the Tap Gesture Recognizer object comes into view Drag and drop the object onto the View in the design area if the object is dropped outside the view the connection between the recognizer and the view on which the gestures are going to be performed will not be established Repeat these steps to add Pinch Rotation Swipe and Long Press Gesture Recognizer objects to the design Note that the document outline panel which can be displayed by clicking on the panel button in the lower left hand corner of the storyboard panel has updated to reflect the presence of the gesture recognizer objects as illustrated in Figure 56 1 An icon for each recognizer added to the view also appears within the toolbar across the top of the storyboard scene Figure 56 1 Within the document outline panel select the Tap Gesture Recognizer instance and display the Attributes Inspector View Utilities Show Attributes Inspector Within the attributes panel change the Taps value to 2 so that only double taps are detected Similarly select the Long Press Recognizer object and change the Min Duration attribute to 3 seconds Having added and configured the gesture recognizers the next step is to connect each recognizer to its corresponding action method Display the Assistant Editor and verify that it is displaying the content of ViewController swift Ctrl click on the Tap Gesture Recognizer object either in the document outline panel or in the scene toolbar and drag the line to the area immediately beneath the viewDidLoad method in the Assistant Editor panel Release the line and within the resulting connection dialog establish an Action method configured to call a method named tapDetected with the Type value set to UITapGestureRecognizer as illustrated in Figure 56 2 Figure 56 2 Repeat these steps to establish action connections for the pinch rotation swipe and long press gesture recognizers to methods named pinchDetected rotationDetected swipeDetected and longPressDetected respectively taking care to select the corresponding type value for each action eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Implementing the Action Methods Having configured the gesture recognizers the next step is to add code to the action methods that will be called

    Original URL path: http://www.techotopia.com/index.php/An_iOS_9_Gesture_Recognition_Tutorial (2016-02-13)
    Open archived version from archive

  • An iOS 9 3D Touch Force Handling Tutorial - Techotopia
    on the 3DTouchForce folder entry and select New File from the resulting menu In the New File window select the iOS source Cocoa Touch Class icon and click Next On the subsequent options screen change the Subclass of menu to UIView and the class name to MyUIView Click Next and on the final screen click on the Create button Select the Main storyboard file and select the UIView component in either the view controller canvas or the Document Outline panel Display the Identity Inspector View Utilities Show Identity Inspector and change the Class setting from UIView to our new class named MyUIView With the UIView object still selected display the Attributes Inspector panel and change the background color to green eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Locating the drawRect Method in the UIView Subclass Now that we have subclassed our application s UIView instance the next step is to implement the drawRect method in this subclass Fortunately Xcode has already created a template of this method for us To locate this method select the MyUIView swift file in the Project Navigator panel Having located the method in the file remove the comment markers and within which it is currently encapsulated import UIKit class MyUIView UIView override func drawRect rect CGRect Drawing code Remaining within the MyUIView swift file add a variable to store the height of the rectangle and implement the drawing code in the drawRect method as follows class MyUIView UIView var size CGFloat 0 override func drawRect rect CGRect let view width self bounds width let view height self bounds height let context UIGraphicsGetCurrentContext let rectangle CGRectMake 0 view height size view width size CGContextAddRect context rectangle CGContextSetFillColorWithColor context UIColor redColor CGColor CGContextFillRect context rectangle The code added identifies the height and width of the visible area of the display and then uses Core Graphics to draw a filled red rectangle starting at the bottom of the display at a height defined by the size variable The next step is to implement the touch event methods to set the size value based on the force of the touch eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Implementing the Touch Methods This example project will require that the touchesBegan touchesMoved and touchesEnded methods be implemented The touchesBegan and touchesMoved methods will both call a method named handleTouch passing through to that method the UITouch objects associated with the touch event The touchesEnded method on the other hand will set the size variable to zero and trigger a redraw of the rectangle via a call to the view s setNeedDisplay method Within the MyUIView swift file implement these

    Original URL path: http://www.techotopia.com/index.php/An_iOS_9_3D_Touch_Force_Handling_Tutorial (2016-02-13)
    Open archived version from archive

  • An iOS 9 3D Touch Quick Actions Tutorial - Techotopia
    on the arrow to the left of the Item 0 key so that it is pointing downwards and click on the button to add the first entry into the dictionary Enter UIApplicationShortcutItemIconType into the Key column and UIApplicationShortcutItemTypePlay into the value column as shown in Figure 58 3 Figure 58 3 Repeat this step to add entries for the remaining keys so that the entries in the Info plist file resemble those shown in Figure 58 4 keeping in mind that the UIApplicationShortcutItemUserInfo entry needs to be declared as a dictionary Figure 58 4 To see the entries in XML format Ctrl click on the Info plist file in the Project Navigator panel and select the Open As Source Code menu item The section of the file relating to the quick action should read as follows key UIApplicationShortcutItems key array dict key UIApplicationShortcutItemIconType key string UIApplicationShortcutIconTypePlay string key UIApplicationShortcutItemTitle key string Play string key UIApplicationShortcutItemSubtitle key string Start playback string key UIApplicationShortcutItemType key string PlayMusic string key UIApplicationShortcutItemUserInfo key dict key firstShorcutKey1 key string firstShortcutKeyValue1 string dict dict array eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Switch back to Property Editor mode by Ctrl clicking on the Info plist file once more this time selecting the Open As Property List menu option Adding a Dynamic Quick Action The next step is to add some code to the application delegate class to add a dynamic quick action to the app In code quick actions are represented by the UIApplicationShortcutItem class on which the same properties must be set as those defined for a static quick action Due to the fact that instances of the UIApplicationShortcutItem class are immutable new quick action items are created using the UIMutableApplicationShortcutItem class Once quick action items have been created and configured they are added to the app by placing them in an array and assigning that array to the shortcutItems property of the application context To add dynamic quick actions to the app select the AppDelegate swift file and modify the didFinishLaunchingWithOptions method to create and add the dynamic actions func application application UIApplication didFinishLaunchingWithOptions launchOptions NSObject AnyObject Bool let shortcut2 UIMutableApplicationShortcutItem type SearchMusic localizedTitle Search localizedSubtitle Find a track to play icon UIApplicationShortcutIcon type Search userInfo nil let shortcut3 UIMutableApplicationShortcutItem type AddMusic localizedTitle Add Track localizedSubtitle Add track to playlist icon UIApplicationShortcutIcon type Add userInfo nil application shortcutItems shortcut2 shortcut3 return true Compile and run the app on a 3D Touch capable device and once it is running press the device home button to place the app into the background Locate the icon for the app on the device home screen and perform a deep press on it at which point the quick action menu should appear as shown in Figure 58 5 Figure 58 5 Adding Removing and Changing Dynamic Quick Actions

    Original URL path: http://www.techotopia.com/index.php/An_iOS_9_3D_Touch_Quick_Actions_Tutorial (2016-02-13)
    Open archived version from archive

  • An iOS 9 3D Touch Peek and Pop Tutorial - Techotopia
    has been created it is configured with the web page URL to be loaded into the web view using the currently selected row as the index into the array of web site addresses detailViewController webSite webAddresses indexPath row Next the size of the preview view is configured to have a height of 600 points The width is set to zero to allow the system to choose an appropriate width for the screen size and device orientation detailViewController webSite webAddresses indexPath row detailViewController preferredContentSize CGSize width 0 0 height 600 The sourceRect property of the previewing context object passed to the method is set to the cell frame of the currently selected table row This will cause the cell to remain in focus while the rest of the table view is blurred Finally the detail view controller is returned ready to be displayed to the user previewingContext sourceRect cell frame return detailViewController Assigning the Detail Controller Storyboard ID The code added above to create an instance of the detail view controller does so by referencing the storyboard ID assigned to the AttractionDetailViewController scene in the storyboard file As of yet however the referenced storyboard ID has not been assigned to this view controller This needs to be addressed by selecting the Main storyboard file in the Project Navigator panel and selecting the AttractionDetailViewController scene so that it highlights in blue Once selected display the Identity Inspector panel and enter AttractionDetailViewController into the Storyboard ID field as shown in Figure 59 2 Figure 59 2 Implementing the Pop Delegate Method With the peek view controller delegate method written the method to pop to the full detail view controller now needs to be added to the extension as follows func previewingContext previewingContext UIViewControllerPreviewing commitViewController viewControllerToCommit UIViewController showViewController viewControllerToCommit sender self When this method is called it is passed a reference to the detail view controller that was created in the peek delegate method All that this method needs to do therefore is call the current view controller s showViewController method passing along the detail view controller reference Registering the Previewing Delegate If the app were to be run on a 3D Touch capable iOS device at this point the peek and pop features would not work The reason for this is that although the AttractionTableViewController class has now been declared as implementing the previewing protocol the view controller has not been registered as the previewing delegate Edit the AttractionTableViewController swift file locate the viewDidLoad method and add the code to check for 3D Touch support and to register the delegate override func viewDidLoad super viewDidLoad attractionNames Buckingham Palace The Eiffel Tower The Grand Canyon Windsor Castle Empire State Building webAddresses https en wikipedia org wiki Buckingham Palace https en wikipedia org wiki Eiffel Tower https en wikipedia org wiki Grand Canyon https en wikipedia org wiki Windsor Castle https en wikipedia org wiki Empire State Building attractionImages buckingham palace jpg eiffel tower jpg grand canyon jpg windsor castle jpg empire state building jpg

    Original URL path: http://www.techotopia.com/index.php/An_iOS_9_3D_Touch_Peek_and_Pop_Tutorial (2016-02-13)
    Open archived version from archive

  • Basic iOS 9 Animation using Core Animation - Techotopia
    in both height and width myView transform CGAffineTransformMakeScale 2 2 Similarly the UIView object may be rotated using the CGAffineTransformMakeRotation function which takes as an argument the angle in radians by which the view is to be rotated The following code for example rotates a view by 90 degrees let angle CGFloat 90 M PI 180 myView transform CGAffineTransformMakeRotation angle The key point to keep in mind with transformations is that they become animated effects when performed within an animation block The transformations evolve over the duration of the animation and follow the specified animation curve in terms of timing eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Combining Transformations Two transformations may be combined to create a single transformation effect via a call to the CGAffineTransformConcat function This function takes as arguments the two transformation objects that are to be combined The result may then be assigned to the transform property of the UIView object to be transformed The following code fragment for example both scales and rotates a UIView object named myView let scaleTrans CGAffineTransformMakeScale 2 2 let angle CGFloat 90 M PI 180 let rotateTrans CGAffineTransformMakeRotation angle myView transform CGAffineTransformConcat scaleTrans rotateTrans Affine transformations offer an extremely powerful and flexible mechanism for creating animations and it is just not possible to do justice to these capabilities in a single chapter In order to learn more about affine transformations a good starting place is the Transforms chapter of Apple s Quartz 2D Programming Guide Creating the Animation Example Application The remainder of this chapter is dedicated to the creation of an iOS application intended to demonstrate the use of Core Animation The end result is a simple application on which a blue square appears When the user touches a location on the screen the box moves to that location Through the use of affine transformations the box will rotate 180 degrees as it moves to the new location whilst also changing in size Begin by launching Xcode and creating a new Single View Application project named Animate using Swift as the programming language and with the Universal device option selected Implementing the Variables For the purposes of this application we will need a UIView to represent the blue square and variables to contain the rotation angle and scale factor by which the square will be transformed These need to be declared in the ViewController swift file as follows import UIKit class ViewController UIViewController var scaleFactor CGFloat 2 var angle Double 180 var boxView UIView Drawing in the UIView Having declared the UIView reference we now need to initialize an instance object and draw a blue square located at a specific location on the screen We also need to add boxView as a subview of the application s main view object These tasks only need to be performed once

    Original URL path: http://www.techotopia.com/index.php/Basic_iOS_9_Animation_using_Core_Animation (2016-02-13)
    Open archived version from archive



  •