Wednesday, 27 February 2013

UIComboBox For iPhone.

I have made custom file by using which you can use UICombo box in ios code.Let the code begins.

UICombo.h 
// Created by Prince Kumar Sharma on 27/02/13.
// Copyright (c) 2013 Prince Kumar Sharma All rights reserved.
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
typedef enum {
CBRed,
CBBlue,
CBGray,
CBLightGray,
CBYellow,
CBMagenta,
CBBrown,
CBPurple,
CBBlack
} CBColor;
@class UIViewController;
@interface UICombo : UIView<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIScrollViewDelegate>
{
@private
int numberOfObjects;
UILabel *arrow;
NSArray *items;
UITableView *_tableView;
UITextField *_textField;
}
- (id)initWithFrame:(CGRect)frame andItems:(NSArray*)itemArray andColor:(CBColor)color;
-(void)addItem:(NSString*)item;
-(void)insertItem:(NSString*)item AtIndex:(int)index;
-(int)removeItem;
-(void)removeItemAtIndex:(NSUInteger)index;
-(void)refresh;
@end
view raw UICombo.h hosted with ❤ by GitHub

UICombo.m
// Created by Prince Kumar Sharma on 27/02/13.
// Copyright (c) 2013 Prince Kumar Sharma All rights reserved.
#import "UICombo.h"
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
@implementation UICombo
- (id)initWithFrame:(CGRect)frame andItems:(NSArray*)itemArray andColor:(CBColor)color
{
if ((self = [super initWithFrame:frame]))
{
self->items=[[NSArray alloc]initWithArray:itemArray];
self->numberOfObjects=[self->items count];
[self setBackgroundColor:[UIColor lightGrayColor]];
[self setAutoresizesSubviews:YES];
_textField=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];
[_textField.layer setCornerRadius:5.0];
[_textField setBorderStyle:UITextBorderStyleBezel];
if (self->items.count!=0) {
[_textField setText:[self->items objectAtIndex:0]];
}
[_textField setFont:[UIFont systemFontOfSize:15.0f]];
[_textField setDelegate:self];
[_textField addTarget:self action:@selector(open) forControlEvents:UIControlEventTouchDown];
arrow=[[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-20,5,20,20)];
[arrow setText:@">"];
[arrow setTextAlignment:NSTextAlignmentCenter];
[arrow setFont:[UIFont boldSystemFontOfSize:14.0f]];
[arrow setBackgroundColor:[UIColor clearColor]];
arrow.layer.anchorPoint=CGPointMake(0.5,0.5);
[arrow setTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90))];
[_textField addSubview:arrow];
switch (color) {
case CBRed:
[_textField setTextColor:[UIColor redColor]];
break;
case CBBlue:
[_textField setTextColor:[UIColor blueColor]];
break;
case CBGray:
[_textField setTextColor:[UIColor grayColor]];
break;
case CBLightGray:
[_textField setTextColor:[UIColor lightGrayColor]];
break;
case CBYellow:
[_textField setTextColor:[UIColor yellowColor]];
break;
case CBMagenta:
[_textField setTextColor:[UIColor magentaColor]];
break;
case CBBrown:
[_textField setTextColor:[UIColor brownColor]];
break;
case CBPurple:
[_textField setTextColor:[UIColor purpleColor]];
break;
case CBBlack:
default:
[_textField setTextColor:[UIColor blackColor]];
break;
}
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = _textField.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor lightGrayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[_textField.layer insertSublayer:gradient atIndex:0];
_tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,_textField.frame.size.height, self.frame.size.width,0)];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[_tableView setBackgroundColor:[UIColor clearColor]];
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[_tableView setRowHeight:25];
[_tableView setDelegate:self];
[_tableView setDataSource:self];
[_tableView.layer setMasksToBounds:YES];
[_tableView.layer setCornerRadius:2.0];
[_tableView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[_tableView.layer setBorderWidth:1.5f];
[_tableView.layer setShadowColor:[UIColor blackColor].CGColor];
[_tableView.layer setShadowOpacity:0.8];
[_tableView.layer setShadowRadius:3.0];
[_tableView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
[self addSubview:_textField];
[self addSubview:_tableView];
[_tableView reloadData];
self.clipsToBounds=YES;
}
return self;
}
-(void)addItem:(NSString*)item
{
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
[tempArray addObject:item];
self->items=[NSArray arrayWithArray:tempArray];
self->numberOfObjects=[self->items count];
}
-(void)insertItem:(NSString*)item AtIndex:(int)index
{
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
[tempArray insertObject:item atIndex:index];
self->items=[NSArray arrayWithArray:tempArray];
self->numberOfObjects=[self->items count];
}
-(int)removeItem
{
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
[tempArray removeLastObject];
self->items=[NSArray arrayWithArray:tempArray];
self->numberOfObjects=[self->items count];
return self->numberOfObjects;
}
-(void)removeItemAtIndex:(NSUInteger)index
{
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
[tempArray removeObjectAtIndex:index];
self->items=[NSArray arrayWithArray:tempArray];
self->numberOfObjects=[self->items count];
}
-(void)refresh
{
[_tableView reloadData];
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[_tableView setNeedsDisplay];
[_textField setNeedsDisplay];
}
#pragma mark- Combo Animation
-(void)open
{
__block CGRect rect=self.frame;
rect.size.height=150;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[self setFrame:rect];
[arrow setTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270))];
} completion:^(BOOL finished){
[self setNeedsDisplay];
[_tableView reloadData];
}];
}
-(void)close
{
__block CGRect rect=self.frame;
rect.size.height=_textField.frame.size.height;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[self setFrame:rect];
[arrow setTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-270))];
} completion:^(BOOL finished) {
[self setNeedsDisplay];
}];
}
#pragma mark- UITableView datasource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self->items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.font=[UIFont systemFontOfSize:14.0f];
cell.textLabel.text=[self->items objectAtIndex:indexPath.row];
CGRect cellFrame=cell.frame;
cellFrame.size.height=25;
[cell setFrame:cellFrame];
UIView *bgView=[[UIView alloc] initWithFrame:cell.frame];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = bgView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor lightGrayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[bgView.layer insertSublayer:gradient atIndex:0];
[cell setBackgroundView:bgView];
return cell;
}
#pragma mark- UITableView delegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_textField.text=[self->items objectAtIndex:indexPath.row];
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
[self close];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self bringSubviewToFront:_textField];
[self setNeedsDisplay];
}
#pragma mark- UITextField delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
- (void)dealloc
{
_tableView.delegate=nil;
_tableView.dataSource=nil;
[_tableView removeFromSuperview];
_tableView=nil;
[arrow removeFromSuperview];
arrow=nil;
[_textField removeFromSuperview];
_textField=nil;
}
@end
view raw UICombo.m hosted with ❤ by GitHub

Using Code:-

To use these files,you need to create instance of UICombo Class and pass items and textColor as parameter as given below:

NSArray *itemArray=[[NSArray alloc] initWithObjects:@"BMW",@"Ferrari",@"Swift Desire",nil];

UICombo  *combo=[[UICombo alloc] initWithFrame:CGRectMake(10, 30, 200, 30) andItems:itemArray andColor:CBBrown];

and At Last add it as subview to your view on which you want to display it.

[self.view addSubview:combo];

These combo includes methods to addItem , InsertItemAtIndex,removeItem,removeItemAtIndex,
refresh combo etc.

Note:- Include QuartzCore framework.


                                               
You can have a sample code IOSCombo

Tuesday, 26 February 2013

Stock Ticker

Stock Ticker is simply a file that will allow to pass views and number of views to get display on your tickerView.

Ticker you have always see on pull down shutter in your iphone.
Here I have passed stock Views and Currency views.
You can pass your views to display.This is a continuous object display.No space will come then ticker ends.It will continue with first object of the ticker Views array.

 

Using:- To use it,import StockTicker.h file in your code window.Made an array of UIViews or UILabels whatever you want to display as objects on your ticker.

 

  Make TickerView instance as

StockTiker *stkticker=[[StockTiker alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
view raw one.m hosted with ❤ by GitHub
  Set its properties as

stkticker.tdelegate=self;
stkticker.ttag=0;
[stkticker setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar addSubview:stkticker];
view raw two.m hosted with ❤ by GitHub

  Implement its delegate method as:


#pragma mark- UITickerView delegate method
- (NSInteger)numberOfRowsintickerView:(StockTiker *)tickerView
{
return [objectArray count];
}
- (id)tickerView:(StockTiker*)tickerView cellForRowAtIndex:(int)index
{
return [objectArray objectAtIndex:index];
}
@end
view raw three.m hosted with ❤ by GitHub
Stock Tikcer.h 


// Created by Prince Kumar Sharma on 20/02/13.
// Copyright (c) 2012 Prince Kumar Sharma. All rights reserved.
#import <UIKit/UIKit.h>
@class StockTiker;
@protocol UIStockTickerDelegate<NSObject>
@required
- (NSInteger)numberOfRowsintickerView:(StockTiker *)tickerView;
- (id)tickerView:(StockTiker*)tickerView cellForRowAtIndex:(int)index;
@end
@interface StockTiker : UIScrollView<UITableViewDataSource>
{
@private
int count;
int numberOfObjects;
id<UIStockTickerDelegate> tdelegate;
}
@property (assign) id<UIStockTickerDelegate>tdelegate;
@property(assign)NSUInteger ttag;
-(void)start;
@end
view raw StockTiker.h hosted with ❤ by GitHub


 Stock Ticker.m  


// Created by Prince Kumar Sharma on 20/02/13.
// Copyright (c) 2012 Prince Kumar Sharma. All rights reserved.
#import "StockTiker.h"
@implementation StockTiker
@synthesize tdelegate=_tdelegate;
@synthesize ttag=_ttag;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
-(void)start
{
count=0;
numberOfObjects=[_tdelegate numberOfRowsintickerView:self];
[NSTimer scheduledTimerWithTimeInterval:0.002 target:self selector:@selector(moveObjects) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(checkPosition) userInfo:nil repeats:YES];
}
-(void)addElement:(UIView*)subView
{
if (![self.subviews containsObject:(id)subView]) {
[subView setFrame:CGRectMake(self.frame.size.width, 0, subView.frame.size.width, subView.frame.size.height)];
[self addSubview:subView];
}
}
-(void)checkPosition
{
UIView *view=[self.tdelegate tickerView:self cellForRowAtIndex:count];
CGRect rect=[view frame];
float x=rect.origin.x+rect.size.width;
if (x<300) {
count=count+1;
if (count==numberOfObjects) {
count=0;
}
}
UIView *subView=[self.tdelegate tickerView:self cellForRowAtIndex:count];
[self addElement:subView];
if ((rect.origin.x+rect.size.width)<0) {
[view removeFromSuperview];
}
}
-(void)moveObjects
{
CGRect rect;
for (UIView *view in self.subviews) {
rect=[view frame];
rect.origin.x=rect.origin.x-0.1;
[view setFrame:rect];
[view setNeedsDisplayInRect:rect];
}
}
@end
view raw StockTiker.m hosted with ❤ by GitHub


You can have a sample code StockTicker