Saturday, 17 September 2016

Blure Effect

Adding blur effect on the view's corner

User interface play an important role in development of a good application. With the useful and important features, it is necessity of a application to make a attractive and good looking user interface. A well design interface force user to play with application and enjoy the application for a time being.

A small small effect can make the interface better like presenting sub-view  with animation or bounce, adding blur effect in the corner of sub-views.
Here i am going to add blur effect in the corner of sub views.
We can achieve blur effect by using gradient. We are having in-build CAGraientLayer in iOS, so its not a tough  to add blur effect in both corner of a view, because of this CAGraientLayer.
Click here for details of CAGraientLayer.

First create a project and add a UIView as a subview of view in the storyboard or xib file.
Than go to .h file and create a outlet for added UIView.

@property (nonatomic, weak) IBOutlet UIView *horizontalLineViewFirst;

again go to storyboard  or xib file and connect the outlet to added UIView.

Now switch to .m file and add a method

#pragma mark - Add Gradient
-(void) addGrandientlayerToView:(UIView *)gradientView{
    //create a gradient layer
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    //set gradient color
    CGColorRef outerColor = [[UIColor colorWithWhite:0.97 alpha:1.0] CGColor];
    CGColorRef innerColor = [[UIColor colorWithWhite:0.72 alpha:0.0] CGColor];

    //set name of the layer
    gradientLayer.name = @"BlurGradientLayer";
    //set color to gradien layer
    gradientLayer.colors = [NSArray arrayWithObjects:(__bridge id)outerColor,(__bridge id)innerColor,(__bridge id)innerColor,(__bridge id)outerColor, nil];
    //set location to each color
    gradientLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.200],[NSNumber numberWithFloat:0.710],[NSNumber numberWithFloat:1.0], nil];
    //set start point for draw a gradient layer
    [gradientLayer setStartPoint:CGPointMake(0, 0.6)];
    //set start point for draw a gradient layer
    [gradientLayer setEndPoint:CGPointMake(1, 0.6)];
    //set bound of the layer
    gradientLayer.bounds = gradientView.bounds;
    //set anchor point of layer to {0,0}
    gradientLayer.anchorPoint = CGPointZero;
    [gradientView.layer addSublayer:gradientLayer];

}


pass the message to above method form viewDidLoad method

[self addGrandientlayerToView:self.horizontalLineViewFirst];

Run the  app,  we can see a blur effect on added subview.





 
We can see a effect similar to above image a horizontal line with blur corners.

Now rotate the simulator or device,  one side of blur effect get misplace,its come in the middle not in both corners. Lets fix the issue

add another method to reset gradient location after orientation

#pragma mark -
#pragma mark Handle Orientation

- (void) handleOrientationChange:(NSNotification *)notification{
    [self resetAllGradientLayer];
}
-(void) resetAllGradientLayer{
    [self resetGradientforView:self.horizontalLineViewFirst];
}
-(void) resetGradientforView:(UIView *)gradientView{
    for (CAGradientLayer *layer in gradientView.layer.sublayers){
        if ([layer.name isEqualToString:@"BlurGradientLayer"]) {
            layer.bounds = gradientView.bounds;
        }
    }
}

 
Add orientation change notification to controller

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

 
Now again run the application and check.
Click here for complete project.

Friday, 2 September 2016

Sqlite Database in ios



Sqlite is an in-process library that implement a self-contained,  serverless,  zero-configuration, transactional SQL database engine. Sqlite is very small and light weight, less than 400kiB fully configured or less than 250 KiB with optional features omitted and its not required a separated server process or system to operate.
Click here for more details.

We can use Sqlite for storage data in iOS Application as a database.
This post is related to get data from two different-different database's tables.

First step is to two get both database path.

So create a method which return db path.

-(NSString *) getDbpathWithName:(NSString *)dbName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
  
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:dbName];
}
     

NSString *firstDbPath = [self getDbpathWithName:@"Firstdb.Sqlite"];

NSString *secondDbPath = [self getDbpathWithName:@"Seconddb.Sqlite"];

//declare a Sqlite database instance
sqlite3 *secondDataBase;

//Open second database
if (sqlite3_open([secondDbPath UTF8String], &secondDataBase)!=SQLITE_OK) {
        NSLog(@"Error to open database = %s",sqlite3_errmsg(userDatabase));
        sqlite3_close(userDatabase);
       
secondDataBase = nil;
        return ;
    }
//write a query for attach first database to second database
 NSString *attachQuery = [NSString stringWithFormat:@"ATTACH DATABASE \"%@\" AS aDB",firstDbPath];

//execute query
    sqlite3_exec(
secondDataBase, [attachQuery UTF8String], NULL, NULL, &error);
    if (error) {
        NSLog(@"Error to Attach = %s",error);
        sqlite3_close(userDatabase);
       
secondDataBase = nil;
    }
Now we have attached both database, and can use to get data from both database's tables.

//Write a query to retrieve data
NSString *queryString = @"Select e.id,  e.name,  e.address, ex.car_allowance. ex.medical_allowance, ex.food_allowance from employee e inner join expenses ex on (e.id == ex.empId)".

Here table 'employee' is in first database and table 'expenses' is in second database, because we need to fetch data from two different table so we are using 'join'.

//Now Prepare statement
sqlite3_stmt *selectStmt;
    if (sqlite3_prepare_v2(
secondDataBase, queryString.UTF8String, -1, &selectStmt, NULL) != SQLITE_OK) {
        NSLog(@"Error to prepare annotation select stmt all comment= %s",sqlite3_errmsg(
queryString));
        sqlite3_finalize(selectStmt);
        sqlite3_close(
secondDataBase);
        selectStmt = nil;
       
secondDataBase = nil;
        return;
    }
Its time to fetch all records 
NSMutableArray *empRecordArray = [[NSMutableArray alloc] init];
   
 while (sqlite3_step(selectStmt)==SQLITE_ROW) {
        NSString *empid = [NSString stringWithFormat:@"%s",(const  char*)sqlite3_column_text(selectStmt, 0)];
        NSString *name = [NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(selectStmt, 1)];
       NSString *address = [NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(selectStmt,2)]; 
       NSString *car_allowance = [NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(selectStmt, 3)]; 
       NSString *medical_allowance = [NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(selectStmt, 4)]; 
       NSString *food_allowance = [NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(selectStmt, 5)];
      
     NSDictionary *recordDict = @{@"id":empid, @"name":name, @"address":address, @"CarAllowance":car_allowance,@"MedicalAllowance":@"medical_allowance", @"FoodAllowance":food_aalowance};

[empRecordArray addObject:recordDict];
}//end while loop

NSLog(@"All record : %@", empRecordArray);


Check out more post related to