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 Overview of iOS 4 iPhone Multitouch, Taps and Gestures - Techotopia
    response chain defined by the object s nextResponder property for further processing and so on up the chain If the first responder is not able to handle the event it will also pass it to the next responder in the chain and so on until it either reaches a responder that handles the event or it reaches the end of the chain the UIApplication object where it will either be handled or discarded Take for example a UIView with a UIButton subview If the user touches the screen over the button then the button as first responder will receive the event If the button is unable to handle the event it will need to be passed up to the view object If the view is also unable to handle the event it would then be passed to the view controller and so on When working with the responder chain it is important to note that the passing of an event from one responder to the next responder in the chain does not happen automatically If an event needs to be passed to the next responder code must be written to make it happen Forwarding an Event to the Next Responder An event may be passed on to the next responder in the response chain by calling the nextResponder method of the current responder passing through the method that was triggered by the event and the event itself Take for example a situation where the current responder object is unable to handle a touchesBegan event In order to pass this to the next responder the touchesBegan method of the current responder will need to make a call as follows void touchesBegan NSSet touches withEvent UIEvent event self nextResponder touchesBegan touches withEvent event Gestures Gesture is an umbrella term used to encapsulate any single interaction between the touch screen and the user between starting at the point that the screen is touched by one or more fingers and the time that the last finger leaves the surface of the screen Swipes pinches stretches and flicks are all forms of gesture Taps A tap as the name suggests occurs when the user touches the screen with a single finger and then immediately lifts it from the screen Taps can be single taps or multiple taps and the event will contain information about the number of times a user tapped on the screen Touches A touch occurs when a finger establishes contact with the screen When more than one finger touches the screen each figure registers as a touch up to a maximum of five fingers Touch Notification Methods Touch screen events cause one of four methods on the first responder object to be called The method that gets called for a specific event will depend on the nature of the interaction In order to handle events therefore it is important to ensure that the appropriate methods from those outlined below are implemented within your responder chain These methods will be used in the

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


  • An Example iOS 4 iPhone Touch, Multitouch and Tap Application - Techotopia
    from within the view controller code which in turn requires that three outlets be declared Select the touchViewController h file from the main Xcode project window and modify it as follows import UIKit UIKit h interface touchViewController UIViewController UILabel touchStatus UILabel methodStatus UILabel tapStatus property nonatomic retain IBOutlet UILabel touchStatus property nonatomic retain IBOutlet UILabel methodStatus property nonatomic retain IBOutlet UILabel tapStatus end Save the file by selecting File Save or pressing Command S Having declared the outlets in the interface file edit the touchViewController m implementation file and add the corresponding synthesize directive import touchViewController h implementation touchViewController synthesize touchStatus methodStatus tapStatus end Having declared the outlets it is time to design the user interface Designing the user Interface Launch the Interface Builder tool by double clicking on the touchViewController xib file Within Interface Builder modify the user interface by adding label components and modifying properties until the view appears as follows When adding the labels to the right of the view be sure to stretch them so that they reach to the right hand edge of the view area Ctrl click on the File s Owner icon and drag the resulting blue line to the label adjacent to the Method label and select the methodStatus outlet from the resulting menu Repeat these steps to establish connections between the touchStatus and tapStatus outlets and the corresponding label objects Enabling Multitouch on the View By default views are configured to respond to only single touches in other words a single finger touching or tapping the screen at any one time For the purposes of this example we plan to detect multiple touches In order to enable this support it is necessary to change an attribute of the view object To achieve this click on the background of the View window display the Attribute Inspector window Tools Attribute Inspector or Command 1 and make sure that the Multiple Touch option is selection in the Interaction section at the bottom of the window google IOSBOX google Save the design and exit from Interface Builder 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 main Xcode project window select the touchViewController m file and add the touchesBegan methods as follows void touchesBegan NSSet touches withEvent UIEvent event NSUInteger touchCount touches count NSUInteger tapCount touches anyObject tapCount methodStatus text touchesBegan touchStatus text NSString stringWithFormat d touches touchCount tapStatus text NSString stringWithFormat d taps tapCount 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

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

  • Detecting iOS 4 iPhone Touch Screen Gesture Motions - Techotopia
    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 This will require the creation of two outlets that will later be connected to labels within the user interface of the application In addition we need to add an instance variable to our view controller in which to store the start position of the touch To create these outlets and the instance variable select the touchMotionViewController h file and modify it as follows import UIKit UIKit h interface touchMotionViewController UIViewController UILabel xCoord UILabel yCoord CGPoint startPoint property retain nonatomic IBOutlet UILabel xCoord property retain nonatomic IBOutlet UILabel yCoord property CGPoint startPoint end Next edit the touchMotionViewController m file and add the synthesize directive import touchMotionViewController h implementation touchMotionViewController synthesize xCoord yCoord startPoint end Once the code changes have been implemented be sure to save both files before proceeding to the user interface design phase Designing the Application User Interface Double click on the touchMotionViewController xib file to load the NIB file into Interface Builder Within Interface Builder create a user interface such that it resembles the layout in the following figure Be sure to stretch the labels so that they both extend to cover most of the width of the view Once the label objects have been added to the view establish a connection between the xCoord outlet and the top label by Crtl clicking on the File s Owner and dragging the blue line to the top label Release the pointer and select xCoord from the resulting menu Repeat this step for the yCoord outlet and save the design before exiting from Interface Builder Implementing the touchesBegan Method When the user first touches the screen the location coordinates need to be saved in the startPoint instance variable and the coordinates reported to the user This can be achieved implementing the touchesBegan method in the touchMotionViewController m file as follows void touchesBegan NSSet touches withEvent UIEvent event UITouch theTouch touches anyObject startPoint theTouch locationInView self view CGFloat x startPoint x CGFloat y startPoint y xCoord text NSString stringWithFormat x f x yCoord text NSString stringWithFormat y f 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 void touchesMoved NSSet touches withEvent UIEvent event UITouch theTouch touches anyObject CGPoint touchLocation theTouch locationInView self view CGFloat x touchLocation x CGFloat y touchLocation y xCoord text NSString stringWithFormat x f x yCoord text NSString stringWithFormat y f 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

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

  • Drawing iOS 4 iPhone 2D Graphics with Quartz - Techotopia
    device screen with 0 0 representing the top left hand corner of the display These coordinates are stored in the form of CGFloat variables An additional C structure named CGPoint is used to contain both the x and y coordinates to specify a point on the display Similarly the CGSize structure stores two CGFloat values designating the width and height of an element on the screen Further the position and dimension of a rectangle can be defined using the CGRect structure which contains a CGPoint the location and CGSize the dimension of a rectangular area Of key importance when working with points and dimensions is that these values do not correspond directly to screen pixels In other words there is not a one to one correlation between pixels and points Instead the underlying framework decides based on a scale factor where a point should appear and at what size relative to the resolution of the display on which the drawing is taking place This enables the same code to work on both higher and lower resolution screens for example an iPhone 3GS screen and an iPhone 4 retina display without the programmer having to worry about it For more precise drawing requirements iOS version 4 and later allows the scale factor for the current screen to be obtained from UIScreen UIView UIImage and CALayer classes allowing the correlation between pixels and points to be calculated for greater drawing precision For iPhone OS 3 or older the scale factor is always returned as 1 0 The Graphics Context Almost without exception all Quartz API function calls require that the graphics context be passed as an argument Each view has its own context which is responsible for performing the requested drawing tasks and subsequently rendering those drawings onto the corresponding view The graphics context can be obtained with a call to the UIGraphicsGetCurrentContext function which returns a result of type CGContextRef CGContextRef graphics context UIGraphicsGetCurrentContext Working with Colors in Quartz 2D The Core Graphics CGColorRef data type is used to store colors when drawing with Quartz This data type holds information about the colorspace of the color RGBA CMYK or gray scale together with a set of component values that specify both the color and the transparency of that color For example the color red with no transparency would be defined with the RGBA components 1 0 0 0 0 0 1 0 A colorspace can be created via a Quartz API function call For example to create an RGB colorspace CGColorSpaceRef colorspace CGColorSpaceCreateDeviceRGB If the function fails to create a colorspace it will return a NULL value In the case of a successful creation the colorspace must be released when no longer required via a call to the CGColorSpaceRelease function CGColorSpaceRelease colorspace Gray scale and CMYK color spaces may similarly be created using the CGColorSpaceCreateDeviceGray and CGColorSpaceCreateDeviceCMYK functions respectively Once the colorspace has been created the next task is to define the components The following declaration defines a set of RGBA

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

  • An iOS 4 iPhone Graphics Drawing Tutorial using Quartz 2D - Techotopia
    CGColorRef color CGColorCreate colorspace components CGContextSetStrokeColorWithColor context color CGContextMoveToPoint context 0 0 CGContextAddLineToPoint context 300 400 CGContextStrokePath context CGColorSpaceRelease colorspace CGColorRelease color When compiled and run the application should display as illustrated in the following figure Note that in the above example we manually created the colorspace and color reference As described in Drawing iOS 4 iPhone 2D Graphics with Quartz colors can also be created using the UIColor class For example the same result as outlined above can be achieved with fewer lines of code as follows void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 2 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGContextMoveToPoint context 0 0 CGContextAddLineToPoint context 300 400 CGContextStrokePath context Drawing Paths As you may have noticed in the above example we draw a single line by essentially defining the path between two points Defining a path that comprises multiple points allows us to draw using a sequence of straight lines all connected to each other using repeated calls to the CGContextAddLineToPoint function Non straight lines may also be added to a shape using calls to for example the CGContextAddArc function The following code for example draws a diamond shape void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 2 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGContextMoveToPoint context 100 100 CGContextAddLineToPoint context 150 150 CGContextAddLineToPoint context 100 200 CGContextAddLineToPoint context 50 150 CGContextAddLineToPoint context 100 100 CGContextStrokePath context When executed the above code should produce output that appears as follows Drawing a Rectangle Rectangles are drawn in much the same way as any other path is drawn with the exception that the path is defined by specifying the x and y co ordinates of the top left hand corner of the rectangle together with the rectangle s height and width These dimensions are stored in a CGRect structure and passed through as an argument to the CGContextAddRect function void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 2 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGRect rectangle CGRectMake 60 170 200 80 CGContextAddRect context rectangle CGContextStrokePath context The above code will result in the following display when compiled and executed Drawing an Ellipse or Circle Circles and ellipses are drawn by defining the rectangular area into which the shape must fit and then calling the CGContextAddEllipseInRect function void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 2 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGRect rectangle CGRectMake 60 170 200 80 CGContextAddEllipseInRect context rectangle CGContextStrokePath context When compiled the above code will produce the following graphics In order to draw a circle simply define a rectangle with equal length sides a square in other words Filling a Path with a Color A path may be filled with a color using a variety of Quartz 2D API functions Rectangular and elliptical paths may be filled using the CGContextFillRect and CGContextFillEllipse functions respectively Similarly a path may be filled using the CGContextFillPath function Prior to executing a fill operation the fill color must be specified using the CGContextSetFillColorWithColor function The

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

  • Integrating iAds into an iOS 4 iPhone App - Techotopia
    that may unfold as a result of a user interacting with that ad Fortunately this can easily be achieved by implementing a number of delegate methods defined in the ADBannerViewDelegate protocol as outlined later in this chapter Creating an Example iAds iPhone Application In the remainder of this chapter we will work step by step through the creation of a simple iOS iPhone application that includes an iAd banner view advertisement Begin this tutorial by launching Xcode and creating a new iOS iPhone application project named iAdApp using the View based Application template The ultimate goal of this example is to create an application consisting of a table view and an iAd banner view The banner will be configured to only appear when an ad is successfully downloaded from the iAds server and will change format to match the orientation of the device Adding the iAds Framework to the Xcode Project Once the new project has been created the first step is to make sure the iAds framework is included in the project Failure to add this framework will result in compilation and linking errors when building the application To add the iAds framework Ctrl click on the Frameworks entry in the Groups Files panel and select the Add Existing Frameworks menu option In the resulting panel scroll down to iAd framework select it and click on the Add button The iAd Framework will now appear in the frameworks list along with the other frameworks already included in the project Configuring the View Controller The next step is to declare an instance of the ADBannerView class and provide an outlet to the table view object that we will be placing in our user interface file in a later step These steps are performed in the iAdAppViewController h interface file The necessary code is outlined below note also the requirement to import the iAd iAd h header file import UIKit UIKit h import iAd iAd h interface iAdAppViewController UIViewController ADBannerViewDelegate UITableView tableView ADBannerView bannerView property nonatomic retain IBOutlet UITableView tableView end In the above code we have also added a line that reads ADBannerViewDelegate This indicates to compiler that within the view controller we will be implementing the delegate methods for the banner view protocol Once the above changes have been made save the file before proceeding Designing the User Interface The next step is to design the user interface and connect the table view object to the tableView outlet declared in the view controller interface file above Double click on the iAdAppViewController xib file to launch Interface Builder and drag and drop a UITableView object into the View window as illustrated in the following figure Establish a connection between the table view object and the tableView outlet in the view controller by holding down the Ctrl key clicking on the File s Owner entry in the document window and dragging the resulting blue line to the table view in the View window Release the line and select the tableView outlet

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

  • An Overview of iOS 4 iPhone Multitasking - Techotopia
    launched application is entering foreground mode the applicationDidBecomeActive method is then called If on the other hand the application is moving directly to the background state either by design or by necessity then the applicationDidEnterBackground delegate method is triggered When a foreground application enters the background the application transitions to the inactive state triggering a call to the application delegate s applicationWillResignActive method This in turn is followed by a transition to background status which is accompanied by a call to the applicationDidEnterBackground method If the application has not indicated that is eligible to continue running in the background the application is given 5 seconds to complete any tasks before returning from the applicationDidEnterBackground method If these tasks cannot be performed in the time given they may be performed by making a call to the beginBackgroundTaskWithExpirationHandler method followed by a call to the endBackgroundTask method when the task is complete Failure to return from the applicationDidEnterBackground method within the allocated will result in the application being terminated When an application is moved from the background to the foreground the applicationWillEnterForeground method is triggered followed by a call to applicationDidBecomeActive Finally when an application is about to be terminated either by the user or the system the application delegate s applicationWillTerminate method is called Disabling Multitasking for an iOS Application Multitasking support is the default for all applications developed using the iOS 4 0 SDK or later In situations where an application is required to exit rather than enter background mode a configuration setting is required in the applications Info plist file To achieve this load the project associated with the app into Xcode and select the Info plist file from the file list In the resulting editor pane click on the button to add an addition entry and select the Application does not run in background key from the drop down menu Once selected set the check box located in the Value column before saving the file and rebuilding the application Checking for Multitasking Support Multitasking is only supported on the iPhone 3GS and later devices running iOS 4 0 or newer In the case of devices where multitasking is not supported applications are simply terminated rather than being placed in the background If you are developing an application that relies on multitasking it is recommended that code be implemented to check for multitasking support so that application behavior can be modified to compensate for the missing functionality This can be achieved using the following code fragment bool multitaskingAvailable UIDevice device UIDevice currentDevice backgroundIsSupported NO if device respondsToSelector selector isMultitaskingSupported backgroundISSupported device multitaskingSupported return backgroundIsSupported When the above method is called it returns either YES or NO depending on whether or not multitasking is supported on the device Supported Forms of Background Execution So far we have looked primarily at the types of applications for which a suspended background state is acceptable to the user Apple however recognizes three categories in which application suspension would be detrimental to the user

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

  • Scheduling iOS 4 iPhone Local Notifications - Techotopia
    In the case of this example a template method can be found in the localNotifyAppDeleage m file Within the main Xcode project window select this file and scroll down until the method comes into view void applicationDidEnterBackground UIApplication application Use this method to release shared resources save user data invalidate timers and store enough application state information to restore your application to its current state in case it is terminated later If your application supports background execution called instead of applicationWillTerminate when the user quits Adding a Sound File to the Project google IOSBOX google When a local notification is triggered the option is available to play a sound to gain the user s attention If such an audio alert is required the corresponding sound file must be added to the application project resources and must be in Linear PCM MA4 IMA ADPCM uLaw or aLaw format If no sound file is specified the default is for the notification to be silent though the iPhone device will still vibrate For the purposes of this exercise we will copy an audio file from the Mac OS X system on which Xcode is running and use it when scheduling the notification To locate this file open the Finder tool navigate to the Macintosh HD System Library Sounds directory and locate a file named Glass aiff AIFF being PCM format Once located drag this file and drop it onto the Resources category located in the Groups Files panel in the main Xcode project window and click the Add button in the resulting panel The audio file should now be included in the list of resources for the project ready to be accessed by the application code Scheduling the Local Notification Local notifications require the use of the UILocalNotification class combined with an NSDate object configured with the date and time of the notification Properties may also be set to specify the text to be displayed to the user an optional repeat interval and a message to be displayed to the user in the alert box With these requirements in the mind the following code creates an NSDate object based on the current date and time plus 10 seconds This date object is then used to schedule a notification with no repeats a text message and the sound from the Glass aiff file void applicationDidEnterBackground UIApplication application NSDate alertTime NSDate date dateByAddingTimeInterval 10 UIApplication app UIApplication sharedApplication UILocalNotification notifyAlarm UILocalNotification alloc init autorelease if notifyAlarm notifyAlarm fireDate alertTime notifyAlarm timeZone NSTimeZone defaultTimeZone notifyAlarm repeatInterval 0 notifyAlarm soundName Glass aiff notifyAlarm alertBody Staff meeting in 30 minutes app scheduleLocalNotification notifyAlarm Testing the Application To test the application save the changes to the localNotifyAppDeleage m file and click on the build and run tool bar button located in the Xcode project window After compiling and linking the application it will load and run in the iOS Simulator Once the application has loaded into the iPhone simulator click on the device home button to place the app

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



  •