Posts

Showing posts with the label iOS

iOS: How to use a delay call to a method or execute a time or with intervals

In iOS or and its underlying Framework extends it to use Grand Central Dispatch (GCD) . So in this example, let say you wanted to call or execute a method at a specified time, you can use the code below, Say you wanted to play an audio file after 2 seconds.             NSURL *audioURL  =   [ [ NSBundle  mainBundle ]  URLForResource : @"TheAudioFile" withExtension : @ "mp3" ] ;              NSError   * error;              AVAudioPlayer * audioPlayer  =   [ [ AVAudioPlayer alloc ]  initWithContentsOfURL : audioURL error :& error ] ;                         dispatch_time_t popTime  =  dispatch_time ( DISPATCH_TIME_NOW, 2.0  * NSEC_PER_SEC ) ; // where 2.0 as a second * nano seconds             dispatch_after ( popTime, di...

iOS: Adding icon in the button and a center text title

Image
In my end, I did this using UIEdgeInsetsMake which the left corner is calculated to make it to the center. I am not sure if there's something really you can make the text at aligned at the center but this works for me. Make sure that you set your left corner to your desired position by calculating the width. e.g. UIEdgeInsetsMake(0.0f, 42.0f, 0.0f, 0.0f) UIButton  * scanBarCodeButton  =   [ UIButton buttonWithType : UIButtonTypeRoundedRect ] ; scanBarCodeButton.frame  =  CGRectMake ( center, 10.0f, fieldWidth, 40.0f ) ; [ scanBarCodeButton setImage : [ UIImage imageNamed : @ "BarCodeIcon.png" ]  forState : UIControlStateNormal ] ; [ scanBarCodeButton setTitle : @ "Scan the Barcode"  forState : UIControlStateNormal ] ; scanBarCodeButton.titleEdgeInsets  =  UIEdgeInsetsMake ( 0.0f, 42.0f, 0.0f, 0.0f ) ; [ scanBarCodeButton setContentHorizontalAlignment : UIControlContentHorizontalAlignmentLeft ] ; [ scanBarCodeButton addTarget :...

iOS: Animating a View (Sample of hiding a UITabBarController view)

The UIView class has it's animation methods like beginAnimations, commitAnimations, animateWithDuration, and setAnimationDuration. You can use this methods to animate such specific view. Below is an example of hiding a UITabBarController view and retain the non-UITabBar class views.           [ UIView beginAnimations : nil  context : NULL ] ;      [ UIView setAnimationDuration : 0.5 ] ;          //    CGRect temp = appDelegate.tabBarController.view.frame;      //    temp.origin.y += 100;          for ( UIView  * view  in   appDelegate.tabBarController.view.subviews )      {          if ( [ view isKindOfClass : [ UITabBar class ] ] )          {              [ view setFrame : CGRectMake ( - 320 , view.frame.origin....

iOS: Adding rounded corners in a layer

Image
    CALayer *layer = roundedFrameView. layer ;     layer. cornerRadius = 8.0f ;     layer. borderColor = [[ UIColor colorWithRed : 0.89 green : 0.89 blue : 0.98 alpha : 1 ] CGColor ];     layer. borderWidth = 0.8 ; The RGB color above will render a light gray color which is ideal for a rounded frame. Check out my screen shot below. You notice the frame where it covers the string "The Item is list as "On Sale"..." is being framed.

iOS: Adding a drop shadow in an image

You can just modify the x,y and width and height values inside the CGRectMake to make adjustments for your object's relative coordinates and you can also adjust as well the radius, opacity and offset of the shadow. Make sure your self.contentView is the view where your image is shown, and take note that your image must be added after the shadow, not before the shadow so it'll be behind the image. By adding an image, just do after the code above somewhat like, self.imageView =  [[ UIImageView alloc ] initWithImage :[ UIImage   imageNamed : @"CellImageTest.png" ]]; [ self . contentView addSubview : self . imageView ];

iOS: Forcing orientation in your view controller (iOS 6 and iOS < 6)

Got this trouble on forcing the app to be in a a landscape mode. Let say, you have a standard orientation of a Portrait but at first force the view controller to be in landscape where the button is located in the left side. So I have this code that you can try, play, and modify. See below, // //  MainViewController.h @interface MainViewController : UIViewController <UIWebViewDelegate> @end // MainViewController.m #import "MainViewController.h" @implementation UINavigationController ( autorotation ) // >= iOS 6 - ( BOOL ) shouldAutorotate {         UIInterfaceOrientation interfaceOrientation = [ UIApplication sharedApplication ] .statusBarOrientation;     NSLog ( @ "intefaceorientation: %d" , interfaceOrientation ) ;     [ self.topViewController shouldAutorotate ] ;     return YES ;     } // >= iOS 6 - ( NSU...