您的位置:首页 > 产品设计 > UI/UE

iOS UIview 学习

2015-06-01 22:39 393 查看
UIview 是一个很重要的方面

目前学习了 UIView,UITextField, UIButton, UILabel, UIImageView,等几个类, 目前可以编写计算器程序。

UIview 常用属性:

给UIview 添加图片

    UIView *aView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 400,
500)];

    [aView setBackgroundColor:[UIColor
greenColor]];
    [aView setClipsToBounds:YES]
    [self.window addSubview:aView];

    
   
UIImageView *aImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 400,
500)];

    [aImageView setBackgroundColor:[UIColor
greenColor]];

    //[aImageView setAlpha:0.8];
    [aImageView
setHighlighted:YES];

    [aImageView setHighlightedImage:[UIImage
imageNamed:@"a.png"]];

   
UIImageView *bImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 60,
60)];

    [bImageView setBackgroundColor:[UIColor
greenColor]];
    [bImageView
setHighlighted:YES];

    [bImageView setHighlightedImage:[UIImage
imageNamed:@"0.png"]];

    [bImageView setUserInteractionEnabled:YES];

    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(doImg1Click)];
    [bImageView
addGestureRecognizer:singleTap1];
    [aView
addSubview:aImageView];
    [aView
addSubview:bImageView];

UItextField:常用属性

    UITextField *aTextField = [[UITextField
alloc] initWithFrame:CGRectMake(30,
30, 200,
40)];

    [aTextField setBorderStyle:UITextBorderStyleRoundedRect];

    [aTextField setKeyboardType:UIKeyboardTypeNamePhonePad];

    //aTextField.editing = NO;
    [aTextField
setEnabled:NO];
    [aTextField
setPlaceholder:@"enter here"];
    [aTextField
setSelected:NO];
    [self.window
addSubview:aTextField];
    [aView
addSubview:aTextField];
UIbutton 常用属性:

UIButtonTypeCustom 这个属性在我的xcode中不起作用,无论设置那个枚举值,都显示直角形状,而且没有按下去的效果。 目前还不清楚原因。

如果要编写计算器,可以用id calButtons[100]; 创建一组button。

    UIButton *aButton = [UIButton
buttonWithType:UIButtonTypeCustom];
    aButton.frame =
CGRectMake(40,
200, 100,
40);

   // [aButton.layer setBorderColor:colorref];

    [aButton addTarget:self
action:@selector(doClick:)
forControlEvents:UIControlEventTouchUpInside];

    [aButton setBackgroundColor:[UIColor
blueColor]];

    [aButton setTitle:@"OK"
forState:UIControlStateNormal];

    [aButton setTitle:@"HAHA"
forState:UIControlStateHighlighted];
    [aButton
setAlpha:0.4];
    [aButton.layer
setCornerRadius:10.0];
    [aButton.layer
setBorderWidth:1.0];
//边框宽度

    CGColorSpaceRef colorSpace =
CGColorSpaceCreateDeviceRGB();
   
CGColorRef colorref =
CGColorCreate(colorSpace,(CGFloat[]){
1, 0, 0,
1 });
    [aButton.layer
setBorderColor:colorref];//边框颜色
UILabel 常用属性也是这些基本设置 
要是把数字设置到label上, 就需要通过字符串转换才行, 在计算器中, 我们需要把button上的数值获得,并转化成数字,设置到label上时,又需要转化回去
把所有需要的button等都设置好tag,记录tag列表。
假设我所有的View都设置了 tag,这一段是把text里的数字放到label上。

    UILabel *sLabel = (UILabel *)[self.window
viewWithTag:1001];
   
UITextField *reTextField = (UITextField *)[self.window
viewWithTag:1002];
   
NSString *sString = reTextField.text;
   
int numVal = [sString
intValue];
    sLabel.text = [NSString
stringWithFormat:@"%d", numVal];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息