Saturday, 15 February 2014

How to change the background color of UIView with delay/animation: Xcode/cocos2d

We can change the background color of an UIView with delay as follows. This can be also used for many other UIElement animations such as changing the properties of an UIButton (such as textColor) with delay/animation.


 [UIView animateWithDuration:2.0 animations:^{
    [myView setBackgroundColor:[UIColor grayColor]];
 }
    

How to set UIImageView image with delay/animation: Xcode/Cocos2d

We can set the image of an UIImageView with delay/animation as follows:


[UIView transitionWithView:myImageView
                      duration:1.0
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        myImageView.image = [UIImage imageNamed:@"newImage.png"];
                    } completion:nil];

    

Thursday, 9 January 2014

How to focus on last cell in a UITableview in Xcode



//Create a method to find out the last index of your table view and will focus to that cell with an animation:

-(void)autoFocusTableCell{
    NSIndexPath *lastIndexPath = [self lastIndexPath];
    [myTableView scrollToRowAtIndexPath:lastIndexPath
                       atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}


//Code to find the last index of your tableview.
-(NSIndexPath *)lastIndexPath{
    NSInteger lastSectionIndex = MAX(0, [myTableView numberOfSections] - 1);
    NSInteger lastRowIndex = MAX(0, [myTableView numberOfRowsInSection:lastSectionIndex] - 1);
    return [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
}

//Then call the method
    [self autoFocusTableCell];
    

Tuesday, 24 December 2013

How to sort an array with respect to another

You can sort an array with respect to another easily by adding them to an NSDictionary and do as follows:
            
    // Put the two arrays into a dictionary as keys and values
    NSDictionary *dictionary = [NSDictionary 
                               dictionaryWithObjects:secondArray forKeys:firstArray];
    // Sort the first array
    NSArray *sortedFirstArray = [[dictionary allKeys]  
                               sortedArrayUsingSelector:@selector(compare:)];
    // Sort the second array based on the sorted first array

    NSArray *sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray 
                                             notFoundMarker:[NSNull null]];

How to set an image at the left corner of an UIButton

Let button Frame is:

  myButton.frame = CGRectMake(008030);

            
To set an image at the left corner of an UIButton, do as follows:

  [myButton setImageEdgeInsets:UIEdgeInsetsMake(030080)];

Sunday, 24 November 2013

How to shuffle an NSMutableArray in an easy way

There are several methods to sort an NSMutableArray. Here we will give you a very simple method to shuffle your NSMutableArray. This method uses the default exchangeObjectAtIndex method to shuffle the array.

Lets initialise a mutable array and add some elements to it as below:

 // Initialising the NSMutableArray
   NSMutableArray *myArray = [[NSMutableArray alloc]init];

 // Add some objects to it (say 15, here) 
   for (int i=1; i<=15; i++) {
     [myArray addObject:[NSString stringWithFormat:@"%d",i]];
   }

Now you have an array with 15 objects/elements in it. Now for sorting the above array, you just need to use the following code:

 // Code for shuffling
  for (int i = 0; i < [myArray count]; ++i) {
    [myArray exchangeObjectAtIndex:i withObjectAtIndex:((random() % ([myArray count]-i)) + i)];
  }


Wednesday, 20 November 2013

How to set the master volume and individual channel volume in corona SDK




This tutorial will help you to change/adjust the volume of the music playing in your corona SDK application.

  • To set the master volume, you can simply call the following line:

  audio.setVolume( 0.5 )

This will setup the master volume to half. The value range from 0.0 (min.) to 1.0 (max.).


  • And, if you need to set the volume of an individual music channel, then you can use the following:
  audio.setVolume( 0.75, { channel=1 } ) 
This will control the volume of channel 1 (here, it will make the channel 1 volume to 3/4th of the default/full volume).