UI复习笔记1

UIStepper

初始化控件

1
UIStepper * step = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

设置控制器值是否连续触发变化

@property(nonatomic,getter=isContinuous) BOOL continuous;

若设置为YES,则长按会连续触发变化,若设置为NO,只有在按击结束后,才会触发。

设置长按是否一直触发变化

@property(nonatomic) BOOL autorepeat; 

若设置为YES,则长按值会一直改变,若设置为NO,则一次点击只会改变一次值

设置控制器的值是否循环(到达边界后,重头开始,默认为NO)

@property(nonatomic) BOOL wraps;

设置控制器的值

@property(nonatomic) double value; 

设置控制器的最大值和最小值

@property(nonatomic) double minimumValue;//默认为0

@property(nonatomic) double maximumValue; //默认为100

设置控制器的步长

@property(nonatomic) double stepValue; 

设置控制器风格颜色

@property(nonatomic,retain) UIColor *tintColor;

设置控制器背景图片

- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state;

获取背景图片

- (UIImage*)backgroundImageForState:(UIControlState)state;

通过左右按钮的状态设置分割线的图片

- (void)setDividerImage:(UIImage*)image forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;

获取分割线图片

- (UIImage*)dividerImageForLeftSegmentState:(UIControlState)state rightSegmentState:(UIControlState)state;

设置和获取加号按钮的图片

- (void)setIncrementImage:(UIImage *)image forState:(UIControlState)state;

- (UIImage *)incrementImageForState:(UIControlState)state;


设置和获取减号按钮的图片

- (void)setDecrementImage:(UIImage *)image forState:(UIControlState)state;

 
- (UIImage *)decrementImageForState:(UIControlState)state;
 
 
UISwitch

一、第一种创建UISwitch控件的方法,在代码中动态创建。

1、打开Xcode  4.3.2, 新建项目Switch,选择Single View Application。

2、打开ViewController.m文件在viewDidLoad方法里添加代码:

[cpp] view plain copy
 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 20, 10)];  
  5.     [switchButton setOn:YES];  
  6.     [switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];  
  7.     [self.view addSubview:switchButton];  
  8.       
  9.     // Do any additional setup after loading the view, typically from a nib.  
  10. }  

[switchButton addTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];

代码中selector中的switchAction:需要我们自己实现,就是按下时接收到的事件。

记得把switchButton加到当前view,调用[self.viewaddSubview:switchButton];

3、监听UISwitch按下事件

实现代码如下:
[cpp] view plain copy
 
  1. -(void)switchAction:(id)sender  
  2. {  
  3.     UISwitch *switchButton = (UISwitch*)sender;  
  4.     BOOL isButtonOn = [switchButton isOn];  
  5.     if (isButtonOn) {  
  6.         showSwitchValue.text = @"是";  
  7.     }else {  
  8.         showSwitchValue.text = @"否";  
  9.     }  
  10. }  
 
DAY-1 UIView
Projecte 1
- (void)viewDidLoad {
    [super viewDidLoad];

    self.aView=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    self.aView.backgroundColor=[UIColor redColor];
   
    self.bView=[[UIView alloc] initWithFrame:CGRectMake(150, 150, 200, 200)];
    self.bView.backgroundColor=[UIColor greenColor];
   
    self.cView=[[UIView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
    self.cView.backgroundColor=[UIColor blueColor];
    //添加子视图
    [self.view addSubview:self.aView];
    [self.view addSubview:self.bView];
    [self.view addSubview:self.cView];
//    插入指定视图 在XXX的下面
    //[self.view insertSubview:self.bView belowSubview:self.aView];
//    在XXX上面
   // [self.view insertSubview:self.bView aboveSubview:self.aView];
//    在指定位置插入子视图
    //[self.view insertSubview:self.bView atIndex:0];
//    在父视图中移除子视图
   // [self.bView removeFromSuperview];
   
//    交换子视图的索引位置
    //[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
    //将子视图放到最后
    [self.view sendSubviewToBack:self.cView];
//    将子视图放到最前
    [self.view bringSubviewToFront:self.aView];
   
}
    //1.初始化视图
    self.myView=[[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];
//    2.背景色
    self.myView.backgroundColor=[UIColor redColor];
//    3.添加子视图到view上
    [self.view addSubview:self.myView];
   
    self.view.backgroundColor=[UIColor purpleColor];
   
    CGRect rectView= self.view.frame;
   
    NSLog(@"%@",NSStringFromCGRect(rectView));
    //frame 相对父视图的坐标位置
    NSLog(@"myView.frame : %@",NSStringFromCGRect( self.myView.frame ));
//    bounds  只是显示当前视图的大小  和位置无关  (0,0)
    NSLog(@"myView.bounds:%@",NSStringFromCGRect(self.myView.bounds));
   
//    center 控件相对于父视图的中心点坐标
    NSLog(@"%@",NSStringFromCGPoint(self.myView.center));
//    设置视图的中心点坐标
    //self.myView.center=CGPointMake(300, 550);
//    改变视图的边界
   
    //self.myView.bounds=CGRectMake(0, 0, 50  , 50);
//    水平方向平移200点
//    self.myView.transform=CGAffineTransformMakeTranslation(200, 0);
     //self.myView.transform=CGAffineTransformMakeTranslation(0, 200);
    // self.myView.transform=CGAffineTransformMakeTranslation(200, 200);
   
    self.myView.transform=CGAffineTransformMakeRotation(3.14/4);
   
   
DAY1  UIlabel
    //1.UILable的大小自适应实例:
    UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, 2)];//设定位置与大小
    [myLabel setFont:[UIFont fontWithName:@"Helvetica" size:20.0]];//格式
    [myLabel setNumberOfLines:0];//行数,只有设为0才可以自适应
    [myLabel setBackgroundColor:[UIColor clearColor]];//背景色
    myLabel.shadowColor = [UIColor darkGrayColor];//阴影颜色
    myLabel.shadowOffset = CGSizeMake(1.0,1.0);//阴影大小
   
    NSString *text = @"abcdefghigklmnopqrstuvwxyz";
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:20.0];
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
    CGRect rect=myLabel.frame;
    rect.size=size;
    [myLabel setFrame:rect];
    [myLabel setText:text];
     myLabel.shadowColor = [UIColor darkGrayColor];//阴影颜色
     myLabel.shadowOffset = CGSizeMake(2.0,2.0);//阴影大小
    [self.view addSubview:myLabel];
    [myLabel release];
    //2.UILable的基本用法获取自馒头MAN百度空间,感谢馒头MAN
            //空间地址:http://hi.baidu.com/bunsman/blog/item/95777b0ebacf05fe36d122e2.html
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 40.0, 200.0, 30.0)];     
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];     
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];     
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];     
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];     
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];     
    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];     
   
    //设置显示文字     
    label1.text = @"label1";     
    label2.text = @"label2";     
    label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--11个";     
    label4.text = @"label4--label4--label4--label4--4个";     
    label5.text = @"label5--label5--label5--label5--label5--label5--6个";     
    label6.text = @"label6";     
    label7.text = @"label7";     
   
    //设置字体:粗体,正常的是 SystemFontOfSize     
    label1.font = [UIFont boldSystemFontOfSize:20];     
   
    //设置文字颜色 
    label1.textColor = [UIColor orangeColor];     
    label2.textColor = [UIColor purpleColor];     
    //设置背景颜色 
    label1.backgroundColor = [UIColor clearColor];
    label2.backgroundColor = [UIColor  colorWithRed:0.5f green:30/255.0f blue:0.3f alpha:0.5f];
    //设置文字位置     
    label1.textAlignment = UITextAlignmentRight;     
    label2.textAlignment = UITextAlignmentCenter;     
    //设置字体大小适应label宽度     
    label4.adjustsFontSizeToFitWidth = YES;
    //设置label的行数     
    label5.numberOfLines = 2;     
   
    //设置高亮     
    label6.highlighted = YES;     
    label6.highlightedTextColor = [UIColor orangeColor];     
   
    //设置阴影     
    label7.shadowColor = [UIColor redColor];     
    label7.shadowOffset = CGSizeMake(1.0,1.0);     
   
    //设置是否能与用户进行交互     
   
    label7.userInteractionEnabled = YES;     
   
    //设置label中的文字是否可变,默认值是YES     
    label3.enabled = NO;     
   
    //设置文字过长时的显示格式     
   
    label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间     
    //  typedef enum {     
    //      UILineBreakModeWordWrap = 0,     
    //      UILineBreakModeCharacterWrap,     
    //      UILineBreakModeClip,//截去多余部分     
    //      UILineBreakModeHeadTruncation,//截去头部     
    //      UILineBreakModeTailTruncation,//截去尾部     
    //      UILineBreakModeMiddleTruncation,//截去中间     
    //  } UILineBreakMode;     
   
    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为     
    label4.baselineAdjustment = UIBaselineAdjustmentNone;     
    //  typedef enum {     
    //      UIBaselineAdjustmentAlignBaselines,     
    //      UIBaselineAdjustmentAlignCenters,     
    //      UIBaselineAdjustmentNone,     
    //  } UIBaselineAdjustment;     
   
   
  添加到视图
    self.lblName=[[UILabel alloc] initWithFrame:CGRectMake(100, 50, 150, 400)];
    self.lblName.backgroundColor=[UIColor redColor];
//    文本
    self.lblName.text=@"姓名";
//    文本颜色
    self.lblName.textColor=[UIColor greenColor];
//    文本对齐方式
    self.lblName.textAlignment=NSTextAlignmentCenter;
//    文本字体
    self.lblName.font=[UIFont systemFontOfSize:32 weight:5];
//    显示行数
    self.lblName.numberOfLines=10;
    [self.view addSubview:self.lblName];
 
DAY-2 UITextField
 
    self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    self.txtName.text=@"guiyang";
    self.txtName.textColor=[UIColor blueColor];
//    边框样式 
    self.txtName.borderStyle=1;
    self.txtName.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:self.txtName];
   
   
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
   // NSLog(@"touch begin");
    [self hideKeyBoard];
}
/**
 *  隐藏键盘的方法
 */
-(void)hideKeyBoard
{
//    判断是否是第一响应者
    if ([self.txtName isFirstResponder]) {
//        失去第一响应者
        [self.txtName resignFirstResponder];
    }
}

UItextField通常用于外部数据输入,以实现人机交互。下面以一个简单的登陆界面来讲解UItextField的详细使用。

//用来显示“用户名”的label

UILabel* label1 = [[UILabelalloc] initWithFrame:CGRectMake(15, 65, 70, 30)];

    label1.backgroundColor = [UIColorclearColor];

    label1.font = [UIFontfontWithName:@"Helvetica-Bold"size:18];

    label1.text = @"用户名";

    label1.textColor = [UIColorwhiteColor];

    [view1 addSubview:label1];

    [label1 release];

   UITextField * accountField = [[UITextField alloc] initWithFrame:CGRectMake(85.0f, 60.0f, 190.0f, 40.0f)];

[accountField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型

accountField.placeholder = @"用户名"; //默认显示的字

accountField.secureTextEntry = NO; //是否以密码形式显示

accountField.autocorrectionType = UITextAutocorrectionTypeNo;//设置是否启动自动提醒更正功能

accountField.autocapitalizationType = UITextAutocapitalizationTypeNone;

accountField.returnKeyType = UIReturnKeyDone;  //键盘返回类型

accountField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X

accountField.delegate = self;

accountField.keyboardType = UIKeyboardTypeDefault;//键盘显示类型

accountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //设置居中输入

accountField.scrollEnabled = YES;//是否可以拖动

accountField.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度

    //用来显示“密码”的label

    UILabel* label2 = [[UILabelalloc] initWithFrame:CGRectMake(15, 120, 70, 30)];

    label2.backgroundColor = [UIColorclearColor];

    label2.font = [UIFontfontWithName:@"Helvetica-Bold"size:18];

    label2.text = @"密码";

    label2.textColor = [UIColorwhiteColor];

    [view1 addSubview:label2];

    [label2 release];

  UITextField*  passwdField = [[UITextField alloc] initWithFrame:CGRectMake(85.0f, 115.0f, 190.0f, 40.0f)];

[passwdFieldsetBorderStyle:UITextBorderStyleRoundedRect]; //外框类型

//passwdField.placeholder = @"密码"; //默认显示的字

passwdField.secureTextEntry = YES; //密码类型

passwdField.autocorrectionType = UITextAutocorrectionTypeNo;   

passwdField.autocapitalizationType = UITextAutocapitalizationTypeNone;

passwdField.returnKeyType = UIReturnKeyDone;

passwdField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X

passwdField.delegate = self;

 // passwdField.keyboardAppearance = UIKeyboardAppearanceDefault;

passwdField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

passwdField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

委托方法

-(void)textFieldDidBeginEditing:(UITextField *)textField;  

//当开始点击textField会调用的方法    

-(void)textFieldDidEndEditing:(UITextField *)textField; 

//当textField编辑结束时调用的方法

 
//按下Done按钮的调用方法,我们让键盘消失  
-(BOOL)textFieldShouldReturn:(UITextField *)textField{  
 
    self.btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.btn setTitle:@"test" forState:UIControlStateNormal];
    self.btn.frame=CGRectMake(100, 100, 150, 44);
    [self.btn addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
}

-(void)test
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"系统提示" message:@"你真的要用2G3G4G流量聊天吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
   
   
    alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        NSLog(@"cancel");
    }
    else if (buttonIndex==1)
    {
       
      UITextField *txtName=  [alertView textFieldAtIndex:0];
        NSLog(@"%@",txtName.text);
    }
 
 
DAY-5 
ScrollView
#define HEIGHT self.view.frame.size.height
#define WIDTH  self.view.frame.size.width
@interface ViewController : UIViewController<UIScrollViewDelegate>

@property(strong,nonatomic) UIScrollView *myScrollView;


@property(strong,nonatomic) UIPageControl *mypageControl;
@end
- (void)viewDidLoad {
    [super viewDidLoad];
 
//    设置控件大小
    self.myScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.myScrollView.backgroundColor=[UIColor grayColor];
    // 内容面板大小
    self.myScrollView.contentSize=CGSizeMake(WIDTH*3, HEIGHT);
   
    //指定代理
   
    self.myScrollView.delegate=self;
    UIImageView *imgView1=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , HEIGHT)];
    imgView1.image=[UIImage imageNamed:@"IMG_3824"];
   
   
    UIImageView *imgView2=[[UIImageView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH , HEIGHT)];
    imgView2.image=[UIImage imageNamed:@"IMG_3825"];
   
    UIImageView *imgView3=[[UIImageView alloc] initWithFrame:CGRectMake(WIDTH*2, 0, WIDTH , HEIGHT)];
    imgView3.image=[UIImage imageNamed:@"IMG_3826"];
   
    [self.myScrollView addSubview:imgView1];
    [self.myScrollView addSubview:imgView2];
    [self.myScrollView addSubview:imgView3];
    //锁定滚动方向
    self.myScrollView.directionalLockEnabled=YES;
//    设置分页
    self.myScrollView.pagingEnabled=YES;
//    隐藏滚动条
    self.myScrollView.showsHorizontalScrollIndicator=NO;
    [self.view addSubview:self.myScrollView];
   
   
    self.mypageControl=[[UIPageControl alloc] init];
   
    CGSize pageSize=CGSizeMake(120, 44);
   
    self.mypageControl.frame=CGRectMake((WIDTH-pageSize.width)/2, HEIGHT-pageSize.height-40, pageSize.width, pageSize.height);
   
    self.mypageControl.backgroundColor=[UIColor clearColor];
   
    self.mypageControl.numberOfPages=3;
    self.mypageControl.currentPage=0;
   
    [self.view addSubview:self.mypageControl];
   
   
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   
    self.mypageControl.currentPage=(int)(scrollView.contentOffset.x/WIDTH);
    //NSLog(@"%f",scrollView.contentOffset.x/WIDTH);
}
 
PickerView  还要练习
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

@property(strong,nonatomic) UIPickerView *myPickerView;
@property(strong,nonatomic) NSArray *arr;
- (void)viewDidLoad {
    [super viewDidLoad];

    self.arr=@[@"aa",@"bb",@"cc",@"dd",@"ee"];
   
    self.myPickerView=[[UIPickerView alloc] initWithFrame:CGRectMake(100, 200, 200, 100)];
    self.myPickerView.backgroundColor=[UIColor redColor];
   
    self.myPickerView.delegate=self;
    self.myPickerView.dataSource=self;
   
    [self.view addSubview:self.myPickerView];
   
   
   
}
#pragma mark 数据源  numberOfComponentsInPickerView
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

#pragma mark 数据源 Method pickerView:numberOfRows:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.arr.count;
}
#pragma mark delegate 显示信息方法

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   return  self.arr[row];
}
#pragma mark 选中行的信息
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%@",self.arr[row]);
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 100.0;
}
DAY-6 6-1 -6- 2练习
页面切换   (模态视图页面切换方式)6-1

1.NavigationController切换UIViewController的两种方式

方法一右侧进入

1 SecondViewController* svc=[[SecondViewController alloc]init];
2 [self.navigationController pushViewController:fvc animated:YES];

返回到上一个

[self.navigationController popViewControllerAnimated:YES];

另一种方法从下面切入

SecondViewController* svc=[[SecondViewController alloc]init];
[self.navigationController presentModalViewController:svc animated:YES];
[svc release];

 返回到上一个UIViewController

[self.navigationController dismissModalViewControllerAnimated:YES];

2.如果没有导航栏NavigationController的话 也是可以切换的

SecondViewController* svc=[[SecondViewController alloc]init];
[self presentModalViewController:svc animated:YES];
[svc release];

 返回到上一个UIViewController

[self dismissModalViewControllerAnimated:YES];
 
DAY-7 传值
原文地址:https://www.cnblogs.com/tianlianghong/p/5343131.html