Pages

Wednesday, August 28, 2013

Animation in Xcode

Here is the global animation code in Objective C

First you need to set the position or size from stating animation 
then Write the below code to animate to its final size or origin

CGRect frameDeleteLabel = lblDeleteAccount.frame;
frameDeleteLabel.origin.y = 304;

[UIView animateWithDuration:1.2 delay:0 options:0 animations:^{
    lblDeleteAccount.frame = frameDeleteLabel;
    deleteAccountButton.frame = frameDeleteButton;
} completion:^(BOOL finished) {
}];

Monday, August 26, 2013

Check iPhone or iPad by Code in Xcode

To Check iPhone or iPad view by Code, we can check by following code


if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    // Code for iPhone View

} else {
    // Code for iPad View
}


Now to Check iPhone5 or iPhone4, we can do a check in iPhone vide code

-(Boolean)checkIphone5{
    UIScreen *screen = [UIScreen mainScreen];
    CGRect fullScreenRect = screen.bounds;
    if (fullScreenRect.size.height==568) {
        return TRUE;
    }
    else{
        return FALSE;
    }
}

and We can use this as like below


if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
   if ([self checkIphone5]) {

      // Code for iPhone5 View
   }
   else
   {
      // Code for iPhone 4 View
   }
else {

    // Code for iPad View
}




Tuesday, August 13, 2013

Debugging in Objective C

I found a better way description of debugging in Xcode in a site
http://www.fiveminutes.eu/debugging-objective-c-code/

So i am just pasting that site matter here, for the reference.


Xcode development environment is integrated with GDB (GNU Debugger), a cross-platform debugger supporting many languages including C/C++, Fortran and Objective C. Xcode provides user interface for tasks as managing breakpoints or inspecting variables. For all advanced tasks there is the GDB command line. Command line provides commands for controlling all aspects of program execution and data manipulation.

Most common commands are:
print - prints the variable or function result value
(gdb) print self.imageView  #show the value of imageView member
(gdb) print -[self description]  #execute [self description] and show the result
NOTE: executing code can change the state of current object or application and lead to unexpected results
po - print object, command introduced in Objective C debugging and used to print out useful information about selected object (that object can be a result of code execution, as for print). _NSPrintForDebugger method will be called on object to retrieve description. All po-able objects must implement this method.
(gdb) po -[NSString stringWithString:@"This is a test."]
This is a test.
call - calls a function or method and stores the returned result.
(gdb) call (NSString*)[NSString stringWithString:@"Another test."]
$1 = (NSString *) 0x5e21120
(gdb) po $1
Another test.
bt - show call stack
cont - continue execution
next - execute next line
step - execute next line with stepping into functions
kill - stop execution
NSLog(NSString* format, …) function is useful for printing out information in the debugger console. NSLog is useful for outputting intermediate results in algorithms or loops where breakpoint usage is not practical. From iOS 4.0 and Mac OS X 10.6 it is possible to use [NSThread callStackSymbols] to get the call stack and monitor the execution flow from code.
Applications sometimes crash because access has been made to an object that has already been freed (and memory possibly reallocated). Best practice to debug such problems is enabling zombie objects by setting the NSZombieEnabled environment variable to YES. When zombies are enabled, all deallocated objects will not be freed, but instead turned into _NSZombie objects. Latter access to a _NSZombie object will trigger a breakpoint and help in tracing the zombie’s origin. No object is freed in this mode and memory consumption can skyrocket and cause application crashing on iPhone or swapping on personal computer. CFZombieLevel environment variable can be used to fine-tune zombie behavior.
Xcode also provides static analysis tool and profiler for catching performance bottlenecks, memory leaks and all other sorts of problems.

Move View while keyboard appears

There are some cases, when you want to type in UITextField and also want to show that in screen. But keyboard hides your UITextField. You can use the following code for this


-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view
    
    CGRect rect = self.completeSingleView.frame;
    if (movedUp)
    {
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.completeSingleView.frame = rect;
    
    [UIView commitAnimations];
}

Now How to Use this

First of all set the <UITextFieldDelegrate> in class ".h" file and set UITextField delegate to self, Like if we have some textfield with name "loginID" then set the delegate to self like below

loginID.delegate = self;

Now use the below code, which call the upper function 

-(void)textFieldDidEndEditing:(UITextField *)sender
{
    if  (self.completeSingleView.frame.origin.y < 0)
        [self setViewMovedUp:NO];
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if  (self.completeSingleView.frame.origin.y >= 0)
        [self setViewMovedUp:YES];
}



Monday, August 12, 2013

Hex Color to UIColor in Xcode

How to use #FF00CC or 0xFF7620 kind of colors in UIColor, So here is the answer

First of all define a function in top, like below:


#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]


Then use this as below


self.logView.backgroundColor = UIColorFromRGB(0xee8029);


Reference Taken From:
http://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string

Saturday, August 3, 2013

Touch Detection inside Polygon Shape xCode

Suppose you have some polygon shape like the above pic and you need to find the touch inside this shape.

For this i have made my own logic from various sources and this code is as below


-(BOOL)pointInPolygon:(int)xPos andY:(int) yPos andPolyArray:(NSArray *)poly
{
    int j = poly.count - 1;
    BOOL oddNodes = false;
    for (int i = 0; i <poly.count; i++) {
        NSValue *val = [poly objectAtIndex:i];
        CGPoint pi = [val CGPointValue];
        
        val = [poly objectAtIndex:j];
        CGPoint pj = [val CGPointValue];
        
        if ((pi.y < yPos && pj.y >= yPos) ||  (pj.y < yPos && pi.y >= yPos)) {
            if (pi.x + (yPos - pi.y) / (pj.y - pi.y) * (pj.x - pi.x) <xPos) {
                oddNodes = !oddNodes;
            }
        }
        j = i;
    }
    return oddNodes;
}

In this function you just need to pass X and Y position of Touch location, with the polygon co-ordinates Array.

Now how to create a Polygon Shape Array?

Here is the solution of this, to define a Polygon Shape points

self.trackArea = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(412,0)],
                  [NSValue valueWithCGPoint:CGPointMake(827,0)],
                  [NSValue valueWithCGPoint:CGPointMake(210,768)],
                  [NSValue valueWithCGPoint:CGPointMake(0,768)],
                  [NSValue valueWithCGPoint:CGPointMake(0,513)],
                  
                  nil];


Now How to call, So here is that

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    if([self pointInPolygon:touchPoint.x andY:touchPoint.y andPolyArray:self.track1Area])
    {
        NSLog(@"Touch Detected for polygon");
    }
}

Add Image from URL in UIView and make clickable

I have made a sample code for this example

Step 1 - Load Image from URL

     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"screen.png"]];

  [self.completeSingleView addSubview:imageView];

Step 2 - Now to make this clickable just use the following code

    imageView.userInteractionEnabled = YES;
   imageView.tag = 2;

Step 3 - Now to add this in any UIView. Follow the code below

     [self.completeSingleView addSubview:imageView];

Step 4- Now to detect the Image in touch began event, follow the code below


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    if([touch.view tag] == 2)
        NSLog(@"Your Image is Touched");
}