由于公司开发需要,需要滚动每道评测题,

并且一道评测题单项选择,按钮和文字都可点击选中

(单选比多选复杂一点,但是原理差不多)

1.当初任务紧,代码也没有优化,仅供思路参考,先放几张图

2.代码部分 :支持返回代理实现界面刷新重置(忽略代码中的网络请求和统计部分)

//  Created by 刘成利 on 15/12/18.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import "RiskEvaluationViewController.h"
#import "NSString+HexColors.h"
#import "UIFont+Fonts.h"
#import "WxHxD.h"
#import "BlackShowView.h"
#import "RiskReportViewController.h"
#import "V_2_X_Networking.h"
#import "ISMResultModel.h"
#import "MyHomeViewController.h"
#import "LoadingView.h"
#import "BlackShowView.h"
#import "UserInfomation.h"
#import "NetworkingString.h"
#import "UIButton+NormalUsedButton.h"
#import "NSNotificationCenterString.h" @interface RiskEvaluationViewController () <UIScrollViewDelegate, NetworkingDelegate,RiskReportDelegate> @property (nonatomic, strong) UIScrollView *scrollView; // 滚动视图
@property (nonatomic, strong) UILabel *pageChange; // 右上角页码
@property (nonatomic, assign) int pageNumber; // 页码
@property (nonatomic, assign) int XCount; // X值倍数
@property (nonatomic, assign) int btnTag; // 选项tag
@property (nonatomic, strong) UIButton *submit; // 提交按钮
@property (nonatomic, strong) NSArray* textArray1; // 测试题-------
@property (nonatomic, strong) NSArray* textArray2;
@property (nonatomic, strong) NSArray* textArray3;
@property (nonatomic, strong) NSArray* textArray4;
@property (nonatomic, strong) NSArray* textArray5;
@property (nonatomic, strong) NSMutableArray* scoreArray; // 选项分数
@property (nonatomic, strong) UIImageView *currentPiont; // 当前页圆点 @property (nonatomic, strong) V_2_X_Networking *networking;
@property (nonatomic, strong) NSString *riskLevel; // 风险结果
@property (nonatomic, strong) LoadingView *showLoadingView; @end @implementation RiskEvaluationViewController - (void)setup {
[super setup]; self.titleLabel.text = @"风险测评";
self.backgroundView.backgroundColor = [@"f4f4f4" hexColor]; if (self.isRegister == YES)
{
[self.backButton removeFromSuperview];
}
// 初始化基本数据
[self foundationData]; // 创建界面
[self buildView]; } - (void)foundationData{ // 标记
self.pageNumber = ;
self.XCount = ;
self.btnTag = ; // 从110开始 // 测试题
self.textArray1 = @[@"1.您用于证券投资的大部分资金不会用作其它用途的时间段为:",@"短期——0到1年",@"中期——1到5年",@"长期——5年以上"];
self.textArray2 = [NSArray arrayWithObjects:@"2.您家庭预计进行证券投资的资金占家庭现有总资产(不含自助、自用房产及汽车等固定资产。扣除债务)的比例是:",@"10%以下",@"10%——30%",@"30%——50%",@"50%——70%",@"70%以上", nil];
self.textArray3 = [NSArray arrayWithObjects:@"3.以下投资,您会选择哪一项:\n",@"确定赢取100元",@"80%的机会赢取1,000元",@"50%的机会赢取5,000元",@"25%的机会赢取10,000元",@"5%的机会赢取100,000元", nil];
self.textArray4 = [NSArray arrayWithObjects:@"4.您认为自己能承受的最大投资损失是多少?\n",@"不能承受本金亏损",@"10%以内",@"10%——30%",@"30%——50%",@"超过50%", nil];
self.textArray5 = [NSArray arrayWithObjects:@"5.您是否设置并执行止损?\n",@"买前选定止损且执行过",@"买前未想,但实际有操作",@"仅能对部分股份进行止损",@"不止损,牢底坐穿型",@"在资金充裕的情况下,不断补充摊低成本", nil]; // 初始化分数,默认为0
self.scoreArray = [NSMutableArray array];
for (int i = ; i <; i++) {
[self.scoreArray addObject:@];
} } - (void)buildView{ // 滚动视图
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , self.width, *ScreenHeightRate)];
self.scrollView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.scrollView];
self.scrollView.contentSize = CGSizeMake(self.width*, *ScreenHeightRate);
self.scrollView.scrollEnabled = YES;
self.scrollView.directionalLockEnabled = YES;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self; // 添加5个试题
[self createQustionView:self.textArray1];
[self createQustionView:self.textArray2];
[self createQustionView:self.textArray3];
[self createQustionView:self.textArray4];
[self createQustionView:self.textArray5]; // 滚动条 // 1,添加虚线
UIImageView *ScrollBar = [[UIImageView alloc]initWithFrame:CGRectMake(*ScreenWidthRate, *ScreenHeightRate, self.width-*ScreenWidthRate, )];
ScrollBar.image = [UIImage imageNamed:@"line_03"];
[self.contentView addSubview:ScrollBar]; // 2,添加灰色圆点
for (int i = ;i< ; i++)
{
CGFloat X = (self.width-*ScreenWidthRate)/;
UIImageView* selectedPiont = [[UIImageView alloc]initWithFrame:CGRectMake((*ScreenWidthRate+X*i), *ScreenHeightRate, *ScreenHeightRate, *ScreenHeightRate)];
selectedPiont.image = [UIImage imageNamed:@"risk05"];
selectedPiont.tag = i+;
[self.contentView addSubview:selectedPiont]; } // 3,添加大圆点
self.currentPiont = [[UIImageView alloc]initWithFrame:CGRectMake(*ScreenWidthRate, *ScreenHeightRate, *ScreenHeightRate, *ScreenHeightRate)];
self.currentPiont.image = [UIImage imageNamed:@"risk05"];
[self.contentView addSubview:self.currentPiont]; // 提交按钮
CGRect submitframe = CGRectMake(*ScreenWidthRate, *ScreenHeightRate, self.width-*ScreenWidthRate, *ScreenHeightRate); self.submit = [UIButton normalUsedButtonStyleWithFrame:submitframe
title:@"提交"
target:self action:@selector(submitAction)];
[self.contentView addSubview:self.submit];
self.submit.enabled = NO; // 跳过按钮
if (self.isRegister == YES) { UIButton *skip = [UIButton buttonWithType:UIButtonTypeCustom]; skip.frame = CGRectMake(self.width-(+)*ScreenWidthRate, *ScreenHeightRate, *ScreenWidthRate, *ScreenHeightRate); skip.backgroundColor = [UIColor clearColor];
skip.titleLabel.font = [UIFont HeitiSCWithFontSize:];
skip.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
/**
* 富文本
*/
NSString *originStr = @"跳过";
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString: originStr];
[attributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(, originStr.length)];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[@"" hexColor] range:NSMakeRange(, originStr.length)];
[skip setAttributedTitle:attributedStr forState:UIControlStateNormal];
[skip addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:skip]; } } // 生成试题View
- (void)createQustionView:(NSArray *)textArray{ // 容器View
UIView *questionView = [[UIView alloc]initWithFrame:CGRectMake(self.width*self.XCount, , self.width, *ScreenHeightRate)];
questionView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:questionView]; // 背景图片
UIImageView *questionBG = [[UIImageView alloc]initWithFrame:CGRectMake(*ScreenWidthRate, *ScreenHeightRate, self.width-*ScreenWidthRate, *ScreenHeightRate)];
questionBG.image = [UIImage imageNamed:@"questionBG"];
questionBG.userInteractionEnabled = YES;
[questionView addSubview:questionBG]; // 问题
UILabel *questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(*ScreenWidthRate, *ScreenHeightRate, self.width-(+)*ScreenWidthRate, *ScreenHeightRate)];
questionLabel.backgroundColor = [UIColor clearColor];
questionLabel.textColor = [@"" hexColor];
questionLabel.text = textArray[]; questionLabel.font = [UIFont HeitiSCWithFontSize:]; questionLabel.textAlignment = NSTextAlignmentLeft;
questionLabel.numberOfLines = ;
[questionBG addSubview:questionLabel]; // 选项
for (int index= ; index <textArray.count;index++){ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(*ScreenWidthRate, (+(index-)*-)*ScreenHeightRate, (+)*ScreenHeightRate, (+)*ScreenHeightRate);
btn.backgroundColor = [UIColor clearColor];
btn.tag = self.btnTag + index;
btn.selected = NO;
[btn setImage:[UIImage imageNamed:@"risk02"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"risk03"] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(chooseQuestion:) forControlEvents:UIControlEventTouchUpInside];
[questionBG addSubview:btn]; // 选项文字
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(*ScreenWidthRate, (+(index-)*)*ScreenHeightRate, self.width-(+)*ScreenWidthRate, *ScreenHeightRate)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [@"" hexColor];
label.text = textArray[index];
label.font = [UIFont HeitiSCWithFontSize:];
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = ;
label.tag = self.btnTag- + index;
[questionBG addSubview:label]; // bug : 选项文字可以点击
UIButton *btnList = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor clearColor];
btnList.frame = CGRectMake(*ScreenWidthRate, (+(index-)*)*ScreenHeightRate, self.width-(+)*ScreenWidthRate, *ScreenHeightRate);
btnList.tag = self.btnTag+ + index;
[btnList addTarget:self action:@selector(chooseQuestion:) forControlEvents:UIControlEventTouchUpInside];
[questionBG addSubview:btnList]; }
// tag从 110,120,130,140,150开始
self.btnTag = self.btnTag +; // view的X值倍数
self.XCount ++; } // 选择的题目
- (void)chooseQuestion:(UIButton *)sender{ if (sender.tag >) {
sender.tag = sender.tag-;
} // 第一页 pageNumber = 1 第一页题目数量和其他页题目数量不一样
if (self.pageNumber == )
{
// 1,先全部设为非选中状态,字体原色
for (int i = ; i<=; i++) {
UIButton *btn = [self.view viewWithTag:+i];
btn.selected = NO; UILabel *label = [self.view viewWithTag:+i];
label.textColor = [@"" hexColor];
} // 2,再单独设置选中的按钮
UIButton *btn = [self.view viewWithTag:sender.tag];
btn.selected = YES; UILabel *label = [self.view viewWithTag:sender.tag -];
label.textColor = [@"3c77d6" hexColor]; // 3,传递分数
if (sender.tag == ) {
[self.scoreArray replaceObjectAtIndex: withObject:@];
}else if (sender.tag == ){
[self.scoreArray replaceObjectAtIndex: withObject:@];
}else if (sender.tag == ){
[self.scoreArray replaceObjectAtIndex: withObject:@];
} }else{
/**
* 第二页到第五页 pageNumber = 2,3,4,5
*/ // 1,先全部设为非选中状态
for (int i = ; i<=; i++) {
UIButton *btn = [self.view viewWithTag:+self.pageNumber*+i];
btn.selected = NO; UILabel *label = [self.view viewWithTag:self.pageNumber*+i];
label.textColor = [@"" hexColor]; } // 2,再单独设置选中的按钮
UIButton *btn = [self.view viewWithTag:sender.tag];
btn.selected = YES;
UILabel *label = [self.view viewWithTag:sender.tag -];
label.textColor = [@"3c77d6" hexColor]; // 3,传递分数 : 121-100-2*10 = 1分
NSNumber *number = [NSNumber numberWithInteger:sender.tag--self.pageNumber*];
[self.scoreArray replaceObjectAtIndex:self.pageNumber- withObject:number]; } // 改变圆点颜色
UIImageView *point = [self.view viewWithTag:self.pageNumber];
point.image = [UIImage imageNamed:@"risk04"]; self.currentPiont.image = point.image; [self isFinishQuestion]; // 判断题目是否答完 } // 判断题目是否答完
- (void)isFinishQuestion{ // 判断是否答完题
int flag = ;
for (int i = ;i <;i++){ if ([self.scoreArray[i] integerValue] != ) { // 答完情况 flag ++;
}
} if (flag == ) {
self.submit.enabled = YES;
}else{ self.submit.enabled = NO;
}
} /**
* 滚动视图代理
*/ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ // 计算页数 +1(从第一页开始)
NSUInteger temp = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width; // 避免负数
self.pageNumber = (int)temp +; // 改变右上角
self.pageChange.text = [NSString stringWithFormat:@"%d-5",self.pageNumber]; // 改变大圆点的位置
CGFloat X = (self.width-*ScreenWidthRate)/;
[UIView animateWithDuration:0.3 animations:^{
self.currentPiont.frame = CGRectMake((*ScreenWidthRate+X*(self.pageNumber -)), *ScreenHeightRate, *ScreenHeightRate, *ScreenHeightRate);
}]; if ([self.scoreArray[self.pageNumber-] intValue] == ) {
self.currentPiont.image = [UIImage imageNamed:@"risk05"];
}else{
self.currentPiont.image = [UIImage imageNamed:@"risk04"];
} } // 提交事件
- (void)submitAction{ if (self.isRegister) {
[[RCStatHelper shareInstance] insertEvent:BUT_REGISTER_RISK_SUBMIT];
}else{
[[RCStatHelper shareInstance] insertEvent:BUT_RISK_SEND];
} // 计算得分
/**
* S=1/3S1+2/3S25
其中S为评测总分,S1为第一题得分,S25为第二题到第五题得分汇总;
*/
int s1 = [self.scoreArray[] intValue];
int s25 = [self.scoreArray[] intValue]+[self.scoreArray[] intValue]+[self.scoreArray[] intValue]+[self.scoreArray[] intValue];
double all = s1/3.0 +s25*/3.0; if (all < ) { // @"稳健型"; // 1稳健型 2 平衡型 3积极型
self.riskLevel = @"";
}else if (all > ){ //@"积极型";
self.riskLevel = @"";
}else{ //@"平衡型";
self.riskLevel = @""; } // 上传网络结果
[self launchNetworking]; RiskReportViewController *riskReportVC = [RiskReportViewController new];
riskReportVC.totalScore = all;
riskReportVC.isRegister = self.isRegister;
riskReportVC.delegate = self;
[self.navigationController pushViewController:riskReportVC animated:YES]; } - (void)launchNetworking{ // 提交风险评测结果
self.networking = [V_2_X_Networking postMethodNetworkingWithNetworkInfomation:ServiceName(kRiskAssess)
requestDictionary:@{@"userId" : userInfomation().userId,
@"riskLevel" : self.riskLevel}
requestBodyType:[HttpBodyType type]
responseDataType:[JsonDataType type]];
// [self.networking appendJsonDictionary:@{@"userId" : @"U000002698", @"riskLevel" : self.riskLevel}];
self.networking.delegate = self; [self.networking startRequest]; self.showLoadingView = [LoadingView loadingViewStartLoadingInContentView:self.loadingView];
} #pragma mark 网络代理
- (void)requestSucess:(Networking *)networking data:(id)data { [self.showLoadingView stopLoading]; ISMResultModel *result = [[ISMResultModel alloc] initWithDictionary:data];
if (result.isSucceed) { NSLog(@"风险评估上传成功!");
// [self.navigationController dismissViewControllerAnimated:YES completion:^{
//
// }];
//
// [self refreshMyHomeViewController]; }else { NSLog(@"风险评估上传失败!");
[BlackShowView alertViewShowInContentView:self.loadingView message:result.errorInfo];
}
} - (void)requestFailed:(Networking *)networking error:(NSError *)error {
NSLog(@"风险评估%@",error);
// 提示已发送 [self.showLoadingView stopLoading];
[BlackShowView alertViewShowInContentView:self.loadingView message:networkErrorString];
} - (void)refreshMyHomeViewController { for (int i = ; i < self.navigationController.viewControllers.count; i++) { UIViewController *cv = self.navigationController.viewControllers[i];
if ([cv isKindOfClass:[MyHomeViewController class]]) { MyHomeViewController *myHomeVC = (MyHomeViewController *)cv;
[myHomeVC networkingToGetNetData];
}
}
} // 跳过事件
- (void)skipAction {
[[RCStatHelper shareInstance] insertEvent:BUT_REGISTER_RISK_JUMP];
[[NSNotificationCenter defaultCenter] postNotificationName:MyHomeViewControllerNetworkingToGetNetDataNotification object:nil];
[self.navigationController dismissViewControllerAnimated:YES completion:^{ }];
} // 导航栏右侧——页码
- (void)buildTitleView {
[super buildTitleView]; self.pageChange = [[UILabel alloc]initWithFrame:CGRectMake(self.width--, , , )];
self.pageChange.backgroundColor = [UIColor clearColor];
self.pageChange.textColor = [@"ffffff" hexColor];
self.pageChange.text = @"1-5";
self.pageChange.font = [UIFont HeitiSCWithFontSize:];
self.pageChange.textAlignment = NSTextAlignmentRight;
self.pageChange.numberOfLines = ;
[self.titleView addSubview:self.pageChange]; } #pragma mark - view.
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if(self.isRegister){ [[RCStatHelper shareInstance] startPage:PAGE_REGISTER_RISK]; }else{ [[RCStatHelper shareInstance] startPage:PAGE_RISK]; } self.enableInteractivePopGestureRecognizer = NO;
} - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated];
if(self.isRegister){ [[RCStatHelper shareInstance] endPage:PAGE_REGISTER_RISK]; }else{ [[RCStatHelper shareInstance] endPage:PAGE_RISK]; } self.enableInteractivePopGestureRecognizer = YES;
} // 重测刷新 delegate方法
- (void)refreshRiskEvaluate{ // [self.contentView setNeedsLayout];
[self.scrollView removeFromSuperview];
[self.submit removeFromSuperview];
[self.currentPiont removeFromSuperview]; // 清除滚动点小灰点
for (int i = ;i< ; i++)
{ UIImageView *point = [self.view viewWithTag:i+];
[point removeFromSuperview];
} self.pageChange.text = @"1-5"; // 初始化基本数据
[self foundationData]; // 创建界面
[self buildView]; }
@end

iOS开发——图片轮播图+单选选项的更多相关文章

  1. ios开发图片轮播器以及定时器小问题

    一:图片轮播器效果如图:能实现自动轮播,到最后一页时,轮播回来,可以实现拖拽滚动 二:代码: #import "ViewController.h" ; @interface Vie ...

  2. iOS开发--图片轮播

    直接上代码了,比较简单.演示下载地址:Demo // // UYViewController.m // 图片轮播器 // // Created by jiangys on 15/5/23. // Co ...

  3. iOS 图片轮播图(自动滚动)

    iOS 图片轮播图(自动滚动) #import "DDViewController.h" #define DDImageCount 5 @interface DDViewContr ...

  4. 图片轮播图插件的使用 unslider!!!

    1.百度图片轮播图unslider,第一个就会出现jquery unslider轮播图,点击进去,下载网站提供的文件,解压,内部有我们需要使用的各种js,图片等. 2.在自己的eclipse或者int ...

  5. BannerDemo【图片轮播图控件】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这里简单记录下一个开源库youth5201314/banner的运用.具体用法请阅读<youth5201314/banner& ...

  6. android 使用图片轮播图---banner 使用

    转自:https://github.com/youth5201314/banner 使用步骤 Step 1.依赖banner Gradle dependencies{ compile 'com.you ...

  7. js图片轮播图

    /*焦点图*/        var Box='.carousel';//盒子        var Menu=$(Box+' .l_cursor li');//圆点菜单        var Con ...

  8. css3爆炸效果更换图片轮播图

    思路:给一个div设置一个背景图片1.jpg,然后在这个div上面用两个for循环动态的创建一个列数为C行数为R数量的span,并给这些span设置宽高.定位并设置背景图片0.jpg,然后设置每个sp ...

  9. JavaScript实现图片轮播图

    <!DOCTYPE html><html> <head> <script > var time; function init(){ //设置定时操作 t ...

随机推荐

  1. Objective-C Runtime 运行时之二:成员变量与属性

    类型编码(Type Encoding) 作为对Runtime的补充,编译器将每个方法的返回值和参数类型编码为一个字符串,并将其与方法的selector关联在一起.这种编码方案在其它情况下也是非常有用的 ...

  2. js中动态载入css js样式

    js中动态载入css样式,方法如下: //<link rel="stylesheet" type="text/css" href="http:/ ...

  3. rank() over(partition)的使用

    有的时候会遇到这样的问题,我们需要查询一张表,而且要按照业务排序,比如我需要如下的结果: 地区   日期    费用  产品编号   用户编号 290 201202 258 1             ...

  4. Android设计模式系列--观察者模式

    观察者模式,是一种非常常见的设计模式,在很多系统中随处可见,尤其是涉及到数据状态发生变化需要通知的情况下.本文以AbstractCursor为例子,展开分析.观察者模式,Observer Patter ...

  5. 局部更新listview的问题(只更新某个item)

    转:http://blog.csdn.net/wu_shu_jun/article/details/7794576 public void updateView(int itemIndex) { // ...

  6. PostgreSQL的prepare 和 execute 动作背后

    我给PostgreSQL的源代码加入了调试信息以后,会有如下表现: 我执行Prepare: postgres=# prepare s(; PREPARE postgres=# 背后的反应: ** In ...

  7. Java对MySQL数据库进行连接、查询和修改【转载】

    一般过程: (1) 调用Class.forName()方法加载驱动程序. (2) 调用DriverManager对象的getConnection()方法,获得一个Connection对象. (3) 创 ...

  8. Image1.Canvas画图笔刷

      如何背景透明       unit Unit1;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Va ...

  9. 基于新浪sae使用php生成图片发布图文微博

    1.生成图片的代码: <?php header ("Content-type: image/png"); mb_internal_encoding("UTF-8&q ...

  10. 06---Java基础、面向对象

    一.Java基础 1.Java概述 Java语言特点:                     简单性.解释性.面向对象.高性能.分布式处理                     多线程.健壮性.动 ...