常用控件

Posted 东方春

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用控件相关的知识,希望对你有一定的参考价值。

//UISlider
UISlider *_sli = [[UISlideralloc]initWithFrame:CGRectMake(10, 10,200, 30)];
[_sli setMaximumValue:64.0];
[_sli setMinimumValue:-64.0];
[_sli setValue:2.0];
[_sli setThumbImage:[UIImageimageNamed:@"img1.png"]forState:UIControlStateNormal];
[_sli setThumbImage:[UIImage imageNamed:@"img2.png"] forState:UIControlStateHighlighted];
[_sli setMinimumTrackTintColor:[UIColorredColor]];
[_sli setMaximumTrackTintColor:[UIColorgreenColor]];
[_sli setThumbTintColor:[UIColoryellowColor]];
[_sli addTarget:selfaction:@selector(SliderUP:)forControlEvents:UIControlEventTouchUpInside];
[sliderView addSubview:_sli];

-(void)SliderUP:(id)sender
{
   UISlider*slider = (UISlider*)sender;
   floatvalSlider =slider.value;
}

  

//UIImageView
UIImage *img = [UIImageimageNamed:@"delete.png"];
UIImageView *imgView = [[UIImageViewalloc]initWithImage:img];
imgView.backgroundColor= [UIColor greenColor];
imgView.frame= CGRectMake(100, 100, 200,100);
imgView.contentMode=UIViewContentModeCenter;
[self.view addSubview:imgView];

//UIImageView通过多张图片做动画
UIImageView *imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,200,200)];
imgView.animationDuration = 0.2;//图片轮动的时间
imgView.backgroundColor= [UIColor greenColor];
imgView.contentMode=UIViewContentModeCenter;
imgView.animationImages = imageArr;//自行分配的image数组
[self.view addSubview:imgView];

  

 

//UILabel
_lblFps = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 98, 30)];
_lblFps.textColor = [UIColor redColor];
_lblFps.backgroundColor = [[UIColor alloc]initWithRed:100 green:100blue:100 alpha:0.3];
[_lblFps setHidden:NO];
[_lblFps setTextAlignment:NSTextAlignmentCenter];
_lblFps.numberOfLines = 4;
_lblFps.lineBreakMode = NSLineBreakByWordWrapping;
_lblFps.font = [UIFont boldSystemFontOfSize:14.0f];
_lblFps.center = self.view.center;
_lblFps.adjustFontSizeToFitWidth = YES;
[_lblFps sizeToFit];
_lblFps.shadowColor = [UIColor redColor];
_lblFps.shadowOffset= CGSizeMake(3.0f,2.0f);
[self.view addSubview:_lblFps];

  

//UISwitch
UISwtich *_s = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
[_s setOn:NO];
_s.tintColor = [UIColor redColor];
_s.onTintColor = [UIColor blackColor];
_s.thumbTintColor = [UIColor yellowColor];
[_s addTarget:self action:@selector(UISwitchClickChanged:) forControlEvents:UIControlEventValueChanged];

- (void)UISwitchClickChanged:(id)sender
{
    UISwitch *s = (UISwitch*)sender;
    if([s isOn])    {
        NSLog(@"ON");
    }
    else   {
        NSLog(@"OFF");
    }
}

  

//UIButton
UIButton *btnScaner = [[UIButtonalloc]initWithFrame:CGRectMake(20,400, 100, 40)];//位置
[btnScaner setTitle:@"QRClick"forState:UIControlStateNormal];//标题
[btnScaner setBackgroundColor:[UIColorgrayColor]];//背景颜色
[btnScaner setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//文字颜色
btnScaner.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btnScanersetBackgroundImage:[UIImageimageNamed:@"preview"]forState:UIControlStateNormal];//背景图
[btnScaner.layersetCornerRadius:3.0];//圆角半径
[btnScaner.layer setBorderWidth:1.0f];//边框
[btnScaner.layer setBorderColor:[[UIColor black]CGColor]];//边框颜色
[btnScanersetContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];//对齐方式
[btnScaner addTarget:selfaction:@selector(Click:)forControlEvents:UIControlEventTouchUpInside];//事件
[btnScaner SetImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];//图片,不是背景图,此时图片会在左边,文字在右方
//shadowhh
[contentViewInner.layer setShadowColor:[[UIColor grayColor]CGColor]];
[contentViewInner.layer setShadowOpacity:0.8f];  
[contentViewInner.layer setShadowOffset:CGSizeMake(3, 3)];
 
 

//UISegmentedControl
UISegmentedControl *_segment = [[UISegmentedControlalloc]init];
[_segment removeAllSegments];
[_segment insertSegmentWithTitle:@"a1"atIndex:0animated:NO];
[_segment insertSegmentWithTitle:@"a2"atIndex:1animated:NO];
[_segment insertSegmentWithTitle:@"a3"atIndex:2animated:NO];
[_segment setSelectedSegmentIndex:1];
_segment.frame= CGRectMake(0, 300, 100, 30);
[_segment addTarget:selfaction:@selector(SegentChanged:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:_segment];

- (void)SegentChanged:(id)sender
{
  UISegmentedControl *seg= (UISegmentedControl*)sender;
  NSInteger iSeg = [seg selectedSegmentIndex];
  NSString * str = [seg titleForSegmentAtIndex:iSeg];
  NSLog(@"selected is : %d , %@",iSeg,str);
}

 


//TextField
UITextField *tf1 = [[UITextField alloc]initWithFrame:CGRectMake(200, 170, 100, 40)];//位置
tf1.text = @"Default Text";/*文字*/ 

tf1.placeholder = @"input str";//holder string
[tf1 setBackgroundColor:[UIColor grayColor]];//背景色
tf1.font = [UIFont systemFontOfSize:16];//字体大小
[tf1.layer setCornerRadius:3.0];//圆角半径

[tf1 setTextColor:[UIColor redColor]];//前景色
tf1.autocapitalizationType = UITextAutoCapitalizationTypeNone;//首字母取消默认大写
 
 
关于UITextField和UITextView添加实时监测变化
[[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(textFieldChange:)
                                            name:UITextFieldTextDidChangeNotification
                                          object:tf1];
- (void)textFieldChange:(NSNotification *)notify
{
    NSLog(@"TextField editing");
}
//[[NSNotificationCenter defaultCenter]removeObserver:self

  

 
//UITextView
UITextView *tv1 = [[UITextView alloc]initWithFrame:CGRectMake(200, 230, 100, 80)];
tv1.text = @"Default Text";
[tv1 setBackgroundColor:[UIColor grayColor]];
tv1.font = [UIFont systemFontOfSize:16];
[tv1.layer setCornerRadius:3.0];
    
关于UITextField和UITextView添加实时监测变化
[[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(textViewChange:)
                                            name:UITextViewTextDidChangeNotification
                                          object:tv1];

- (void)textViewChange:(NSNotification  *)notify
{
    NSLog(@"TextView editing");
}

  

//UIActionSheet
(1) 添加UIActionSheetDelegate代理
(2)添加函数:
- (void)showSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"delete these items"
                                  delegate:self
                                  cancelButtonTitle:@"cancel"
                                  destructiveButtonTitle:@"YES"
                                  otherButtonTitles:nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
   NSLog(@"%d",buttonIndex);
}

 

  

 

以上是关于常用控件的主要内容,如果未能解决你的问题,请参考以下文章

常用python日期日志获取内容循环的代码片段

swift常用代码片段

# Java 常用代码片段

# Java 常用代码片段

21个常用代码片段

js常用代码片段(更新中)