사용자 정의 pickview 및 선택 효과

5229 단어 view

//
//  ViewController.m
//  UIPickerViewBySelf
//
//  Created by MAC on 13-1-29.
//  Copyright (c) 2013  MAC. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize fontlabel;
@synthesize pickView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
    
    fonts = [UIFont familyNames];
    fontSize = [NSArray arrayWithObjects:@"10",@"15",
                                          @"20",@"25",
                                          @"30",@"35",
                 @"40",@"45", nil];
    
    fontColor =[NSArray arrayWithObjects:
                [UIColor redColor],
                [UIColor blueColor],
                [UIColor blackColor],
                [UIColor yellowColor],
                [UIColor grayColor],
                [UIColor greenColor],
                [UIColor brownColor],
                [UIColor orangeColor],
                [UIColor purpleColor],
                [UIColor magentaColor],nil];
    
    for(int i= 0;i<3;i++)
    {
        int row = 0;
        if(i==0)
        {
            row = [fonts count]/2;
        }
        else if (i==1)
        {
            row = [fontColor count]/2;
        }
        else if (i==2)
        {
            row =[fontSize count]/2;
        }
[pickView selectRow:row inComponent:i animated:true];
    }


//    [fontColor retain];
    
	// Do any additional setup after loading the view, typically from a nib.
}
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    UIView *view0,*view1,*view2;
    NSInteger row0,row1,row2;
    row0 = [pickView selectedRowInComponent:0];
    row1 = [pickView selectedRowInComponent:1];
    row2 = [pickView selectedRowInComponent:2];
    
    view0 = [pickView viewForRow:row0 forComponent:0];
    view1 = [pickView viewForRow:row1 forComponent:1];
    view2 = [pickView viewForRow:row2 forComponent:2];
    


    UILabel *label1,*label2,*label0;
    label0 =(UILabel *)[view0 viewWithTag:200];
    label1 =(UILabel *)[view1 viewWithTag:200];
    label2 =(UILabel *)[view2 viewWithTag:200];
    
    fontlabel.font = [UIFont fontWithName:label0.text size:[label2.text floatValue]];
    fontlabel.textColor = label1.backgroundColor;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark UIPICKVIEW CUSTOM FUNTION
//        
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
//         
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component==0)
    {
        return [fonts count];}
    else if(component==1)
    {
        return [fontColor count];
        
    }
    else if(component ==2)
    {
        return [fontSize count];
    }
    return -1;
}
//        
-(CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if(component==0)
    {
        return 180.0f;}
    else if(component==1)
    {
        return 90.0f;
        
    }
    else if(component ==2)
    {
        return 50.0f;
    }
    return 0.0f;
}
//  row  
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 50.0f;
}
//         view  
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    CGFloat width = [self pickerView:pickView widthForComponent:component];
    CGFloat rowheight =[self pickerView:pickView rowHeightForComponent:(component)];
    
    UIView *myView = [[UIView alloc]init];
    myView.frame =CGRectMake(0.0f, 0.0f, width, rowheight);
    UILabel *txtlabel = [[UILabel alloc] init];
    txtlabel.tag=200;
    txtlabel.frame = myView.frame;
    
    [myView addSubview:txtlabel];
    if(component==0)
    {
        txtlabel.text = [fonts objectAtIndex:row];
    }
    else if(component==1)
    {
        txtlabel.backgroundColor =[fontColor objectAtIndex:row];
    }
    else if(component==2)
    {
        txtlabel.text =[fontSize objectAtIndex:row];
    }
    return myView;
}


@end


좋은 웹페이지 즐겨찾기