iOS开发项目篇—09新版本特性·分享

iOS开发项目篇—09新版本特性·分享和开始

一、ios开发中图片的加载

图片的加载:

[UIImage imageNamed:@"home"];  加载png图片

(一)非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英寸对应的[email protected]图片
 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)手动加载[email protected]的图片

1 //        if ([UIScreen mainScreen].bounds.size.height==568.0) {
2 //            name=[name stringByAppendingString:@"-568h"];
3 //        }
4         if (FourInch) {//需要手动去加载4英寸对应的[email protected]图片
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 }

三、补充说明

1.关于边距的设置

原始的图片:

(1)contentEdgeInsets : 切掉按钮内部的内容,内边距=自切   被切掉的区域不能再显示内容

   shareButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);

  设置效果如下:

(2)titleEdgeInsets : 切掉按钮内部UILabel的内容

 shareButton.titleEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);

设置效果如下:

(3)imageEdgeInsets : 切掉按钮内部UIImageView的内容

shareButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);

设置效果如下:

2.切换控制器

有下面三种方式可以切换控制器:

(1)设置Window的根控制器(注意这里不推荐使用通过self.view.window获取window)

UIWindow *window = [UIApplication sharedApplication].keyWindow;

window.rootViewController = vc;

提示:可以销毁之前的控制器,显示新的控制器

(2)使用push切换

push : [self.navigationController pushViewController:vc animated:NO];

提示:无法销毁之前的控制器,显示新的控制器

(3)通过modal切换

modal : [self presentViewController:vc animated:NO completion:nil];

提示:无法销毁之前的控制器,显示新的控制器

(4)三种切换方式的比较

切换之前在内存中的表现如下图所示:

说明:新特性控制器是window的根控制器,所以,新特性控制器的view此时添加到window的view上。

使用modal的方式(之前的控制器无法被销毁):

  这里的控制器切换是不可逆的,也就是说当切换之后,将不会再切换回去。也就是说新特性控制器在切换之后,就没有继续留在内存中的理由了,但是通过上图可以看出,新特性的控制器因为有强指针引用着它,所以它将不会被销毁。此方法不被推荐(使用push也不会销毁)。

四、附录

项目的pch文件

 1 #import <Availability.h>
 2
 3 #ifndef __IPHONE_5_0
 4 #warning "This project uses features only available in iOS SDK 5.0 and later."
 5 #endif
 6
 7 #ifdef __OBJC__
 8     #import <UIKit/UIKit.h>
 9     #import <Foundation/Foundation.h>
10     #import "UIImage+Extension.h"
11     #import "UIBarButtonItem+Extension.h"
12     #import "UIView+Extension.h"
13
14 #ifdef DEBUG // 调试状态, 打开LOG功能
15 #define YYLog(...) NSLog(__VA_ARGS__)
16 #else // 发布状态, 关闭LOG功能
17 #define YYLog(...)
18 #endif
19
20 // 颜色
21 #define YYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
22
23 // 随机色
24 #define YYRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
25
26 // 是否为iOS7
27 #define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)
28
29 //是否为4英寸
30 #define FourInch ([UIScreen mainScreen].bounds.size.height==568.0)
31 #endif

iOS开发项目篇—09新版本特性·分享

时间: 2024-08-03 20:30:18

iOS开发项目篇—09新版本特性·分享的相关文章

iOS开发项目篇—08新版本特性&#183;SrollView

iOS开发项目篇—08新版本特性 一.简单说明 展示新版本的特性:第一次使用一些应用软件时,展示这个版本的软件的新特性,一般在第一次启动程序的时候显示. 1.什么情况下显示版本新特性: (1)第一次使用某个软件时(X) (2)第一次使用某个版本时,显示版本新特性(V) 2.怎么知道这个软件的版本呢? 在plist文件里,bundle version中显示版本号. 3.如何显示版本新特性? 应该在YYAppDelegate.m中进行判断: 如果是第一次使用这个版本,那么就显示版本新特性(设置为wi

iOS开发项目篇—20存储账号信息

iOS开发项目篇—20存储账号信息 一.简单说明 1.不论请求是否成功,都在发送Post请求后,隐藏遮罩. 2.在授权成功后,切换根控制器. (1)说明 切换到控制器有几种情况,要么是切换到新特性,要么是切换到“首页”. 没有必要每次进入程序都需要登录,且每次返回的数据都是一样的,所以只需要把拿到的信息保存到沙盒里就可以了. 判断上次有没有登录成功(把拿到的access_token保存到沙盒中,如果沙盒中有access_token,说明上次登录成功),如果上次登陆成功,那么就判断是否要进入新特性

iOS开发项目篇—21抽取工具类

iOS开发项目篇—21抽取工具类 一.抽取宏 把和应用相关的信息抽取出来 App Key:1972915028 App Secret:b255603c4dfd82b4785bf9a808ce2662 回调地址:http://www.cnblogs.com/wendingding/ (1)appkey和回调页面在很多地方都要用到 (2)如果是不同应用的话,只需要把这几个参数换掉就可以了.把它们抽取成一个宏,写到pch文件中. 项目的PCH文件 1 #import <Availability.h>

iOS开发项目篇—16OAuth授权简介

iOS开发项目篇—16OAuth授权简介 一.资源的授权 在互联网行业,公司要能长期存活下来,用户量很重要,比如腾讯.新浪,它们的用户量是非常巨大的 要想长期留住用户,用户资源(数据)的管理也很重要,如果你经常在不经过用户同意的情况下,把用户的一些资源共享出去,那肯定是留不住用户的,甚至会遭到法律的制裁 但是,有时候确实要把某些用户资源共享出去,比如第三方想访问用户的QQ数据.第三方想访问用户的新浪微博数据 要想把用户资源共享出去,就必须取得用户的同意,那么这里就有个资源授权的问题 资源授权的方

iOS开发项目篇—28自定义UITextView

iOS开发项目篇—28自定义UITextView 一.简单说明 1.要实现的效果 2.分析 (1)UITextField 1.最多只能输入一行文字 2.能设置提醒文字(placehoder) 3.不具备滚动功能 (2)UITextView 1.能输入N行文字(N>0) 2.不能设置提醒文字(没有placehoder属性) 3.具备滚动功能 需求:技能输入多行文字,又具备文字提醒功能. 这里选择自定义一个类,让其继承自UITextView类,为其添加一个设置文字提醒的功能. 二.实现 自定义UI控

iOS开发项目篇—29自定义工具条

iOS开发项目篇—29自定义工具条 一.简单说明 1.实现效果: 2.实现思路: (1)尝试: 1 //添加子控件 2 -(void)setupTextView 3 { 4 //1.创建输入控件 5 YYTextView *textView=[[YYTextView alloc]init]; 6 //设置frame 7 textView.frame=self.view.bounds; 8 [self.view addSubview:textView]; 9 self.textView=textV

iOS开发项目篇-01环境搭建

iOS开发项目篇-01环境搭建 一.基本过程 1.新建一个项目 (1)删除storyboard (2)在配置界面中,把main  删除,手动设置 2.准备素材 图片素材如下: 这个项目是模仿新浪,实现一个山寨的新浪微博系统,新版本的系统中已经放弃了非视网膜屏幕. 说白了,这种情况下,如果手机是3.5英寸的ios7系统,则看不到该启动动画. 设置方法,右键单击show in finder,修改json文件. Contents.json,对文件进行修改,让系统启动画面对3.5英寸的ios7系统提供支

iOS开发项目篇—35封装网络请求

iOS开发项目篇—35封装网络请求 一.简单说明 1.分析项目对网路请求(AFN框架)的依赖 项目中,多个控制器都使用了AFN框架发送网络请求,如果AFN2.0存在重大BUg,或者是升级至3.0版本,那么对于整个项目都是及其危险的,所有用到AFN的地方都需要做出相应的修改. 另外,如果现在要求不再使用AFN框架,而是使用一个新的框架,那么有关AFN的依赖所关联的所有代码都需要重新来过. 如果把afn这个第三方框架从项目中删除的话,那么项目就相当于作废了,这就是项目对第三方框架的强依赖的体现. 说

iOS开发项目篇—33发微博

iOS开发项目篇—33发微博 一.简单说明 1.发送按钮 当textView的文字发生改变(有内容)的时候,设置导航栏右侧的按钮为可点击的. 说明:监听内容的改变,既可以使用通知来实现,也可以使用代理来实现(下面使用的是代理的方式) 代码说明: 1 #pragma mark-设置代理方法 2 /** 3 *当textView的内容改变的时候,通知导航栏“发送”按钮为可用 4 */ 5 -(void)textViewDidChange:(UITextView *)textView 6 { 7 se