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 5 iPhone Touch, Multitouch and Tap Application - Techotopia
    methodStatus UILabel tapStatus property strong nonatomic IBOutlet UILabel touchStatus property strong nonatomic IBOutlet UILabel methodStatus property strong nonatomic IBOutlet UILabel tapStatus end 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 Load the view into the Interface Builder tool by selecting the touchViewController xib file Within the Interface Builder panel modify the user interface by adding label components from the Object library View Utilities Show Object Library and modifying properties until the view appears as outlined in Figure 36 1 When adding the labels to the right of the view be sure to stretch them so that they reach to the right hand margin 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 Figure 36 1 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 View Utilities Show Attribute Inspector and make sure that the Multiple Touch option is selection in the Interaction section at the bottom of the window Figure 36 2 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 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 the information on the touchStatus and tapStatus labels Implementing the touchesMoved Method When the user moves one or more fingers currently in contact with the surface of the iPhone touch screen the touchesMoved method is called repeatedly until the movement ceases In order to

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


  • Detecting iOS 5 iPhone Touch Screen Gesture Motions - Techotopia
    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 strong nonatomic IBOutlet UILabel xCoord property strong 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 Designing the Application User Interface Select the touchMotionViewController xib file to load the NIB file into Interface Builder Within the Interface Builder panel create a user interface such that it resembles the layout in Figure 37 1 Figure 37 1 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 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 by 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 view controller such that it displays the start and end points of the gesture void touchesEnded NSSet touches withEvent UIEvent event UITouch theTouch touches anyObject CGPoint endPoint theTouch locationInView

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

  • Identifying iPhone Gestures using iOS 5 Gesture Recognizers - Techotopia
    rotation gesture essentially two fingers in contact with the screen located opposite each other and moving in a circular motion UILongPressGestureRecognizer Used to identify when the user touches the screen with one or 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 5 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 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 UITapGestureRecognizer doubleTap UITapGestureRecognizer alloc initWithTarget self action selector tapDetected doubleTap numberOfTapsRequired 2 self view addGestureRecognizer doubleTap A template method for the action method for this and other gesture recognizers is as follows IBAction tapDetected UIGestureRecognizer sender Code to respond to gesture here Recognizing Pinch Gestures Pinch gestures are detected using the UIPinchGestureRecognizer class For example UIPinchGestureRecognizer pinchRecognizer UIPinchGestureRecognizer alloc initWithTarget self action selector pinchDetected self view addGestureRecognizer pinchRecognizer Detecting Rotation Gestures Rotation gestures are recognized by the UIRotationGestureRecognizer the sample code for which is as follows UIRotationGestureRecognizer rotationRecognizer UIRotationGestureRecognizer alloc initWithTarget self action selector

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

  • An iPhone iOS 5 Gesture Recognition Tutorial - Techotopia
    viewDidUnload Release any retained subviews of the main view e g self myOutlet nil self statusLabel nil end Designing the User Interface Select the recognizerViewController xib file to display the interface builder panel In addition to the UIView created for us by Xcode when the project was created the only user interface object that needs to be added is the status label Display the Object library View Utilities Show Object Library and drag a Label object from the library to the center of the view Once positioned stretch the label to the outer edges of the view until the blue dotted lines representing the recommended margins appear and then modify the label properties to center the label text Ctrl click on the File s Owner object drag the blue line to the label object and release From the resulting menu select the statusLabel outlet Configuring the Gesture Recognizers The application code will need to configure gesture recognizers for taps swipes rotations and pinches Since these recognizers will need to be attached to the view object the ideal place to create them is in the viewDidLoad method of our recognizerViewController class so select the recognizerViewController m file and modify this method so that it reads as follows void viewDidLoad super viewDidLoad UITapGestureRecognizer doubleTap UITapGestureRecognizer alloc initWithTarget self action selector tapDetected doubleTap numberOfTapsRequired 2 self view addGestureRecognizer doubleTap UIPinchGestureRecognizer pinchRecognizer UIPinchGestureRecognizer alloc initWithTarget self action selector pinchDetected self view addGestureRecognizer pinchRecognizer UIRotationGestureRecognizer rotationRecognizer UIRotationGestureRecognizer alloc initWithTarget self action selector rotationDetected self view addGestureRecognizer rotationRecognizer UISwipeGestureRecognizer swipeRecognizer UISwipeGestureRecognizer alloc initWithTarget self action selector swipeDetected swipeRecognizer direction UISwipeGestureRecognizerDirectionRight self view addGestureRecognizer swipeRecognizer UILongPressGestureRecognizer longPressRecognizer UILongPressGestureRecognizer alloc initWithTarget self action selector longPressDetected longPressRecognizer minimumPressDuration 3 longPressRecognizer numberOfTouchesRequired 1 self view addGestureRecognizer longPressRecognizer Adding the Action Methods Having configured the gesture recognizers the next step is

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

  • Drawing iOS 5 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 iOS 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 CGColorSpace CreateDeviceGray 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_5_iPhone_2D_Graphics_with_Quartz (2016-02-13)
    Open archived version from archive

  • An iOS 5 iPhone Graphics Drawing Tutorial using Quartz 2D - Techotopia
    void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 4 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 Figure 41 5 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 CGContextSet FillColorWithColor function The following example defines a path and then fills it with the color red void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextMoveToPoint context 100 100 CGContextAddLineToPoint context 150 150 CGContextAddLineToPoint context 100 200 CGContextAddLineToPoint context 50 150 CGContextAddLineToPoint context 100 100 CGContextSetFillColorWithColor context UIColor redColor CGColor CGContextFillPath context The above code produces the following graphics on the device or simulator display when executed Figure 41 6 The following code draws a rectangle with a blue border and then once again fills the rectangular space with red void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 4 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGRect rectangle CGRectMake 60 170 200 80 CGContextAddRect context rectangle CGContextStrokePath context CGContextSetFillColorWithColor context UIColor redColor CGColor CGContextFillRect context rectangle When added to the example application the resulting display should appear as follows Image iphone ios 5 draw filled border jpg A filled rectangle with a border drawn on an iPhone Figure 41 7 Drawing an Arc An arc may be drawn by specifying two tangent points and a radius using the CGContextAddArcToPoint function for example pre void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 4 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGContextMoveToPoint context 100 100 CGContextAddArcToPoint context 100 200 300 200 100 CGContextStrokePath context The above code will result in the following graphics output Figure 41 8 Drawing a Cubic Bézier Curve A cubic Bézier curve may be drawn by moving to a start point and then passing two control points and an end point through to the CGContextAddCurveToPoint function void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 4 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGContextMoveToPoint context 10 10 CGContextAddCurveToPoint context 0 50 300 250 300 400 CGContextStrokePath context The above code will cause the curve illustrated in Figure 41 9 to be drawn when compiled and executed in our example application Figure 41 9 Drawing a Quadratic Bézier Curve A quadratic Bézier curve is drawn using the CGContextAddQuadCurveToPoint function providing a control and end point as arguments having first moved to the start point void drawRect CGRect rect CGContextRef context UIGraphicsGetCurrentContext CGContextSetLineWidth context 4 0 CGContextSetStrokeColorWithColor context UIColor blueColor CGColor CGContextMoveToPoint context 10 200 CGContextAddQuadCurveToPoint context 150 10 300 200 CGContextStrokePath context The above code when executed will display a curve

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

  • Basic iOS 5 iPhone Animation using Core Animation - Techotopia
    the transform property of the UIView object For example to change the scale of a UIView object named myView by a factor of 2 in both height and width myView transform CGAffineTransformMakeScale 2 2 Similarly the UIView object may be rotated using the CGAffineTransformMakeRotation 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 myView transform CGAffineTransformMakeRotation 90 M PI 180 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 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 CGAffineTransform scaleTrans CGAffineTransformMakeScale 2 2 CGAffineTransform rotateTrans CGAffineTransformMakeRotation angle M PI 180 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 iPhone 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 with both product and class prefix named animate Implementing the Interface File 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 animateViewController h file as follows import UIKit UIKit h interface animateViewController UIViewController UIView boxView float scaleFactor float angle property strong nonatomic UIView boxView end 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 initialize our scaleFactor and angle variables and add boxView as a subview of the application s main view object These tasks only need to be performed once when the application first starts up so a good option is to override loadView method in the animateViewController m file

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

  • Integrating iAds into an iOS 5 iPhone App - Techotopia
    step through the creation of a simple iOS 5 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 Single View 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 select the iAdApp target located at the top of the project navigator panel In the center pane select the Build Phases tab and unfold the Link Binary With Libraries panel Click on the button to display a list of existing frameworks locate and select iAd Framework and click 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 strong nonatomic IBOutlet UITableView tableView end In the above code we have also added a line that reads ADBannerViewDelegate This indicates to the compiler that within the view controller we will be implementing the delegate methods for the banner view protocol 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 Select the iAdAppViewController xib file display the Object library View Utilities Show Object Library and drag and drop a UITableView object into the View window as illustrated in Figure 43 1 Figure 43 1 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 object and dragging the resulting blue line to the table view in the design panel Release the line and select the tableView outlet from the resulting menu Creating the Banner Ad For the purposes of this example we will create an instance of the ADBannerView in the viewDidLoad method of the view controller Select the iAdAppViewController m implementation file locate the method and modify it as follows note also the

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



  •