您的位置:首页 > 移动开发 > IOS开发

iOS实践03

2016-03-01 22:01 507 查看
  主要目标:版本新特性界面,新浪授权界面(登录界面)的处理

  任务基本完成了,基本的框架也就到这了,接下来的应该是首页获取微博了。

  1.版本新特性,可以单独作为一个model,写完之加入到项目中。我们新建一个mvc方式的分组Newpart,主要应用的就是那个scrollview,有点类似于广告轮播,这个换成手动的切换图片。

  2.在appdelegate中将window的根控制器换成newpartController,完成效果后在处理他们之间的逻辑。

  3.添加UIScrollView来实现滚动,添加图片,设置scroll的滚动属性(代码:009)

// 009
// 新特性控制器的scroll设置
- (void)viewDidLoad
{
[super viewDidLoad];

// 初始化scroll
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
// 设置scroll的代理
scroll.delegate = self;
// 添加到view上
[self.view addSubview:scroll];

// 添加图片
CGFloat imageW = self.view.frame.size.width;
CGFloat imageH = self.view.frame.size.height;
for (int index = 0; index < SVNewPAartImageCount; index++) {
UIImageView *imageView = [[UIImageView alloc] init];

NSString *imageName = [NSString stringWithFormat:@"new_feature_%d-568h@2x", index+1];
imageView.image = [UIImage imageNamed:imageName];

CGFloat imageX = index * imageW;
imageView.frame = CGRectMake(imageX, 0, imageW, imageH);

[scroll addSubview:imageView];

}

// 设置滚动的尺寸
scroll.contentSize = CGSizeMake(imageW * SVNewPAartImageCount, 0);
scroll.showsHorizontalScrollIndicator = NO;  // 滚动条
scroll.pagingEnabled = YES; // 分页
scroll.bounces = NO;
}


  4.添加pagecontrol(代码:010),使用scroll的代理完成pagecotrol的变换(代码:011)

// 010
// 添加pagecontrol
- (void)addPagecontrol
{
// 初始化page
UIPageControl *pageControl = [[UIPageControl alloc] init];
// 设置属性
pageControl.numberOfPages = SVNewPAartImageCount;
CGFloat centerX = self.view.frame.size.width * 0.5;
CGFloat centerY = self.view.frame.size.height * 0.95;
pageControl.center = CGPointMake(centerX, centerY);
pageControl.bounds = CGRectMake(0, 0, 150, 40);
pageControl.userInteractionEnabled = NO;
// 添加page
[self.view addSubview:pageControl];
self.pageControl = pageControl;

// 更改圆点的颜色
pageControl.currentPageIndicatorTintColor = SLColor(253, 98, 42);
pageControl.pageIndicatorTintColor = SLColor(189, 189, 189);
}


// 011
// 实现page的变换
#pragma mark UIScrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 取出水平方向滚动的距离
CGFloat offsetX = scrollView.contentOffset.x;
// 计算页码
double pageDouble = offsetX / scrollView.frame.size.width;
int pageInt = (int)(pageDouble + 0.5);
self.pageControl.currentPage = pageInt;
}


  5.为最后一张图片添加分享复选框和开始微博按钮(代码:012),实现从新特性跳转到首页(代码:013)

// 012
- (void)setupLastImageView:(UIImageView *)imageView
{
// 让imageview可以和用户进行交互
imageView.userInteractionEnabled = YES;
// 添加开始微博按钮
UIButton *startBtn = [[UIButton alloc] init];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
// 设置frame
CGFloat centerX = imageView.frame.size.width * 0.5;
CGFloat centerY = imageView.frame.size.height * 0.6;
startBtn.center = CGPointMake(centerX, centerY);
startBtn.bounds = (CGRect){CGPointZero, startBtn.currentBackgroundImage.size};
// 设置文字
[startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
[startBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// 绑定事件
[startBtn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:startBtn];

// 添加选择框
UIButton *checkBox = [[UIButton alloc] init];
checkBox.selected = YES;
[checkBox setTitle:@"分享给大家" forState:UIControlStateNormal];
[checkBox setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
[checkBox setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
checkBox.bounds = CGRectMake(0, 0, 200, 50);
CGFloat checkboxcenterX = centerX;
CGFloat checkboxcenterY = imageView.frame.size.height * 0.5;
checkBox.center = CGPointMake(checkboxcenterX, checkboxcenterY);
[checkBox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
checkBox.titleLabel.font = [UIFont systemFontOfSize:15];
[checkBox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
//    checkbox.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
checkBox.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
//    checkbox.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
[imageView addSubview:checkBox];
}


// 013
- (void)start
{
// 显示状态栏
[UIApplication sharedApplication].statusBarHidden = NO;
// 切换窗口的根控制器
self.view.window.rootViewController = [[SVTabbarController alloc] init];
}
- (void)checkboxClick:(UIButton *)checkbox
{
checkbox.selected = !checkbox.isSelected;
}


  6.存储版本信息,当不是第一次安装本软件的时候不显示新特性界面,在appdelegate中完成存储和判断,(代码:014)

// 014
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// 设置窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

// 版本信息的键值
NSString *key = @"CFBundleVersion";
// 取出沙盒中是上次存储的软件版本信息
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lastVersion = [defaults stringForKey:key];
// 获得但前版本
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];

if ([currentVersion isEqualToString:lastVersion]) {
// 显示状态栏
[UIApplication sharedApplication].statusBarHidden = NO;
self.window.rootViewController = [[SVTabbarController alloc] init];
} else { // 新版本
self.window.rootViewController = [[SVNewpartController alloc] init];
// 存储新版本
[defaults setObject:currentVersion forKey:key];
[defaults synchronize];
}
// 显示窗口
[self.window makeKeyAndVisible];

return YES;
}


  7.微博的Author授权,首先要成为新浪的开发者,这个很好申请的,我这个上次就已经弄好了,这次就直接用了。

  8.这个授权也可以作为一个模块但度拿出来。想以后要是使用微博分享自己应用的一些动态就可以把这儿授权模块直接该一下就好,我们新建一个mvc组Author。

  9.新浪的授权也是一个网页的形式,所以要用到webview,加载页面的时候iOS9也许会报错,是因为iOS要求使用更安全的https,需要在info.plist中加入一句话,(代码:特殊0)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</plist>


  10.登陆后,通过webView的代理截取返回的code,并通过code换取accesstoken,在换取accesToken的方法中使用AFN发送请求

  11.为了实现模型编程,新建一个用户账号模型SVAccount,为了优化账号的存取,添加一个AccountTool类,用来将用户归档保存。为后面的用户是否已经登录判断做基础

  12.软件启动顺序:(图004),添加工具类weiboTool选择app的进入界面,在appdelegate中的判断就少了很多(代码015)



// 015
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 设置窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 显示窗口
[self.window makeKeyAndVisible];

// 先判断有无存储账号信息
SVAccount *account = [SVAccountTool account];
if (account) { // 之前登录成功
[SLWeiboTool chooseRootController];
} else { // 之前没有登录成功
self.window.rootViewController = [[SVAuthorController alloc] init];
}
return YES;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: