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];
目前学习了 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];
相关文章推荐
- IOS高级教程1:处理1000张图片的内存优化
- IOS 7.1 兼容性bug 问题,UIButton,UILabel ...
- CALayer与UIView的关系
- UIImageView animationImages内存问题
- UIView的bounds和frame区别
- UIImage的一个实例函数stretchableImageWithLeftCapWidth
- uiview
- uiview生命周期
- UIView翻译 (参考)
- iOS 转变UIView到UIImage
- UIView动画
- UIButton小结
- UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例
- initWithFrame 和 initWithCoder
- [CS193P] 第六堂課摘要及心得筆記
- 【无线互联】基于ios系统的图片(滤镜)编辑
- 棋盘
- ios实现button变换颜色并可以放大、缩小、旋转
- UIButton的使用
- UIImageView的使用