国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

[iOSUI進(jìn)階-2.0]彩票Demov1.0

2019-11-14 19:41:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
A.需求
1.模仿“網(wǎng)易彩票”做出有5個(gè)導(dǎo)航頁(yè)面和相應(yīng)功能的Demo
2.v1.0 版本搭建基本框架
 
code source:https://github.com/hellovoidworld/HelloLottery
Image
 
B.搭建基本框架
1.拖入TaBarController,5個(gè)NavigationController和對(duì)應(yīng)的5個(gè)UIViewController
Image(296)
 
2.配置圖標(biāo)和啟動(dòng)畫面
AppIcon直接拖入圖片
LaunchImage在Xcode6中需要先更改啟動(dòng)圖使用圖庫(kù)的圖片,而不是LaunchImage.xib
6A490F17-B44B-454E-B38D-6DA56E05F17E
 
Image(297)
 
Image(298)
 
Image(299)
 
2.引入圖片包
Image(300)
 
4. 按照模塊分類代碼包
Image(301)
 
3.底部導(dǎo)航--自定義TabBar
(1)基本設(shè)計(jì)
Image(302)
 
a.自定義HVWTabBarController、繼承自UIView的HVWTabBar、繼承UIButton的HVWTabBarButton
Image(303)
 
 
b.在自定義TabBar控制器viewDidLoad,刪除原來(lái)的TabBar,加上自定義的TabBar
c.加入自定義的按鈕圖片、選中圖片
d.選中事件,轉(zhuǎn)換圖片
e.按下馬上激發(fā)按鈕激發(fā)
重寫TabBarButton的[button setHighlighted:]取消高亮狀態(tài)
HVWTabBarButton:
1 // 覆蓋setHighlighted,取消點(diǎn)擊時(shí)的高亮狀態(tài)2 - (void)setHighlighted:(BOOL)highlighted {3 //    [super setHighlighted:highlighted];4 }
 
初步實(shí)現(xiàn)都在HVWTabBarController的viewDidLoad方法中:
 1 - (void)viewDidLoad { 2     [super viewDidLoad]; 3     // Do any additional setup after loading the view. 4     5     // 1.刪除原來(lái)的TabBar 6     [self.tabBar removeFromSuperview]; 7     8     // 2.添加自定義TabBar 9     HVWTabBar *hvwTabBar = [[HVWTabBar alloc] init];10     hvwTabBar.frame = self.tabBar.frame;11     hvwTabBar.backgroundColor = [UIColor greenColor]; // 設(shè)置綠色底的tabBar,稍后會(huì)被按鈕圖片覆蓋12     [self.view addSubview:hvwTabBar];13    14     // 3.添加按鈕15     for (int i=0; i<5; i++) {16         // 3.1創(chuàng)建按鈕17         HVWTabBarButton *button = [HVWTabBarButton buttonWithType:UIButtonTypeCustom];18         button.tag = i;19 20         // 3.2設(shè)置按鈕背景圖片21         [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d", i+1]] forState:UIControlStateNormal];22        23         [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel", i+1]]  forState:UIControlStateSelected];24        25         // 3.3設(shè)置frame26         CGFloat buttonWidth = hvwTabBar.frame.size.width / 5;27         CGFloat buttonHeight = hvwTabBar.frame.size.height;28         CGFloat buttonX = i * buttonWidth;29         CGFloat buttonY = 0;30         button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);31        32         // 3.4添加到tabBar33         [hvwTabBar addSubview:button];34        35         // 3.5添加監(jiān)聽事件36         [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];37        38         // 3.6默認(rèn)已經(jīng)點(diǎn)擊了第一個(gè)按鈕39         if (i == 0) {40             [self tabBarButtonClicked:button];41         }42     }43 }44 45 - (void) tabBarButtonClicked:(HVWTabBarButton *) button {46     // 1.取消選中之前的按鈕47     self.selectedButton.selected = NO;48    49     // 2.選中新點(diǎn)擊的按鈕50     button.selected = YES;51    52     // 3.設(shè)置為當(dāng)前選中的按鈕53     self.selectedButton = button;54    55     // 4.切換子控制器56     self.selectedIndex = button.tag;57 }

 

 
Image(304)
 
 
(2)封裝TabBar代碼
Image(305)
a.重寫initWithFrame:創(chuàng)建初始化TabBar
b.移動(dòng)子控件的初始化代碼到layoutSubviews
c.通過(guò)代理轉(zhuǎn)換Navigation頁(yè)面
d.封裝添加按鈕函數(shù)
 1 // 2 //  HWTabBarController.m 3 //  HelloLottery 4 // 5 //  Created by hellovoidworld on 14/12/31. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "HvWTabBarController.h"10 #import "HVWTabBar.h"11 #import "HVWTabBarButton.h"12 13 @interface HVWTabBarController () <HVWTabBarDelegate>14 15 @end16 17 @implementation HVWTabBarController18 19 - (void)viewDidLoad {20     [super viewDidLoad];21     // Do any additional setup after loading the view.22    23     // 1.刪除原來(lái)的TabBar24     [self.tabBar removeFromSuperview];25    26     // 2.添加自定義TabBar27     HVWTabBar *hvwTabBar = [[HVWTabBar alloc] init];28     hvwTabBar.frame = self.tabBar.frame;29     hvwTabBar.backgroundColor = [UIColor greenColor]; // 設(shè)置綠色底的tabBar,稍后會(huì)被按鈕圖片覆蓋30     hvwTabBar.delegate = self;31     [self.view addSubview:hvwTabBar];32 }33 34 - (void)didReceiveMemoryWarning {35     [super didReceiveMemoryWarning];36     // Dispose of any resources that can be recreated.37 }38 39 #PRagma mark - HVWTabBarDelegate 代理方法40 - (void)hvwTabBar:(HVWTabBar *)hvwTabBar didClickedButtonFrom:(int)from to:(int)to {41     // 切換子控制器42     self.selectedIndex = to;43 }44 45 @end
 
 1 // 2 //  HVWTabBar.h 3 //  HelloLottery 4 // 5 //  Created by hellovoidworld on 14/12/31. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import <UIKit/UIKit.h>10 11 @class HVWTabBar;12 13 /** 代理協(xié)議 */14 @protocol HVWTabBarDelegate <NSObject>15 @optional16 - (void) hvwTabBar:(HVWTabBar *) hvwTabBar didClickedButtonFrom:(int) from to:(int) to;17 @end18 19 @interface HVWTabBar : UIView20 21 /** 代理 */22 @property(nonatomic, weak) id<HVWTabBarDelegate> delegate;23 24 @end

 

 1 // 2 //  HVWTabBar.m 3 //  HelloLottery 4 // 5 //  Created by hellovoidworld on 14/12/31. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "HVWTabBar.h"10 #import "HVWTabBarButton.h"11 12 #define HVWTabBarButtonCount 513 14 @interface HVWTabBar()15 16 @property(nonatomic, weak) HVWTabBarButton *selectedButton;17 18 @end19 20 @implementation HVWTabBar21 22 // 重寫initWithFrame方法,添加tabBar按鈕23 - (instancetype)initWithFrame:(CGRect)frame {24     if (self = [super initWithFrame:frame]) {25         [self initButtons];26     }27    28     return self;29 }30 31 /** 初始化按鈕 */32 - (void) initButtons {33     for (int i=0; i<HVWTabBarButtonCount; i++) {34         // 3.1創(chuàng)建按鈕35         HVWTabBarButton *button = [HVWTabBarButton buttonWithType:UIButtonTypeCustom];36         button.tag = i;37        38         // 3.2設(shè)置按鈕背景圖片39         [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d", i+1]] forState:UIControlStateNormal];40        41         [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel", i+1]]  forState:UIControlStateSelected];42        43         // 3.3添加到tabBar44         [self addSubview:button];45        46         // 3.4添加監(jiān)聽事件47         [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];48        49         // 3.5默認(rèn)已經(jīng)點(diǎn)擊了第一個(gè)按鈕50         if (i == 0) {51             [self tabBarButtonClicked:button];52         }53     }54 }55 56 /** 初始化子控件的位置尺寸 */57 - (void)layoutSubviews {58     [super layoutSubviews];59    60     for (int i=0; i<HVWTabBarButtonCount; i++) {61         HVWTabBarButton *button = self.subviews[i];62         CGFloat buttonWidth = self.frame.size.width / 5;63         CGFloat buttonHeight = self.frame.size.height;64         CGFloat buttonX = i * buttonWidth;65         CGFloat buttonY = 0;66         button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);67     }68 }69 70 - (void) tabBarButtonClicked:(HVWTabBarButton *) button {71     // 1.調(diào)用代理方法,通知TabBarController切換子控制器72     if ([self.delegate respondsToSelector:@selector(hvwTabBar:didClickedButtonFrom:to:)]) {73         [self.delegate hvwTabBar:self didClickedButtonFrom:self.selectedButton.tag to:button.tag];74     }75    76     // 2.取消選中之前的按鈕77     self.selectedButton.selected = NO;78    79     // 3.選中新點(diǎn)擊的按鈕80     button.selected = YES;81    82     // 4.設(shè)置為當(dāng)前選中的按鈕83     self.selectedButton = button;84 }85 86 @end
 
4.頭部Navigation導(dǎo)航欄主題設(shè)置
Image(306)
 
Image(307)
 
(1)讓applicatoin管理狀態(tài)欄
(2)Navigation導(dǎo)航欄背景圖片
(3)統(tǒng)一設(shè)置所有Navigation導(dǎo)航欄
[UINavigationBar appearance]; // 所有Navigation導(dǎo)航欄(頭部導(dǎo)航欄)
(4)設(shè)置所有Navigation導(dǎo)航欄字體顏色
(5)根據(jù)系統(tǒng)版本,設(shè)置Navigation導(dǎo)航欄背景圖片
 
(6)在二級(jí)頁(yè)面隱藏底部導(dǎo)航條,重寫導(dǎo)航控制器的push方法
a.自定義一個(gè)導(dǎo)航控制器HVWNavigationController類,重寫push,隱藏底部導(dǎo)航欄
b.設(shè)置為每個(gè)NavigationController的class
c.導(dǎo)航控制器類的initialize只會(huì)在類第一次使用的時(shí)候調(diào)用一次
所以,導(dǎo)航欄的主題設(shè)置可以放在initialize方法中
 
解決:
(1)使用application管理狀態(tài)欄
設(shè)置不使用控制器控制狀態(tài)欄
Image(308)
 
在AppDelegate中設(shè)置:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {2     // Override point for customization after application launch.3    4     // 設(shè)置狀態(tài)欄樣式為白色5     application.statusBarStyle = UIStatusBarStyleLightContent;6    7     return YES;8 }
 
(2)創(chuàng)建自定義NavigationController類,并設(shè)置5個(gè)Navigation控制器的class為此類
Image(309)
 
 
(3)不要?jiǎng)h除原來(lái)的tabBar,而是覆蓋它,才能控制push事件中底部導(dǎo)航欄
HVWTabBarController:
 1 - (void)viewDidLoad { 2     [super viewDidLoad]; 3     // Do any additional setup after loading the view. 4     5     // 1.添加自定義TabBar 6     HVWTabBar *hvwTabBar = [[HVWTabBar alloc] init]; 7     hvwTabBar.frame = self.tabBar.bounds; 8     hvwTabBar.delegate = self; 9    10     // 2.設(shè)置tabBar11     [self.tabBar addSubview:hvwTabBar];12 }
 
(4)在NavigationController中編寫類初始化方法和重寫push方法
 1 // 2 //  HVWNavigationController.m 3 //  HelloLottery 4 // 5 //  Created by hellovoidworld on 15/1/1. 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8  9 #import "HVWNavigationController.h"10 11 12 @interface HVWNavigationController ()13 14 @end15 16 @implementation HVWNavigationController17 18 - (void)viewDidLoad {19     [super viewDidLoad];20     // Do any additional setup after loading the view.21    22 }23 24 - (void)didReceiveMemoryWarning {25     [super didReceiveMemoryWarning];26     // Dispose of any resources that can be recreated.27 }28 29 /** 類初始化方法,僅調(diào)用一次 */30 + (void) initialize {31     // 獲取能夠控制所有NavigationBar的實(shí)例32     UINavigationBar *navBar = [UINavigationBar appearance];33    34     // 設(shè)置背景圖片35     NSString *bgImageName;36     if (iOS7) { // 在HelloLottery-Prefix.pch中定義了判斷iOS版本的全局變量37         bgImageName = @"NavBar64";38     } else {39         bgImageName = @"NavBar";40     }41    42     [navBar setBackgroundImage:[UIImage imageNamed:bgImageName] forBarMetrics:UIBarMetricsDefault];43    44     // 設(shè)置文本45     NSMutableDictionary *attr = [NSMutableDictionary dictionary];46     attr[NSForegroundColorAttributeName] = [UIColor whiteColor];47     attr[NSFontAttributeName] = [UIFont systemFontOfSize:16];48     [navBar setTitleTextAttributes:attr];49 }50 51 // 攔截所有的push操作52 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {53     viewController.hidesBottomBarWhenPushed = YES; // 這是原來(lái)NavigationController中的tabBar,所以要設(shè)置自定義的tabBar為Navigation中的tabBar54     [super pushViewController:viewController animated:YES];55 }56 57 @end58  
 
 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 含山县| 谢通门县| 松滋市| 瑞金市| 马公市| 文山县| 连云港市| 蒙城县| 类乌齐县| 九龙县| 孝义市| 城口县| 汾西县| 曲靖市| 梨树县| 常德市| 姚安县| 吐鲁番市| 安阳市| 海丰县| 汕尾市| 正蓝旗| 台北县| 恭城| 喜德县| 绩溪县| 招远市| 西乌珠穆沁旗| 易门县| 法库县| 淮滨县| 凉城县| 新乡县| 阆中市| 临安市| 永泰县| 二连浩特市| 山丹县| 中西区| 襄汾县| 河池市|