iOS开发——图片轮播图+单选选项
由于公司开发需要,需要滚动每道评测题,
并且一道评测题单项选择,按钮和文字都可点击选中
(单选比多选复杂一点,但是原理差不多)
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开发——图片轮播图+单选选项的更多相关文章
- ios开发图片轮播器以及定时器小问题
一:图片轮播器效果如图:能实现自动轮播,到最后一页时,轮播回来,可以实现拖拽滚动 二:代码: #import "ViewController.h" ; @interface Vie ...
- iOS开发--图片轮播
直接上代码了,比较简单.演示下载地址:Demo // // UYViewController.m // 图片轮播器 // // Created by jiangys on 15/5/23. // Co ...
- iOS 图片轮播图(自动滚动)
iOS 图片轮播图(自动滚动) #import "DDViewController.h" #define DDImageCount 5 @interface DDViewContr ...
- 图片轮播图插件的使用 unslider!!!
1.百度图片轮播图unslider,第一个就会出现jquery unslider轮播图,点击进去,下载网站提供的文件,解压,内部有我们需要使用的各种js,图片等. 2.在自己的eclipse或者int ...
- BannerDemo【图片轮播图控件】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这里简单记录下一个开源库youth5201314/banner的运用.具体用法请阅读<youth5201314/banner& ...
- android 使用图片轮播图---banner 使用
转自:https://github.com/youth5201314/banner 使用步骤 Step 1.依赖banner Gradle dependencies{ compile 'com.you ...
- js图片轮播图
/*焦点图*/ var Box='.carousel';//盒子 var Menu=$(Box+' .l_cursor li');//圆点菜单 var Con ...
- css3爆炸效果更换图片轮播图
思路:给一个div设置一个背景图片1.jpg,然后在这个div上面用两个for循环动态的创建一个列数为C行数为R数量的span,并给这些span设置宽高.定位并设置背景图片0.jpg,然后设置每个sp ...
- JavaScript实现图片轮播图
<!DOCTYPE html><html> <head> <script > var time; function init(){ //设置定时操作 t ...
随机推荐
- oracle 在表中有数据的情况下修改表字段类型或缩小长度
分享自己一些常用的sql语句给大家 偶尔我们需要在已有表,并且有数据的情况下,修改其某个字段的类型或缩短他的长度,但是因为表中有数据,所以不可以直接修改,需要换个思路. //建立测试表,可跳过(善于应 ...
- 运用集合来做一个DVD管理器(全代码)
package DVD;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Array ...
- 高性能javascript
高性能javascript开发 标签(空格分隔): javascript DOM访问与修改 访问DOM元素是具有代价的,修改元素代价较为昂贵,会导致浏览器重新计算页面的几何变化. 尽量减少DOM访问, ...
- React-native 中的触摸响应功能
我们在做APP的时候,与桌面应用系统不同的是触摸响应. web页面对触摸响应的支持和原生的APP有着很大的差异. 基本用法 componentWillMount: function() { this. ...
- FormsAuthentication.GetRedirectUrl 方法
https://msdn.microsoft.com/zh-cn/library/8a22t5t3(v=vs.80) FormsAuthentication.GetRedirectUrl 方法 .NE ...
- 使用 StoryBoard 的时候加入用户引导页面
如果想让一个APP加上引导页面是一个非常完美的举动 但是,总会遇到一些问题 (不要忘记在APDelegate里面加上用户引导页面的头文件和程序启动时的第一个页面哦) 情况一:纯代码 判断是否是第一次启 ...
- 增强的for循环(或foreach)
增强的for循环(也称为foreach循环):不用下标变量,顺序的訪问整个数组.不能以其它顺序訪问数组,或者改变数组的元素. for(elementType element: arrayRefVar) ...
- C++中的头文件和源文件
一.C++编译模式 通常,在一个C++程序中,只包含两类文件——.cpp文件和.h文件.其中,.cpp文件被称作C++源文件,里面放的都是C++的源代码:而.h文件则被称作C++头文件,里面放的也是C ...
- Mysql导出表结构及表数据 mysqldump用法
几个常用用例: 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > ...
- Apache中 RewriteRule 规则参数介绍
Apache中 RewriteRule 规则参数介绍 摘要: Apache模块 mod_rewrite 提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.它支持每个完整规则可以拥有不限数量 ...