Objective-C语法系列在之前的文章中一直在介绍基本的语法的相关知识,但是学习语法的目的还是为了走进iPhone IOS的开发世界。从今以后Objective-C语法文章将不在更新。全力更新IOS游戏开发 软件开发系列文章,这篇文章MOMO将带各位盆友们简单介绍iPhone开发的一些基本控件的使用,简单的构建我们第一个iPhone应用程序。各位盆友们我们先预热一下,嘿嘿。
读过我Android系列开发的盆友应该很清楚这个熟悉的界面吧,哇咔咔~~
获取手机屏幕尺寸的方法
-
- CGRect rect=[[UIScreen mainScreen] bounds];
- CGSize size = rect.size;
- int screenWidth = size.width;
- int screenHeight = size.height;
1.文本框视图
在视图中加入一个文本框,可在框内攥写一些内容,设置字体颜色,位置 ,大小等等。
-
- label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
-
- label.text = @"雨松MOMO的程序世界";
-
- label.backgroundColor = [UIColor blueColor];
-
- label.textColor = [UIColor whiteColor];
-
- label.textAlignment = UITextAlignmentCenter;
-
- label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
2.按钮视图
按钮类型为1 为普通按钮,CGrectMake设置按钮的位置与大小,前两个参数设置按钮起始X 与 Y坐标,后两个参数设置按钮的宽度与高度。
这里重点说一下addTarget, 它可以设置按钮的绑定事件,action:设置按钮点击后响应方法,这行代码的意思是点击这个按钮后程序执行方法ButtonPressed这个函数中的代码。
-
- button = [UIButton buttonWithType:1];
-
- button.frame = CGRectMake(0, 40, screenWidth, 30);
-
- [button setTitle:@"这是一个按钮" forState:UIControlStateNormal];
-
- button.backgroundColor = [UIColor blackColor];
-
- [button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
点击这个按钮后进入下面这个方法,弹出一个dialog对话框。
- - (void)ButtonPressed
- {
-
-
- UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"欢迎一起学习IPHONE开发" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
-
- [alertA addButtonWithTitle:@"取消"];
-
- [alertA show];
-
- [alertA release];
-
- }
3.进度条视图
和上面button视图的构建差不多,这里设置进度条最大值与最小值,拖动的时候就可以直接得到这个范围之间的数值,同样将拖动事件绑定在valueChangeTest方法中。
-
- slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)];
-
- slider.maximumValue=100;
-
- slider.minimumValue=0;
-
- slider.value=20;
-
- slider.backgroundColor=[UIColor blackColor];
-
- [slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged];
拖动进度条后发生改变进入下面方法,[slider vale]可以得到拖动的进度值。
- - (void)valueChangeTest
- {
-
- float value = [slider value];
- NSLog(@"进度条已经发生改变:%f",value);
-
- }
4.编辑框视图
非常常见的视图,可以在编辑框中输入信息。前提是用户触摸点击输入框,这时弹出系统软键盘方可输入信息,但是这个输入框不会自动关闭,须要我们在程序中自己调用代码去关闭,稍后介绍如何关闭这个输入框。
-
- textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)];
-
-
- textfield.text = @"这是一个输入框";
-
- textfield.placeholder = @"请在输入框是输入信息";
-
- textfield.textAlignment = UITextAlignmentLeft;
-
- textfield.textColor = [UIColor grayColor];
-
- textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17];
-
- textfield.borderStyle = 3;
-
- textfield.clearsOnBeginEditing = YES;
-
- textfield.backgroundColor = [UIColor blackColor];
5.图片视图
设置图片在屏幕中显示的位置,当然这个图片文件必需拷贝到工程当中,拷贝方法可以直接将图片用鼠标拖动到Xcode的工程中。
-
- imageview = [[UIImageView alloc] initWithFrame:
- CGRectMake(100, 200, 120, 120)];
-
-
- [imageview setImage:[UIImage imageNamed:@"temp.jpg"]];
6.透明全屏按钮
它的存在就是为了解决输入法出现后无法自动关闭,就是说如果输入法软键盘出现后 ,这时候点击屏幕任意位置关闭输入法,实现的原理是用户点击到了屏幕中设置的透明按钮,调用关闭输入法方法将输入法关闭了而已 .
-
- backgroudButton=[[UIButton alloc] init];
-
- backgroudButton.frame = self.view.frame;
-
- [backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];
点击屏幕任意位置,关闭输入法。
- -(void)ButtonClick
- {
-
- [textfield resignFirstResponder];
- }
这样所有的视图的代码都已经贴上,这些视图实际上是subView,须要将这些subView添加到屏幕的主视图当中。并且为了避免内存出现泄漏,一定要及时的释放这些视图。
-
- [self.view addSubview:backgroudButton];
- [self.view addSubview:label];
- [self.view addSubview:imageview];
- [self.view addSubview:button];
- [self.view addSubview:slider];
- [self.view addSubview:textfield];
-
-
-
-
- [imageview release];
- [label release];
- [slider release];
- [textfield release];
下面给出完整的代码
HelloWorldViewController.h
- #import <UIKit/UIKit.h>
-
- @interface HelloWorldViewController : UIViewController
- {
-
- UILabel * label;
-
- UIButton * button;
-
- UISlider *slider;
-
- UITextField * textfield;
-
- UIImageView *imageview ;
-
- UIButton * backgroudButton;
- }
- @end
HelloWorldViewController.m
- #import "HelloWorldViewController.h"
-
- @implementation HelloWorldViewController
-
- - (void)didReceiveMemoryWarning
- {
-
- [super didReceiveMemoryWarning];
-
-
- }
-
- #pragma mark - View lifecycle
-
-
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- CGRect rect=[[UIScreen mainScreen] bounds];
- CGSize size = rect.size;
- int screenWidth = size.width;
- int screenHeight = size.height;
-
-
-
-
- label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
-
- label.text = @"雨松MOMO的程序世界";
-
- label.backgroundColor = [UIColor blueColor];
-
- label.textColor = [UIColor whiteColor];
-
- label.textAlignment = UITextAlignmentCenter;
-
- label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
-
-
-
- button = [UIButton buttonWithType:1];
-
- button.frame = CGRectMake(0, 40, screenWidth, 30);
-
- [button setTitle:@"这是一个按钮" forState:UIControlStateNormal];
-
- button.backgroundColor = [UIColor blackColor];
-
- [button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
-
-
-
-
- slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)];
-
- slider.maximumValue=100;
-
- slider.minimumValue=0;
-
- slider.value=20;
-
- slider.backgroundColor=[UIColor blackColor];
-
- [slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged];
-
-
-
-
-
- textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)];
-
-
- textfield.text = @"这是一个输入框";
-
- textfield.placeholder = @"请在输入框是输入信息";
-
- textfield.textAlignment = UITextAlignmentLeft;
-
- textfield.textColor = [UIColor grayColor];
-
- textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17];
-
- textfield.borderStyle = 3;
-
- textfield.clearsOnBeginEditing = YES;
-
- textfield.backgroundColor = [UIColor blackColor];
-
-
-
-
- imageview = [[UIImageView alloc] initWithFrame:
- CGRectMake(100, 200, 120, 120)];
-
-
- [imageview setImage:[UIImage imageNamed:@"temp.jpg"]];
-
-
- backgroudButton=[[UIButton alloc] init];
-
- backgroudButton.frame = self.view.frame;
-
- [backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];
-
-
-
- [self.view setBackgroundColor:[UIColor blackColor]];
-
-
-
-
- [self.view addSubview:backgroudButton];
- [self.view addSubview:label];
- [self.view addSubview:imageview];
- [self.view addSubview:button];
- [self.view addSubview:slider];
- [self.view addSubview:textfield];
-
-
-
-
- [imageview release];
- [label release];
- [slider release];
- [textfield release];
-
- }
-
- - (void)ButtonPressed
- {
-
-
- UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"欢迎一起学习IPHONE开发" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
-
- [alertA addButtonWithTitle:@"取消"];
-
- [alertA show];
-
- [alertA release];
-
- }
-
-
- - (void)valueChangeTest
- {
-
- float value = [slider value];
- NSLog(@"进度条已经发生改变:%f",value);
-
- }
-
- -(void)ButtonClick
- {
-
- [textfield resignFirstResponder];
- }
-
-
-
- - (void)viewDidUnload
- {
- [super viewDidUnload];
-
-
- }
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
-
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
-
- @end