Friday, May 31, 2013

Core Animation 1 - Simple Animation by just changing frame size


In this series of tutorials , we are going to study how can achieve various kinds of animations of UIView . Animations play a very important role in better looking applications and increases its quality . They make the apps look ... WOW !!!Also , animations can help make us various kinds of games .


In this tutorial , we are going to make a very simple animation of UIView . In the beginning there are no animating views . Then we click on the    "Show" button , then four views originate from four corners of the iPhone screen and accumulate in the center of the screen and join together .

We have created four animating views as follows . To make views appears as if they are invisible before clicking the "show" button , the height and width of views are 0, 0.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        animatingView1 = [[UIView alloc] init];
        [animatingView1 setBackgroundColor:[UIColor redColor]];
        animatingView1.frame = CGRectMake(0, 0, 50, 50);
        
        
        animatingView2 = [[UIView alloc] init];
        [animatingView2 setBackgroundColor:[UIColor yellowColor]];
        animatingView2.frame = CGRectMake(320, 0, 0, 0);
        
        animatingView3 = [[UIView alloc] init];
        [animatingView3 setBackgroundColor:[UIColor greenColor]];
        animatingView3.frame = CGRectMake(0, 480, 0, 0);
        
        animatingView4 = [[UIView alloc] init];
        [animatingView4 setBackgroundColor:[UIColor blueColor]];
        animatingView4.frame = CGRectMake(320, 480, 0, 0);
        
        [self.view setBackgroundColor:[UIColor lightGrayColor]];
        
    }
    return self;
}
 
In the following function we add the animating views to our viewcontroller's view and also create two button "show" and "hide" as follows . 

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view addSubview:animatingView1];
    [self.view addSubview:animatingView2];
    [self.view addSubview:animatingView3];
    [self.view addSubview:animatingView4];
 
    UIButton *nShowBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [nShowBtn setFrame:CGRectMake(20 , 50 , 100 ,  50)];
    [nShowBtn setTitle:@"show" forState:UIControlStateNormal];
    [nShowBtn addTarget:self action:@selector(normalShowAnimation:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nShowBtn];
    
    UIButton *nHideBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [nHideBtn setFrame:CGRectMake(200 , 50 , 100 ,  50)];
    [nHideBtn setTitle:@"hide" forState:UIControlStateNormal];
    [nHideBtn addTarget:self action:@selector(normalHideAnimation:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nHideBtn];

} 



Following are the selector function for touchUpInside event of two buttons. We used here a class method of UIView  which take the duration of animation and a block where we can put the final state of our animation . In our animation , we are putting the final frame of our views with height and width of 50,50 and location at the centre of the screen . This makes views appear as if we they are originating from the four corners of the screen .


-(void)normalShowAnimation:(id)sender
{
    [UIView animateWithDuration:0.6 animations:^(void){
        animatingView1.frame = CGRectMake(110, 180, 50, 50);
        animatingView2.frame = CGRectMake(160, 180, 50, 50);
}

-(void)normalHideAnimation:(id)sender
{
    [UIView animateWithDuration:0.6 animations:^(void){
        animatingView1.frame = CGRectMake(0, 0, 0, 0);
        animatingView2.frame = CGRectMake(320, 0, 0, 0);
}

Here are the final output of our animation .
                                 

Thursday, May 30, 2013

Iron Man 3 : TV spots


Iron Man 3 - TV Spot 1



Iron Man 3 - TV Spot 2



Iron Man 3 - TV Spot 3



Iron Man 3 - TV Spot 4



Iron Man 3 - TV Spot 5



Iron Man 3 - TV Spot 6



Iron Man 3 - TV Spot  7

Store input strings in an array of strings

Yes , it is possible to store the input strings from the user to store it into an array of strings . We know , an array of 5 strings s can be created and an input string can be stored in a string str as follows
 
   char *s[5];
   gets(str); 
Using the above two methods , we made a program to input strings in an array of strings .

// program to store input strings in an array of strings 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *strings[10];
    int i = 0;
    
    while(i < 10)
    {
        strings[i++] = (char *)malloc(100);
        gets(strings[i-1]);
        printf("\nstrings[%d] = %s",i-1,strings[i-1]);
    }
    
    return 0;
    
}


INPUT :

hello
Code
how
are you
good
na
me 
too
haaaaaa
Heeeee
 
OUTPUT : strings[0] = hello strings[1] = Code strings[2] = how strings[3] = are you strings[4] = good strings[5] = na strings[6] = me strings[7] = too strings[8] = haaaaaa strings[9] = Heeeee

Tuesday, May 28, 2013

Remove Extra white spaces from the given string


Suppose you are given a string with more than one space between two words. You are supposed to remove these extra spaces .
For doing this , i have implemented two methods .

In the first method , there is flag called x , that denotes whether a space is already encountered or not . If yes , x is set to 1 or else it is set to 0

In the second method , I copied each element from the input string to the output string except for the condition that when two consecutive spaces occurs . If two consecutive spaces occur , it just iterates forward , until a non-space character is found .The process repeats for all the characters in the input string .


// program to trim two or more blanks in the given string

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *trimBlank1(char *str)
{
    int i = 0 ,x = 0 , j =0;
    char *newString = (char *)malloc(sizeof(strlen(str)));

    while(str[i] != '\0')
    {
        if(str[i] == ' ' && x == 0)
            x = 1 ;
        else if(str[i] == ' ' && x == 1)
        {
            i++;
            continue;
        }
        else if (str[i] != ' ')
                x = 0 ;

        newString[j++] = str[i++] ;
    }
    newString[j] = '\0';
    printf("\nMethod 1 : %s",newString);
    return newString ;
}

void trimBlank2(char *is , char *os)
{
    int i = 0 , j = 0;

    while(i < strlen(is))
    {
        os[j++] = is[i++];

        while(is[i] == ' ' && is[i+1]==' ')
            i++;
    }

    os[j]='\0';
    printf("\nMethod 2 : %s",os);
}


int main()
{
    char input[100] , output[100];

    printf("\nEnter the input string:");

    gets(input);
    printf("\nBefore trimming:%s",input);
    printf("\nAfter trimming : %s",trimBlank1(input));

    printf("\n\nBefore Trimming: %s",input);
    trimBlank2(input,output);
    printf("\nAfter Trimming : %s",output);
    printf("\n\n");

    return 0;
}

Find the substring of a given input string

Given a string . If you are given a specified position and the number of characters , then find a substring from that specified position and having the given number of characters in it from the input string given.

Following is the code .

// program to find a substring of a given string
//if specified position is given and number of characters is 0 , then the entire string is printed from the specified posiition

#include <stdio.h>
#include <string.h>

char *substring(char *str,int i , int n)
{

    char *substr ;
    int k ;

    if(n==0)
        n = strlen(str) - i ;

    substr = (char *)malloc(n+1);

    while(n>0)
    {
        substr[k++] = str[i-1];
        i++;
        n--;
    }
    substr[k] = '\0';
    printf("\n%s",substr);

    return substr;
}

int main()
{
    char input[100] ;
    int i , j ; 
    printf("\nEnter the input String : ");
gets(input); printf("\nEnter the position : "); scanf("%d",&i); printf("\nEnter the number of characters : "); scanf("%d",&j); printf("\n%s",substring(input,i,j)); }

Iron Man 3 - 2013 Official Trailers

Iron Man 3 - 2013 Official trailer - 1  

Iron Man 3 - 2013 Official trailer - 2

Man of Steel TV Spots

Man of Steel  -  TV Spot - 1
 

Man of Steel  -  TV Spot - 2
 

Man of Steel  -  TV Spot - 3


 Man of Steel  -  TV Spot - 4
 

Man of Steel  -  TV Spot - 5
 

Man of Steel  -  TV Spot - 6
 

Man of Steel  -  TV Spot - 7

Man of Steel Official Trailers HD

 First official trailer of Man of steel

 

Second Official trailer of Man of Steel
   

Third Official trailer of Man of Steel
   

Man of Steel trailer - "Fate of your planet"
   

Man of Steel TV spot (Special Edition)
 

Monday, May 27, 2013

convert a numerical string into a number

here is an example to convert a string of numbers into a number it represents

// a program to convert given string into integer ex: "124" to 124

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    char str[10] ;
    int i,l ,n , num =  0;

    printf("Enter the number to be converted :");
    scanf("%s",str);

    l = strlen(str);

    for(i = 0 ; i < l ; i++)
    {
           n = str[i]-48;
           num = num +n*pow(10,l-i-1);
    }

    printf("num : %d",num);

}

Lower Case to Upper Case and Vice-Versa

Two methods to lower case letters of a given string into upper case letters

1) First Method - using ASCII code

// convert all lower case letters to upper case letter and vice-versa

#include <stdio.h>
#include <string.h>

int main()
{
    char str[100] = "Hi CodeJack ! How are you ?";
    int i = 0 ;

    for(i = 0 ; i< strlen(str);i++)
    {
            if(str[i] >= 97 && str[i] <= 122)
            {
                str[i] = str[i] - 32 ;
            }
            else if (str[i] >= 65 && str[i] <= 90)
            {
                str[i] = str[i] + 32 ;
            }
    }

    printf("%s",str);
}
 
 
2) Second Method - without using ASCII code
 
 
#include <stdio.h>
#include <string.h>

int main()
{
    char str[100] = "Hi CodeJack ! How are you ?";
    int i = 0 ;

    for(i = 0 ; i< strlen(str);i++)
    {
            if(str[i] >= 'a' && str[i] <= 'z')
            {
                str[i] = str[i] - ('a' - 'A') ;
            }
            else if (str[i] >= 'A' && str[i] <= 'Z')
            {
                str[i] = str[i] + ('a' - 'A') ;
            }
    }

    printf("%s",str);
}
 
Here is the ASCII table
 
 
 

discard the unread new line character from input buffer


Today while going through a simple program to understand the behavior of scanf and gets , I was stuck in a very interesting concept usually occurring  with scanf .

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str1[10],str2[10],str3[10],str4[10],str5[50],c;
    scanf("%s%s%s%s",str1,str2,str3,str4);
    printf("str1 : '%s'\n",str1);
    printf("str2 : '%s'\n",str2);
    printf("str3 : '%s'\n",str3);
    printf("str4 : '%s'\n",str4);
    
    gets(str5);
    printf("str5 : '%s'",str5);
    return 0;
}
--------------------------------------------------------------------------------------------- 
Input :

    Alice in wonder land

   

Ouptut:

    str1 : 'Alice'

    str2 : 'in'

    str3 : 'wonder'

    str4 : 'land'

    str5 : ''
 
 
We see that program does not wait for us for giving input for str5 . 



Reason :

This is because when a user presses Enter , two characters are added to the input buffer

1)Enter key (ASCII code 13)

2)newLine character(\n) (ASCII code 10)



At the gets statement , the input buffer contains still contains "\n" , new line character . 
As a result , str5 contains only the newLine character and goes the printf statement , 
printing the undesired value of str5.
To avoid this thing , we can do one of the following things 



1)Use of "%*c" 
 
scanf("%s%s%s%s%*c",str1,str2,str3,str4);
Adding "%*c" consumes the next character in the input buffer . As a result , the "\n" is consumed, 
and hence no longer remains in the input buffer . This is one of the ways we can avoid the trailing "\n" 
from the input buffer .



2)Use of getchar() , getc() 

After the four printf() statements we can add  getchar() or getc() . These function gets the next unread 
character from the input buffer . Hence , the newLine character does not remain in the input buffer and we 
can  input the string for str5 without any difficulty.