Objective-C语法系列在之前的文章中一直在介绍基本的语法的相关知识,但是学习语法的目的还是为了走进iPhoneIOS的开发世界。从今以后Objective-C语法文章将不在更新。全力更新IOS游戏开发软件开发系列文章,这篇文章MOMO将带各位盆友们简单介绍iPhone开发的一些基本控件的使用,简单的构建我们第一个iPhone应用程序。各位盆友们我们先预热一下,嘿嘿。读过我Android系列开发的盆友应该很清楚这个熟悉的界面吧,哇咔咔~~
获取手机屏幕尺寸的方法view source2 | CGRect rect=[[UIScreen mainScreen] bounds]; |
3 | CGSize size = rect.size; |
4 | int screenWidth = size.width; |
5 | int screenHeight = size.height; |
1.文本框视图在视图中加入一个文本框,可在框内攥写一些内容,设置字体颜色,位置 ,大小等等。view source02 | label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)]; |
04 | label.text = @ "雨松MOMO的程序世界" ; |
06 | label.backgroundColor = [UIColor blueColor]; |
08 | label.textColor = [UIColor whiteColor]; |
10 | label.textAlignment = UITextAlignmentCenter; |
12 | label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20]; |
2.按钮视图 按钮类型为1 为普通按钮,CGrectMake设置按钮的位置与大小,前两个参数设置按钮起始X 与 Y坐标,后两个参数设置按钮的宽度与高度。 这里重点说一下addTarget, 它可以设置按钮的绑定事件,action:设置按钮点击后响应方法,这行代码的意思是点击这个按钮后程序执行方法ButtonPressed这个函数中的代码。view source02 | button = [UIButton buttonWithType:1]; |
04 | button.frame = CGRectMake(0, 40, screenWidth, 30); |
06 | [button setTitle:@ "这是一个按钮"
forState:UIControlStateNormal]; |
08 | button.backgroundColor = [UIColor blackColor]; |
10 | [button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside]; |
点击这个按钮后进入下面这个方法,弹出一个dialog对话框。view source05 | UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@ "我的视图"
message:@ "欢迎一起学习IPHONE开发"
delegate:self cancelButtonTitle:@ "确定"
otherButtonTitles: nil]; |
07 | [alertA addButtonWithTitle:@ "取消" ]; |
10 | //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯 |
3.进度条视图和上面button视图的构建差不多,这里设置进度条最大值与最小值,拖动的时候就可以直接得到这个范围之间的数值,同样将拖动事件绑定在valueChangeTest方法中。view source02 | slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)]; |
04 | slider.maximumValue=100; |
10 | slider.backgroundColor=[UIColor blackColor]; |
12 | [slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged]; |
拖动进度条后发生改变进入下面方法,[slider vale]可以得到拖动的进度值。view source4 | float
value = [slider value]; |
5 | NSLog(@ "进度条已经发生改变:%f" ,value); |
4.编辑框视图非常常见的视图,可以在编辑框中输入信息。前提是用户触摸点击输入框,这时弹出系统软键盘方可输入信息,但是这个输入框不会自动关闭,须要我们在程序中自己调用代码去关闭,稍后介绍如何关闭这个输入框。view source02 | textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)]; |
05 | textfield.text = @ "这是一个输入框" ; |
07 | textfield.placeholder = @ "请在输入框是输入信息" ; |
09 | textfield.textAlignment = UITextAlignmentLeft; |
11 | textfield.textColor = [UIColor grayColor]; |
13 | textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17]; |
15 | textfield.borderStyle = 3; |
17 | textfield.clearsOnBeginEditing = YES; |
19 | textfield.backgroundColor = [UIColor blackColor]; |
5.图片视图设置图片在屏幕中显示的位置,当然这个图片文件必需拷贝到工程当中,拷贝方法可以直接将图片用鼠标拖动到Xcode的工程中。view source2 | imageview = [[UIImageView alloc] initWithFrame: |
3 | CGRectMake(100, 200, 120, 120)]; |
6 | [imageview setImage:[UIImage imageNamed:@ "temp.jpg" ]]; |
6.透明全屏按钮它的存在就是为了解决输入法出现后无法自动关闭,就是说如果输入法软键盘出现后 ,这时候点击屏幕任意位置关闭输入法,实现的原理是用户点击到了屏幕中设置的透明按钮,调用关闭输入法方法将输入法关闭了而已 .view source2 | backgroudButton=[[UIButton alloc] init]; |
4 | backgroudButton.frame = self.view.frame; |
6 | [backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside]; |
点击屏幕任意位置,关闭输入法。view source4 | [textfield resignFirstResponder]; |
这样所有的视图的代码都已经贴上,这些视图实际上是subView,须要将这些subView添加到屏幕的主视图当中。并且为了避免内存出现泄漏,一定要及时的释放这些视图。view source02 | [self.view addSubview:backgroudButton]; |
03 | [self.view addSubview:label]; |
04 | [self.view addSubview:imageview]; |
05 | [self.view addSubview:button]; |
06 | [self.view addSubview:slider]; |
07 | [self.view addSubview:textfield]; |
下面给出完整的代码HelloWorldViewController.hview source01 | #import <UIKit/UIKit.h> |
03 | @interface HelloWorldViewController : UIViewController |
12 | UITextField * textfield; |
14 | UIImageView *imageview ; |
16 | UIButton * backgroudButton; |
HelloWorldViewController.mview source001 | #import "HelloWorldViewController.h" |
003 | @implementation HelloWorldViewController |
005 | - ( void )didReceiveMemoryWarning |
007 | // Releases the view if it doesn't have a superview. |
008 | [super didReceiveMemoryWarning]; |
010 | // Release any cached data, images, etc that aren't in use. |
013 | #pragma mark - View lifecycle |
015 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. |
021 | CGRect rect=[[UIScreen mainScreen] bounds]; |
022 | CGSize size = rect.size; |
023 | int
screenWidth = size.width; |
024 | int
screenHeight = size.height; |
027 | label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)]; |
029 | label.text = @ "雨松MOMO的程序世界" ; |
031 | label.backgroundColor = [UIColor blueColor]; |
033 | label.textColor = [UIColor whiteColor]; |
035 | label.textAlignment = UITextAlignmentCenter; |
037 | label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20]; |
040 | button = [UIButton buttonWithType:1]; |
042 | button.frame = CGRectMake(0, 40, screenWidth, 30); |
044 | [button setTitle:@ "这是一个按钮"
forState:UIControlStateNormal]; |
046 | button.backgroundColor = [UIColor blackColor]; |
048 | [button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside]; |
051 | slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)]; |
053 | slider.maximumValue=100; |
055 | slider.minimumValue=0; |
059 | slider.backgroundColor=[UIColor blackColor]; |
061 | [slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged]; |
064 | textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)]; |
067 | textfield.text = @ "这是一个输入框" ; |
069 | textfield.placeholder = @ "请在输入框是输入信息" ; |
071 | textfield.textAlignment = UITextAlignmentLeft; |
073 | textfield.textColor = [UIColor grayColor]; |
075 | textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17]; |
077 | textfield.borderStyle = 3; |
079 | textfield.clearsOnBeginEditing = YES; |
081 | textfield.backgroundColor = [UIColor blackColor]; |
084 | imageview = [[UIImageView alloc] initWithFrame: |
085 | CGRectMake(100, 200, 120, 120)]; |
088 | [imageview setImage:[UIImage imageNamed:@ "temp.jpg" ]]; |
091 | backgroudButton=[[UIButton alloc] init]; |
093 | backgroudButton.frame = self.view.frame; |
095 | [backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside]; |
098 | [self.view setBackgroundColor:[UIColor blackColor]]; |
101 | [self.view addSubview:backgroudButton]; |
102 | [self.view addSubview:label]; |
103 | [self.view addSubview:imageview]; |
104 | [self.view addSubview:button]; |
105 | [self.view addSubview:slider]; |
106 | [self.view addSubview:textfield]; |
120 | UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@ "我的视图"
message:@ "欢迎一起学习IPHONE开发"
delegate:self cancelButtonTitle:@ "确定"
otherButtonTitles: nil]; |
122 | [alertA addButtonWithTitle:@ "取消" ]; |
125 | //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯 |
130 | - ( void )valueChangeTest |
133 | float
value = [slider value]; |
134 | NSLog(@ "进度条已经发生改变:%f" ,value); |
141 | [textfield resignFirstResponder]; |
146 | [super viewDidUnload]; |
147 | // Release any retained subviews of the main view. |
148 | // e.g. self.myOutlet = nil; |
151 | - ( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
153 | // Return YES for supported orientations |
154 | return
(interfaceOrientation == UIInterfaceOrientationPortrait); |
最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习雨松MOMO希望可以和大家一起进步。下载地址:http://download.csdn.net/detail/xys289187120/3645156
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理