IOS 控件整理总结(基础控件)

来源:互联网 发布:java date 格式化 毫秒 编辑:程序博客网 时间:2024/05/16 18:04



#import "ViewController.h"


@interface ViewController ()<UITextFieldDelegate,UIScrollViewDelegate>

@property (nonatomic,strong)NSTimer *timer;

@property (nonatomic,strong)UIProgressView *pv;

@property (nonatomic,strong)UIPageControl *pc;

@property (nonatomic,strong)UIScrollView *sv;

@property (nonatomic,strong)UILabel *label;

@property (nonatomic)int currentPage;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    [selfinitButton];

    

    [selfinitLabel];

    

    [selfinitTextField];

    

    [selfinitUISwitch];

    

    [selfinitProgressView];

    

    [selfinitUISlider];

    

    //7.UISegmentControl;

    //未完成

    

    [selfinitViewLabel];

    

    [selfinitUIScrollView];

    

    [selfinitUIPageControl];

    

    

}



#pragma mark     1.button;

-(void)initButton{

    UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,100,100)];

    

    [btn setBackgroundImage:[UIImageimageNamed:@"btn_01"]forState:UIControlStateNormal];

    [btn setTitle:@"1"forState:UIControlStateNormal];

    [btn setBackgroundImage:[UIImageimageNamed:@"btn_02"]forState:UIControlStateHighlighted];

    [self.viewaddSubview:btn];

    

    [btn addTarget:selfaction:@selector(ButtonAction:)forControlEvents:UIControlEventTouchUpInside];

    

}

-(void)ButtonAction:(UIButton *)btn{

     [btn setBackgroundImage:[UIImageimageNamed:@"btn_02"]forState:UIControlStateNormal];

    if ((btn.imageView.image = [UIImageimageNamed:@"btn_02"])) {

        [btn setImage:[UIImageimageNamed:@"a"]forState:UIControlStateNormal];

    }

}


#pragma mark     2.label;


-(void)initLabel{

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(100,0,100,100)];

    label.text =@"按钮1是傻子,我是Label";

    [label setNumberOfLines:0];

    label.backgroundColor = [UIColorredColor];

    [label setTextAlignment:NSTextAlignmentRight];

//    label.font = [UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]

    [self.viewaddSubview:label];

}



#pragma mark     3.UITextField;

-(void)initTextField{

    UITextField *tf = [[UITextFieldalloc]initWithFrame:CGRectMake(200,0,100,100)];

    tf.delegate =self;

    tf.text =@"禁止输入@“,和.”";

    tf.font = [UIFontsystemFontOfSize:12];

    tf.backgroundColor = [UIColoryellowColor];

//    [self.view endEditing:YES];

//    [tf resignFirstResponder];

    

    //?收起键盘;

    [self.viewaddSubview:tf];

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if ([stringisEqualToString:@"."]||[stringisEqualToString:@","]) {

        returnNO;

    }else{

        returnYES;

    }

}


#pragma mark     4.ProgressView; 5.UISwitch实现;


-(void)initProgressView{

    self.pv = [[UIProgressViewalloc]initWithFrame:CGRectMake(100,150,100,100)];

    self.pv.progressTintColor = [UIColorblueColor];

    self.pv.trackTintColor = [UIColoryellowColor];

    [self.viewaddSubview:self.pv];

}

-(void)initUISwitch{

    UISwitch *sw = [[UISwitchalloc]initWithFrame:CGRectMake(0,150,100,100)];

    sw.backgroundColor = [UIColorgreenColor];

    [sw addTarget:selfaction:@selector(changeValue:)forControlEvents:UIControlEventValueChanged];

        [self.viewaddSubview:sw];

}


-(void)changeValue:(UISwitch *)sw{

    

    if ([swisOn]) {

        self.timer = [NSTimerscheduledTimerWithTimeInterval:.1target:selfselector:@selector(valueChange)userInfo:nilrepeats:YES];

    }else{

        [self.timerinvalidate];

    }


}


-(void)valueChange{

    self.pv.progress  +=0.01;

}



#pragma mark     6.UISlider;

-(void)initUISlider{

    UISlider *sl = [[UISlideralloc]initWithFrame:CGRectMake(200,120,100,100)];

    sl.minimumValue =0;

    sl.minimumTrackTintColor = [UIColorredColor];

    sl.maximumValue =30;

    sl.maximumTrackTintColor = [UIColoryellowColor];

    [sl addTarget:selfaction:@selector(slAction:)forControlEvents:UIControlEventValueChanged];

    [self.viewaddSubview:sl];

    

    UILabel *l = [[UILabelalloc]initWithFrame:CGRectMake(200,110,100,20)];

    l.backgroundColor = [UIColoryellowColor];

    l.text = [NSStringstringWithFormat:@"%1.f",sl.value];

    [self.viewaddSubview:l];


    

    

}


-(void)slAction:(UISlider *)sl{

    UILabel *l = [[UILabelalloc]initWithFrame:CGRectMake(200,110,100,20)];

    l.backgroundColor = [UIColoryellowColor];

    l.text = [NSStringstringWithFormat:@"%1.f",sl.value];

    [self.viewaddSubview:l];

}





#pragma mark     7.UIScrollView; 8.UIPageControl;混合实现;

-(void)initUIScrollView{

    self.sv = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,220,320,568-220)];

    [self.viewaddSubview:self.sv];

    self.sv.delegate =self;

    //self.sv.backgroundColor = [UIColor yellowColor];

    for (int i =0; i <5; i ++) {

        UIImageView *iv = [[UIImageViewalloc]initWithFrame:CGRectMake(i*320,0,320,568-220)];

        iv.image = [UIImageimageNamed:[NSStringstringWithFormat:@"%d.jpg",i]];

        [self.svaddSubview:iv];

    }

    

    //[sv isUserInteractionEnabled];

    self.sv.contentSize =CGSizeMake(5*320,0);//设置横向内容大小;

    self.sv.pagingEnabled =YES;//设置惯性;

    

}

-(void)initUIPageControl{

    self.pc = [[UIPageControlalloc]initWithFrame:CGRectMake(150,220,100,100)];

    [self.pcsetNumberOfPages:5];

    //self.pc.backgroundColor = [UIColor redColor];

   // self.pc.tintColor = [UIColor redColor];

    self.pc.pageIndicatorTintColor = [UIColoryellowColor];

    self.pc.currentPageIndicatorTintColor = [UIColorredColor];

    [self.viewinsertSubview:self.svatIndex:0];

//    [self.view b/ringSubviewToFront:self.pc];

    [self.viewaddSubview:self.pc];

   

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    //惯性结束

    self.pc.currentPage = scrollView.contentOffset.x/320;

   self.label.text = [NSStringstringWithFormat:@"%d of 5",self.pc.currentPage+1];


}

//动画做完之后执行

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    

    self.label.text = [NSStringstringWithFormat:@"%d of 5",self.pc.currentPage+1];

    

    

    

}


-(void)initViewLabel{

   self.label = [[UILabelalloc]initWithFrame:CGRectMake(150,180,100,100)];

    self.label.textColor = [UIColorredColor];

    self.label.textColor = [UIColorredColor];

    [self.viewaddSubview:self.label];


}







@end


0 0
原创粉丝点击