在眾多移動(dòng)應(yīng)?用中,能看到各式各樣的表格數(shù)據(jù) 。
在iOS中,要實(shí)現(xiàn)表格,數(shù)據(jù),圖片添加,按鈕點(diǎn)擊等方法的展示,最常用的做法就是使用UI中的基本控件來(lái)完成 。
首先在AppDelegate.m里初始化window:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release]; //釋放window
[super dealloc];
}
#pragma make -- app啟動(dòng)完成走這個(gè)方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#//初始化Window對(duì)象
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
#//設(shè)置顏色:
self.window.backgroundColor = [UIColor whiteColor];
#//讓window可見:
[self.window makeKeyAndVisible];
#//為window創(chuàng)建根視圖控制器:
UIViewController *vc = [[UIViewController alloc] init];
#//將vc設(shè)置為window的根視圖控制器:
self.window.rootViewController = vc;
UILable : (標(biāo)簽控件的常用屬性)
//初始化標(biāo)簽控件:
UILabel *label = [[UILabel alloc]
initWithFrame:CGRectMake(100, 200, 175, 100)];
//設(shè)置背景顏色:
label.backgroundColor = [UIColor grayColor];
//設(shè)置label要顯示的文字:
label.text = @"Hello student!";
//設(shè)置文字對(duì)齊方式:(左中右三種 0.1.2)
label.textAlignment = NSTextAlignmentCenter; //給1也行
//設(shè)置字體顏色:
label.textColor = [UIColor yellowColor];
//設(shè)置字體和大小:
// label.font = [UIFont fontWithName:@"Zapfino" size:20];
//設(shè)置顯示的行數(shù)(換行):(0表示自動(dòng)去換行)
label.numberOfLines = 0;
//設(shè)置斷行模式:
label.lineBreakMode = NSLineBreakByWordWrapping; //按
字母用Char換Word
UITextField :(文本框的常用屬性)
//初始化文本框:
UITextField *textfield = [[UITextField alloc]
initWithFrame:CGRectMake(100, 100, 175, 50)];
//設(shè)置提示文字:
textfield.placeholder = @"請(qǐng)輸入密碼:";
//設(shè)置邊框樣式:
textfield.borderStyle = UITextBorderStyleRoundedRect;
//設(shè)置邊框?qū)挾群皖伾?
textfield.layer.borderWidth = 5.0;
textfield.layer.borderColor = [UIColor blueColor].CGColor
//設(shè)置文本顏色:
textfield.textColor = [UIColor redColor];
//設(shè)置文本對(duì)齊方式:(0.1.2 左 中 右)
textfield.textAlignment = 0;
//設(shè)置字體:
textfield.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
#注意字體的書寫一定要標(biāo)準(zhǔn)
//是否允許編輯:
textfield.enabled = YES; //(NO 不可編輯,默認(rèn)為yes)
//開始輸入時(shí)清空之前的內(nèi)容:
textfield.clearsOnBeginEditing = NO; //(NO就清空了,默認(rèn)為 YES)
//設(shè)置密碼輸入模式(小點(diǎn)):
textfield.secureTextEntry = YES; //no為正常輸入
//設(shè)置鍵盤的樣式:
// textfield.keyboardType = UIKeyboardTypeDefault;
// 比如只能輸入數(shù)字textfield.keyboardType = UIKeyboardTypeNumberPad;
//設(shè)置鍵盤右下角按鈕內(nèi)容:
textfield.returnKeyType = UIReturnKeyNext; //有很多種
UIButton :(按鈕的常用屬性)
//初始化按鈕:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//設(shè)置位子
button.frame = CGRectMake(100, 100, 175, 64);
//設(shè)置按鈕文字:
[button setTitle:@"我是按鈕" forState:UIControlStateNormal];
//設(shè)置邊框:
// button.layer.borderWidth = 2.0;
//設(shè)置邊框顏色:
// button.layer.borderColor= [UIColor greenColor].CGColor;
//設(shè)置背景顏色
// button.backgroundColor = [UIColor blackColor];
//設(shè)置圓角:
// button.layer.cornerRadius = 10;
//設(shè)置背景圖:
// [button setBackgroundImage:[UIImage
imageNamed:@"1.jpg"] forState:UIControlStateNormal];
//設(shè)置前景圖
// [button setImage:[UIImage imageNamed:@"666.jpg"]
forState:UIControlStateNormal];
// UIImage *frontImage = [button
imageForState:UIControlStateNormal];
// NSLog(@"%@", frontImage);
// //設(shè)置按鈕字體顏色:
[button setTitleColor:[UIColor redColor]
forState:UIControlStateNormal];
//修改字體大小以及字體:
//修改字體大小以及字體:
button.titleLabel.font = [UIFont fontWithName:@"Zapfino"
size:20];
//獲取字體顏色:
UIColor *wordColor = [button
titleColorForState:UIControlStateNormal];
NSLog(@"%@", wordColor);
//為按鈕button添加點(diǎn)擊事件:
[button addTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];
//為按鈕移除點(diǎn)擊事件
[button removeTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];
//最后將按鈕添加到window上
[view addSubview:button];
#注意:按鈕是系統(tǒng)自動(dòng)釋放 不用再release ,否則將會(huì)過(guò)度釋放
UIImageView :(圖片顯示控件的常用屬性)
//創(chuàng)建UIImageView對(duì)象:
UIImageView *imageView = [[UIImageView
alloc]initWithFrame:self.window.frame];
//設(shè)置顏色:
// imageView.backgroundColor = [UIColor orangeColor];
//為imageView設(shè)置圖片:
// imageView.image = [UIImage imageNamed:@"1.jpg"];
更多圖片的屬性和方法以后會(huì)介紹,這里簡(jiǎn)單為大家做個(gè)植物大戰(zhàn)僵尸中每個(gè)圖片及方法的實(shí)現(xiàn),希望能幫助大家理解:
# 首先我們需要將已有的圖片文件拖到自己的工程里:比如下面
//1).設(shè)置整體背景:
imageView.image = [UIImage
imageNamed:@"BackGround"];
#這里以向日癸為例,僵尸,瓜等同理
(因?yàn)闆](méi)找到符合簡(jiǎn)書規(guī)格的圖片,暫不上傳 sorry)
//2).循環(huán)創(chuàng)建動(dòng)畫素材(UIImage對(duì)象)
NSMutableArray *flowerArray = [NSMutableArray array];
for (NSInteger i = 1; i <= 18; i++) {
//拼接每個(gè)圖片的名字:
NSString *name = [NSString stringWithFormat:@"flower%ld.tiff", i];
UIImage *image = [UIImage imageNamed:name];
[flowerArray addObject:image];
}
//3).創(chuàng)建單獨(dú)的UIImageView對(duì)象
UIImageView *flowerIamgeView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 80, 73, 74)];
[imageView addSubview:flowerIamgeView];
[flowerIamgeView release];
//4).將數(shù)組賦值給imageView
flowerIamgeView.animationImages = flowerArray;//********
//5).設(shè)置播放圖片的時(shí)間間隔(兩張圖片之間播放的時(shí)間差):
flowerIamgeView.animationDuration = 1.0f;
//6).設(shè)置一下重復(fù)次數(shù):
// flowerIamgeView.animationRepeatCount = 5;
#當(dāng)重復(fù)次數(shù)為0時(shí)就一直保持圖片動(dòng)態(tài)播放
//7).開始動(dòng)畫
[flowerIamgeView startAnimating];
//花動(dòng)起來(lái):
[UIView animateWithDuration:10 animations:^{
flowerIamgeView.frame = CGRectMake(200, 400, 146, 148);
}];