Thursday, September 20, 2012

Moving Images/Objects around the screen using Touch function in iOS app

Hi Guyz, in this section i will give you the idea of moving your objects(Image in our case) in the screen using Touch function. In previous blog, we talked about the drag and drop method, where we were dragging some object from one position and dropping it to  another one but in this section we are only going to reposition/move the object in the screen.So basically in this section, we will see how the multiple images moved using touch function. Lets get started !!!!

Create multiple (say 4 in our case) UIImageView in your viewcontroller and connect it to the IBOutlet and assign them with different images.
In your .h file ;
  @property(nonatomic,retain) IBOutlet UIImageView *imageView1;
  @property(nonatomic,retain) IBOutlet UIImageView *imageView2
  @property(nonatomic,retain) IBOutlet UIImageView *imageView3;  
  @property(nonatomic,retain) IBOutlet UIImageView *imageView4; 
In your .m file,
  @synthesize imageView1,imageView2,imageView3,imageView4;
Now,use Touch delegate method as :
  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
     if ([touch view] ==
imageView1) { // If touched view is imageView1 , then assign it its new location
               
imageView1.center = touchLocation;
    }
       else if ([touch view] ==
imageView2) {
          
imageView2.center = touchLocation;
    }
    else if ([touch view] ==
imageView3) {
              
imageView3.center = touchLocation;
    }
    else if ([touch view] ==
imageView4) {
              
imageView4.center = touchLocation;
    }
}

 So that's it!! so simple to move objects around the screen in iOS apps.


Tuesday, September 18, 2012

Draging and Droping objects in iOS App

Hi Guyz, in this section i will  describe you how to drag any particular views/objects and dropping it somewhere else. This is the nice feature  mostly seen in web apps, but here i will be detailing how to use this features in iOS Apps. In this demo, we will be changing the background color of UIView (where we will be dropping objects) depending on the dragged object's color and i will also give you a hint to change the position(repositioning it) of dragged object's as a result of Drag and Drop.
Lets get started !!!!

First design your  ViewController.xib file  as in figure with four dragging objects(here,UIImageView) and one UIView (as dropping target view). Change the background color of all 4 UIImageViews.
Now in .h file,
   // connect target UIView in Interface Builder
@property (nonatomic, strong) IBOutlet UIView *dropTargetView;
     // define temp UIImageView for view being dragged
@property (nonatomic, strong)  UIImageView *dragObjectView; 
    //distance between the actual touch point and the upper left corner of the dragObject
@property (nonatomic, assign) CGPoint touchOffset;
  // drag object's original position  in screen
@property (nonatomic, assign) CGPoint homePosition;

Monday, September 17, 2012

Creating PDF file from simple Texts/NSStrings/UIImages in iOS App

Hi Guyz, In this section i will write you a bunch of custom methods that can be used for generating PDF files from your simple texts and NSStrings in iOS app. In this section, we will be using core Graphics functions of Objective-C, No frameworks are used. You can have your own design customization  beside what i have designed here. I hope this blog will help you to start generating PDF as per your requirements. Lets start coding!!!

Lets define some variables and constants in .h file:
    #define kBorderInset            20.0
    #define kBorderWidth            1.0
    #define kMarginInset            12.0
    #define kLineWidth              1.0
    CGSize pageSize;

-(IBAction)generatePdfButtonPressed:(id)sender; //UIButton method for generating PDF
Now, in .m file : (lets have some custom methods)
- (void) drawBorder{
    /***get the current context, select a color for the border, specify a rect for the border, which has an inset of 20 pixels, and then just stroke the rect using normal Core Graphics.***/
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    UIColor *borderColor = [UIColor brownColor];
    CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2);
    CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor);
    CGContextSetLineWidth(currentContext, kBorderWidth);
    CGContextStrokeRect(currentContext, rectFrame);
}

Thursday, September 13, 2012

Bridging between JavaScript & Objective-C for Hybrid iOS Apps

Hi Guyz, in this section i will write about the bridging between the native Objective-C code with the web portion(JavaScript/HTML) code. The iOS hybrid application delivers a native experience on iOS devices by wrapping the mobile Web storefront with a native shell. The native shell elements are coded using the iOS Software Development Kit (SDK), while the storefront is accessed using the mobile Web interface. That generally means  hybrid app is a combination of web app + native iOS features. We use UIWebView for loading the webapp (linking URL) and we can have our own iOS feature included with it. So, In this section, i will be describing about how to call native Objective-C method from particular button in web portion and vice versa.

Call Javascript function from Objective-C

  // In your Javascript files:
    function myJavascriptFunction () {
        // Do whatever your want!
        }
       // -----------------------------------
       // And in your Objective-C code:
    // Call Javascript function from Objective-C:

    [webViewObject stringByEvaluatingJavaScriptFromString:@"myJavascriptFunction()"];

Call Objective-C function from Javascript

// In Objective-C
    - viewDidLoad {
        webView = [[UIWebView alloc] init];
        // Register the UIWebViewDelegate in order to shouldStartLoadWithRequest to be called (next function)
        webView.delegate = self; 
    }

Thursday, September 6, 2012

Creating Custom UIProgressView

Hi Guyz. In this section i will be creating a custom UIProgressView Class using DrawRect: method and two images (for background, and progress filling image). The images and screenshot of this section is here attached with. In the similar way you can have your own images to fit as per your requirement.
Standard iOS UIProgressView : 
Custom UIProgressView : 



Now, Lets create/add a New Objective -C Class in your project. Now, in your header file make it a subclass of UIProgressView.
@interface CustomProgressView : NSObject  --> @interface CustomProgressView : UIProgressView
your .h file : //  CustomProgressView.h
#import <UIKit/UIKit.h>
@interface CustomProgressView : UIProgressView
@end

Monday, September 3, 2012

Taking ScreenShot programmatically in Objective-C

Hi Guyz, in this section i will  show you a custom programmatic method for taking screenshot of any Views within your iOS application and storing it to Photo Library of your device. The custom method i am writing here simply returns UIImage of screen you are capturing, hence use this UIImage as per your requirements.
-(UIImage *) takeAScreenShot {
// here i am taking screen shot of whole UIWindow, but you can have the screenshot of any individual UIViews, Tableviews  . so in that case just use  object of your UIViews instead of  keyWindow.
       UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
     if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) // checking for Retina display   

         UIGraphicsBeginImageContextWithOptions(keyWindow.bounds.size, YES, [UIScreen mainScreen].scale);
       //if this method is not used for Retina device, image will be blurr.

}
    else
    {  UIGraphicsBeginImageContext(keyWindow.bounds.size);

}
    [
keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

   // now storing captured image in Photo Library of device
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

/*** if you want to save captured image locally in your app's document directory
       NSData * data = UIImagePNGRepresentation(image);

NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"testImage.png"];
    NSLog(@"Path for Image : %@",imagePath);
    [data writeToFile:imagePath atomically:YES];

*******************************/
     return image;
 }

Note : Include #import <QuartzCore/QuartzCore.h>  for using CALayer property.