一、ios開發中圖片的加載
(一)非retina屏幕
(1)3.5 inch(320 x 480)
* home.png
(二)retina屏幕
(1)3.5 inch(640 x 960)
*
[email protected]
(2)4.0 inch(640 x 1136)
*
[email protected](如果home是程序的啟動圖片,才支持自動加載)
(三)舉例(以下情況都是系統自動加載)
(1)home是啟動圖片
* iPhone 1/3G/3GS -- 3.5 inch 非retina :home.png
* iPhone 4/4S -- 3.5 inch retina :
[email protected]
* iPhone 5/5S/5C -- 4.0 inch retina :
[email protected]
(2)home不是啟動圖片
* iPhone 1/3G/3GS -- 3.5 inch 非retina :home.png
* iPhone 4/4S -- 3.5 inch retina :
[email protected]
* iPhone 5/5S/5C -- 4.0 inch retina :
[email protected]
(3)總結
* home.png :3.5 inch 非retina
*
[email protected] :retina
*
[email protected] :4.0 inch retina + 啟動圖片
二、分享和開始微博功能的實現
1.實現效果
2.實現代碼
1 //
2 // YYNewfeatureViewController.m
3 // 06-微博版本新特性
4 //
5
6 #import "YYNewfeatureViewController.h"
7 #import "YYTabBarViewController.h"
8 #define YYNewfeatureImageCount 4
9 @interface YYNewfeatureViewController ()<UIScrollViewDelegate>
10 @property(nonatomic,strong)UIPageControl *pageControl;
11
12 @end
13
14 @implementation YYNewfeatureViewController
15
16
17 - (void)viewDidLoad
18 {
19 [super viewDidLoad];
20 //1.添加UIScrollView
21 [self setupScrollView];
22 //2.添加pageControl
23 [self setupPageControl];
24 }
25 /**
26 *添加UIScrollVie
27 */
28 -(void)setupScrollView
29 {
30 //1.添加UIScrollVie
31 //創建
32 UIScrollView *scrollView=[[UIScrollView alloc]init];
33 //設置frame
34 scrollView.frame=self.view.bounds;
35 //設置代理
36 scrollView.delegate=self;
37 //添加到view
38 [self.view addSubview:scrollView];
39
40 //2.添加圖片
41 //設置每張圖片的寬高和scrollView的一致
42 CGFloat imageW=scrollView.width;
43 CGFloat imageH=scrollView.height;
44 //添加四張圖片
45 for (int i=0; i<YYNewfeatureImageCount; i++) {
46 //創建ImageView
47 UIImageView *imageView=[[UIImageView alloc]init];
48 NSString *name=[NSString stringWithFormat:@"new_feature_%d",i+1];
49 // if ([UIScreen mainScreen].bounds.size.height==568.0) {
50 // name=[name stringByAppendingString:@"-568h"];
51 // }
52 if (FourInch) {//需要手動去加載4英寸對應的-568h@2x圖片
53 name=[name stringByAppendingString:@"-568h"];
54 }
55 imageView.image=[UIImage imageWithName:name];
56
57 //把ImageView添加到scrollView上
58 [scrollView addSubview:imageView];
59
60 //設置imageView的frame
61 imageView.y=0;
62 imageView.width=imageW;
63 imageView.height=imageH;
64 imageView.x=i*imageW;
65
66 if(i==YYNewfeatureImageCount-1)
67 {
68 [self setupLastImageView:imageView];
69 }
70 }
71 //設置其他的屬性
72 //設置活動范圍
73 scrollView.contentSize=CGSizeMake(YYNewfeatureImageCount*imageW, 0);
74 //設置背景顏色
75 scrollView.backgroundColor=YYColor(246, 246, 246);
76 //隱藏水平滾動條
77 scrollView.showsHorizontalScrollIndicator=NO;
78 // scrollView.pagingEnabled=YES;
79 //去除彈簧效果
80 scrollView.bounces=NO;
81 }
82
83 /**
84 *2.添加pageControl
85 */
86 -(void)setupPageControl
87 {
88 UIPageControl *pageControl=[[UIPageControl alloc]init];
89 //設置一共有幾頁
90 pageControl.numberOfPages=YYNewfeatureImageCount;
91 //設置顯示的位置
92 pageControl.centerX=self.view.width*0.5;
93 pageControl.centerY=self.view.height-30;
94 //把pageControl添加到view上
95 [self.view addSubview:pageControl];
96
97 //設置圓點的顏色
98 //當前頁的圓點的顏色
99 pageControl.currentPageIndicatorTintColor=YYColor(253, 98, 42);
100 //其它葉的圓點的顏色
101 pageControl.pageIndicatorTintColor=YYColor(189, 189, 189);
102 self.pageControl=pageControl;
103
104 }
105
106
107 /**
108 * 設置最後一個UIImageView,在上面添加兩個按鈕
109 */
110 -(void)setupLastImageView:(UIImageView *)imageView
111 {
112 //設置為可交互的
113 imageView.userInteractionEnabled=YES;
114 //1.添加開始微博按鈕
115 [self setupStarButton:imageView];
116 //2.添加分享按鈕
117 [self setupShareButton:imageView];
118 }
119
120 /**
121 * 開始微博按鈕
122 */
123 -(void)setupStarButton:(UIImageView *)imageView
124 {
125 // 1.添加開始按鈕
126 UIButton *startButton = [[UIButton alloc] init];
127 [imageView addSubview:startButton];
128
129 // 2.設置背景圖片
130 [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
131 [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
132
133 // 3.設置frame
134 startButton.size = startButton.currentBackgroundImage.size;
135 startButton.centerX = self.view.width * 0.5;
136 //注意:這是為了適配3.5inch和4.0inch
137 startButton.centerY = self.view.height * 0.8;
138
139 // 4.設置文字
140 [startButton setTitle:@"開始微博" forState:UIControlStateNormal];
141 [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
142 [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
143
144 }
145 -(void)start
146 {
147
148 //顯示主控制器
149 YYTabBarViewController *vc=[[YYTabBarViewController alloc]init];
150
151 //切換控制器(3)
152 //push(當前沒有導航控制器,但是可以添加一個)
153 // [self.navigationController pushViewController:vc animated:NO];
154 //modal
155 // [self presentViewController:vc animated:NO completion:Nil];
156 //window.rootViewController
157 // self.view.window注意,不要用這種方法去獲取主窗口
158 [UIApplication sharedApplication].keyWindow.rootViewController=vc;
159 }
160
161 /**
162 *分享給大家按鈕
163 */
164 -(void)setupShareButton:(UIImageView *)imageView
165 {
166 //1.創建並添加分享按鈕
167 UIButton *shareButton=[[UIButton alloc]init];
168 [imageView addSubview:shareButton];
169
170 //2.設置文字和圖片等信息
171 [shareButton setTitle:@"分享給大家" forState:UIControlStateNormal];
172 [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
173 [shareButton setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
174 [shareButton setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
175 //監聽點擊事件
176 [shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
177
178 //3.設置按鈕的frame
179 shareButton.size=CGSizeMake(150, 35);
180 shareButton.centerX=self.view.width*0.5;
181 shareButton.centerY=self.view.height*0.7;
182
183 //4.設置間距
184 shareButton.titleEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
185 }
186
187 -(void)share:(UIButton *)shareButton
188 {
189 //對狀態進行取反
190 shareButton.selected=!shareButton.isSelected;
191 }
192 #pragma mark-UIScrollViewDelegate
193 -(void)scrollViewDidScroll:(UIScrollView *)scrollView
194 {
195 // YYLog(@"scrollViewDidScroll----%f",scrollView.contentOffset.x);
196 //拿到浮點數進行四捨五入
197 double doublePage=scrollView.contentOffset.x/scrollView.width;
198 int intPage=(int)(doublePage + 0.5);
199 //設置當前頁碼
200 self.pageControl.currentPage=intPage;
201
202 }
203 #pragma mark-隱藏狀態欄
204 -(BOOL)prefersStatusBarHidden
205 {
206 return YES;
207 }
208 @end
3.代碼說明
(1)手動加載-568h@2x的圖片
1 // if ([UIScreen mainScreen].bounds.size.height==568.0) {
2 // name=[name stringByAppendingString:@"-568h"];
3 // }
4 if (FourInch) {//需要手動去加載4英寸對應的-568h@2x圖片
5 name=[name stringByAppendingString:@"-568h"];
6 }
宏定義:
//是否為4英寸
#define FourInch ([UIScreen mainScreen].bounds.size.height==568.0)
(2)設置scrollView的細節
設置活動范圍 scrollView.contentSize=CGSizeMake(YYNewfeatureImageCount*imageW, 0);
設置背景顏色 scrollView.backgroundColor=YYColor(246, 246, 246);
隱藏水平滾動條 scrollView.showsHorizontalScrollIndicator=NO;
去除彈簧效果 scrollView.bounces=NO;
(3)設置頁碼
1 #pragma mark-UIScrollViewDelegate
2 -(void)scrollViewDidScroll:(UIScrollView *)scrollView
3 {
4 // YYLog(@"scrollViewDidScroll----%f",scrollView.contentOffset.x);
5 //拿到浮點數進行四捨五入
6 double doublePage=scrollView.contentOffset.x/scrollView.width;
7 int intPage=(int)(doublePage + 0.5);
8 //設置當前頁碼
9 self.pageControl.currentPage=intPage;
10
11 }