Search This Blog

Sep 14, 2010

UIPickerView Delegate Methods with Animations

Code for UIPickerView Delegate Methods. In this example PickerView will comes from Bottom of the iPad Screen with animation.

Code for .h file.

#import


@interface UICreateTeamController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{

    IBOutlet UIButton *btnBack;
    IBOutlet UIButton *btnSave;
    IBOutlet UIButton *btnLogo;
    UIImagePickerController *mainPicker;
   
    NSMutableArray *arrDivision;
    NSMutableArray *arrGroupLevel;
    NSMutableArray *arrSkillSet;
    IBOutlet UILabel *lblDivision;
    IBOutlet UILabel *lblGroupLevel;
    IBOutlet UILabel *lblSkillSet;
   
    IBOutlet UIView *viewPicker;
    IBOutlet UIPickerView *pickerMain;
    IBOutlet UIButton *btnDone;
    IBOutlet UIButton *btnDivision;
    IBOutlet UIButton *btnGroupLevel;
    IBOutlet UIButton *btnSkillSet;
}
@property (nonatomic, assign) UIButton *btnLogo;

-(IBAction)btnSave_TouchUpInside:(id)sender;
-(IBAction)btnBack_TouchUpIndise:(id)sender;
-(IBAction)btnLogo_TouchUpInside:(id)sender;
-(IBAction)btnDone_TouchUpInside:(id)sender;
-(IBAction)btnDivision_TouchUpInside:(id)sender;
-(IBAction)btnGroupLevel_TouchUpInside:(id)sender;
-(IBAction)btnSkillSet_TouchUpInside:(id)sender;
- (void)setView:(UIView*)view MovedUp:(BOOL)movedUp;
- (void)setViewMovedUp:(BOOL)movedUp;

@end



Code for .m file.

#import "UICreateTeamController.h"

@implementation UICreateTeamController
@synthesize btnLogo;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViewMovedUp:NO];
    [self setView:viewPicker MovedUp:NO];
    arrDivision = [[NSMutableArray alloc]initWithObjects:@"East Division",@"North Division",@"West Division",@"South Division",nil];
    arrGroupLevel = [[NSMutableArray alloc]initWithObjects:@"Under 14",@"Under 16",@"Under 19",nil];
    arrSkillSet = [[NSMutableArray alloc]initWithObjects:@"Defensive",@"Aggerasive",nil];
}

-(IBAction)btnSave_TouchUpInside:(id)sender{

    [self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)btnBack_TouchUpIndise:(id)sender{

    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)btnLogo_TouchUpInside:(id)sender{
  
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];  
}

- (void)setView:(UIView*)view MovedUp:(BOOL)movedUp{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    // Make changes to the view's frame inside the animation block. They will be animated instead
    // of taking place immediately.
    CGRect rect = view.frame;
    if (movedUp)
    {
        // If moving up, not only decrease the origin but increase the height so the view
        rect.origin.y = (self.view.frame.size.height + self.view.frame.origin.y) - view.frame.size.height;
    }
    else
    {
        // If moving down, not only increase the origin decrease the height.
        rect.origin.y = (self.view.frame.size.height + self.view.frame.origin.y) + view.frame.size.height;
    }
    view.frame = rect;
  
    [UIView commitAnimations];
}

- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    // Make changes to the view's frame inside the animation block. They will be animated instead
    // of taking place immediately.
    CGRect rect = self.view.frame;
    if (movedUp){
        // If moving up, not only decrease the origin but increase the height so the view
        // covers the entire screen behind the keyboard.
        //rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        //rect.size.height += kOFFSET_FOR_KEYBOARD;
        rect.origin.y = self.view.frame.origin.y - 216;
    }
    else
    {
        // If moving down, not only increase the origin decrease the height.
        //rect.origin.y += kOFFSET_FOR_KEYBOARD;
        //rect.size.height -= kOFFSET_FOR_KEYBOARD;
        if(rect.origin.y <0)
            rect.origin.y = self.view.frame.origin.y + 216;
    }
  
    self.view.frame = rect;
    [UIView commitAnimations];
}

-(IBAction)btnDone_TouchUpInside:(id)sender{

    [self setViewMovedUp:NO];
    [self setView:viewPicker MovedUp:NO];
}

-(IBAction)btnDivision_TouchUpInside:(id)sender{

    [self setViewMovedUp:NO];
    [pickerMain selectRow:0 inComponent:0 animated:YES];
    [pickerMain reloadComponent:0];
    [self setView:viewPicker MovedUp:YES];
  
}
-(IBAction)btnGroupLevel_TouchUpInside:(id)sender{

    [self setViewMovedUp:NO];
    [pickerMain selectRow:0 inComponent:0 animated:YES];
    [pickerMain reloadComponent:0];
    [self setView:viewPicker MovedUp:YES];
}
-(IBAction)btnSkillSet_TouchUpInside:(id)sender{

    [self setViewMovedUp:NO];
    [pickerMain selectRow:0 inComponent:0 animated:YES];
    [pickerMain reloadComponent:0];
    [self setView:viewPicker MovedUp:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  
    [btnLogo setBackgroundImage: [info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
    [picker dismissModalViewControllerAnimated:YES];  
    [picker release];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    lblDivision.text = [arrDivision objectAtIndex:[pickerView selectedRowInComponent:0]];
    lblGroupLevel.text = [arrGroupLevel    objectAtIndex:[pickerView selectedRowInComponent:1]];
    lblSkillSet.text = [arrSkillSet objectAtIndex:[pickerView selectedRowInComponent:2]];
}

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *returnStr;
    if(component == 0){
        returnStr = [arrDivision objectAtIndex:row];
    }
    else if(component == 1){
        returnStr = [arrGroupLevel objectAtIndex:row];
    }
    else if(component == 2){
        returnStr = [arrSkillSet objectAtIndex:row];
    }
    return returnStr;  
}

-(CGFloat) pickerView:(UIPickerView *) pickerView widthForComponent:(NSInteger)component{
  
    return 340;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40.0;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  
  
    int intcount;
    if(component == 0){
        intcount = 4;
    }
    else if(component == 1){
        intcount = 3;
    }
    else if(component == 2){
        intcount = 2;
    }
    return intcount;
}

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
  
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

1 comment:

  1. I am extremely impressed with your writing skills as well as with the layout on your weblog.

    Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it's rare to see a nice blog like this one today.
    my page :: iphone 3 screen repair

    ReplyDelete