大綱
一剩蟀、作業(yè)Homework_CircleMove0314
理論:不可在分線程中刷新UI,否則無法實現(xiàn)預期界面效果切威。
二育特、UINavigationController
項目:UINavigationController0315
理論:
棧:后進先出 隊列:先進先出
導航(NavController):繼承于UIViewController,根據(jù)棧的原理先朦,實現(xiàn)多個視圖的之間的切換功能缰冤。
界面跳轉(zhuǎn)方法共4種:①root②模態(tài)彈出③
④UINavigationController:
步驟:
1.創(chuàng)建ViewController
2.創(chuàng)建NavigationController,且設置vc為nav的root
3.將nav設置為window的root
進棧:pushViewController: animated:
出棧:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController
三喳魏、Navigation_Title_Button
項目:Navigation_Title_Button0315
理論:
1.顯示導航欄標題
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title
2.添加導航按鈕
2.1 系統(tǒng)提供的方式
①initWithBarButtonSystemItem: target: action:
②initWithTitle: style: target: action:
③initWithImage: style: target: action:
2.2 自定義方式
步驟:
1.創(chuàng)建button棉浸,設置其bounds/action等
2.聲明UIBarButtonItem,并initWithCustomView:button
3.將button添加到nav
四截酷、隱藏導航欄
項目:NavigationBar0315
理論:
設置導航隱藏:self.navigationController.navigationBarHidden = YES;
位置:一般設置在viewWillAppear:
五、使用storyBoard創(chuàng)建導航欄
項目:Navigation_UseStoryBoard0315
理論:
1.選中storyboard
2.點擊Editor→Embed In→Navigation Controller
正文
一乾戏、作業(yè)Homework_CircleMove0314
理論:不可在分線程中刷新UI迂苛,否則無法實現(xiàn)預期界面效果。
源碼:
- (void)viewDidLoad
{
[super viewDidLoad];
//1.在圓心添加太陽
UIImageView *sun = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fireball"]];
sun.center = CGPointMake(CENTER_X, CENTER_Y);
sun.bounds = CGRectMake(0, 0, 60, 60);
[self.view addSubview:sun];
//2.在圓周上添加地球
UIImageView *earth = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"a"]];
earth.center = CGPointMake(CENTER_X+RADIOUS, CENTER_Y);
earth.bounds = CGRectMake(0, 0, 60, 60);
[self.view addSubview:earth];
//3.開啟分線程
[NSThread detachNewThreadSelector:@selector(newThread:) toTarget:self withObject:earth];
}
//分線程
//處理耗時數(shù)據(jù)
- (void)newThread:(UIImageView *)earth
{
//無限循環(huán)
for (; ; )
{
[NSThread sleepForTimeInterval:0.01];
//根據(jù)角度鼓择,計算x三幻,y坐標
static float angle = 0;
angle += 1;
float huDu = angle / 180 * M_PI;
_x = CENTER_X + RADIOUS * cos(huDu);
_y = CENTER_Y + RADIOUS * sin(huDu);
[self performSelectorOnMainThread:@selector(refreshUI:) withObject:earth waitUntilDone:NO];
}
}
//主線程
//刷新UI
- (void)refreshUI:(UIImageView *)earth
{
earth.center = CGPointMake(_x, _y);
}
二、UINavigationController
項目:UINavigationController0315
理論:
棧:后進先出 隊列:先進先出
導航(NavController):繼承于UIViewController呐能,根據(jù)棧的原理念搬,實現(xiàn)多個視圖的之間的切換功能抑堡。
界面跳轉(zhuǎn)方法共4種:①root②模態(tài)彈出③
④UINavigationController:
步驟:
1.創(chuàng)建ViewController
2.創(chuàng)建NavigationController,且設置vc為nav的root
3.將nav設置為window的root
進棧:pushViewController: animated:
出棧:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController
源碼:
文件:AppDelegate.m
//1.創(chuàng)建視圖控制器
ViewController1 *vc1 = [[ViewController1 alloc]init];
//2.創(chuàng)建導航控制器,設置vc1為導航的根視圖控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc1];
//3.將nav作為window的根視圖控制器
self.window.rootViewController = nav;
文件:ViewController1.m
//下一頁
//從vc1跳轉(zhuǎn)到vc2朗徊,實現(xiàn)壓棧(push/視圖換入)
- (IBAction)nextClick:(UIButton *)sender
{
//1.創(chuàng)建vc2
ViewController2 *vc2 = [[ViewController2 alloc]init];
//2.壓棧首妖,push
[self.navigationController pushViewController:vc2 animated:YES];
}
文件:ViewController2.m
//從vc2跳轉(zhuǎn)到vc1,實現(xiàn)視圖的換出(pop/出棧)
//由3種方式
- (IBAction)backClick:(UIButton *)sender
{
//pop
//方法1:直接返回到上一級界面
[self.navigationController popViewControllerAnimated:YES];
//方法2:返回到根視圖控制器
[self.navigationController popToRootViewControllerAnimated:YES];
//方法3:
[self.navigationController popToViewController: animated:YES];
}
//跳轉(zhuǎn)到vc3
- (IBAction)nextClick:(UIButton *)sender
{
ViewController3 *vc3 = [[ViewController3 alloc]init];
[self.navigationController pushViewController:vc3 animated:YES];
}
文件:ViewController4.m
//vc4到vc2
- (IBAction)backClick:(UIButton *)sender
{
//第三種pop
//先找到vc2
//1.獲取數(shù)組
NSArray *vcArr = [self.navigationController viewControllers];
//2.獲取vc2
ViewController2 *vc2 = [vcArr objectAtIndex:1];
//3.popTo vc2
[self.navigationController popToViewController:vc2 animated:YES];
}
三爷恳、Navigation_Title_Button
項目:Navigation_Title_Button0315
理論:
1.顯示導航欄標題
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title
2.添加導航按鈕
2.1 系統(tǒng)提供的方式
1>initWithBarButtonSystemItem: target: action:
2>initWithTitle: style: target: action:
3>initWithImage: style: target: action:
2.2 自定義方式
步驟:
1.創(chuàng)建button有缆,設置其bounds/action等
2.聲明UIBarButtonItem,并initWithCustomView:button
3.將button添加到nav
源碼:
//1.顯示導航標題
//方法1:
//title:Localized title for use by a parent controller
self.title = @"主題";
//方法2:
//navigationItem:
//The navigation item used to represent the view controller in a parent’??s navigation bar. (read-only)
self.navigationItem.title = @"設置";
//2.添加導航按鈕
//2.1 系統(tǒng)提供的幾種方式
//(1)系統(tǒng)圖形按鈕
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(photoClick:)];
//self.navigationItem.rightBarButtonItem = item1;
//(2)文字按鈕
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
// self.navigationItem.leftBarButtonItem = item2;
//(3)圖片按鈕
//創(chuàng)建圖片
UIImage *image = [UIImage imageNamed:@"scratch"];
//設置圖片渲染模式
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//使用渲染過后的圖片
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(animationButton)];
//創(chuàng)建item數(shù)組
NSArray *itemArr = [[NSArray alloc]initWithObjects:item1,item3, nil];
//將item數(shù)組賦給nav
self.navigationItem.rightBarButtonItems = itemArr;
//2.2 自定義
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//注意:位置坐標不起作用
// button.center = CGPointMake(100, 30);
button.bounds = CGRectMake(0, 0, 40, 40);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithCustomView:button];
NSArray *itemArr2 = [[NSArray alloc]initWithObjects:item2,item4, nil];
self.navigationItem.leftBarButtonItems = itemArr2;
四捶障、隱藏導航欄
項目:NavigationBar0315
理論:
設置導航隱藏:self.navigationController.navigationBarHidden = YES;
位置:一般設置在viewWillAppear:
源碼:
//在將要顯示時鸠珠,設置Hidden
//而不是在viewDidLoad設置
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
五掠河、使用storyBoard創(chuàng)建導航欄
項目:Navigation_UseStoryBoard0315
理論:
1.選中storyboard
2.點擊Editor→Embed In→Navigation Controller