• 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 = 1;
        self.XCount     = 0;
        self.btnTag     = 110; // 从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.以下投资,您会选择哪一项:
    ",@"确定赢取100元",@"80%的机会赢取1,000元",@"50%的机会赢取5,000元",@"25%的机会赢取10,000元",@"5%的机会赢取100,000元", nil];
        self.textArray4 = [NSArray arrayWithObjects:@"4.您认为自己能承受的最大投资损失是多少?
    ",@"不能承受本金亏损",@"10%以内",@"10%——30%",@"30%——50%",@"超过50%", nil];
        self.textArray5 = [NSArray arrayWithObjects:@"5.您是否设置并执行止损?
    ",@"买前选定止损且执行过",@"买前未想,但实际有操作",@"仅能对部分股份进行止损",@"不止损,牢底坐穿型",@"在资金充裕的情况下,不断补充摊低成本", nil];
        
        // 初始化分数,默认为0
        self.scoreArray = [NSMutableArray array];
        for (int i = 0; i <5; i++) {
            [self.scoreArray addObject:@0];
        }
     
    
    }
    
    - (void)buildView{
        
        
        // 滚动视图
        self.scrollView                                = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, 330 *ScreenHeightRate)];
        self.scrollView.backgroundColor                = [UIColor clearColor];
        [self.contentView addSubview:self.scrollView];
        self.scrollView.contentSize                    = CGSizeMake(self.width*5, 320*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(45*ScreenWidthRate, 340*ScreenHeightRate, self.width-90*ScreenWidthRate, 1)];
        ScrollBar.image = [UIImage imageNamed:@"line_03"];
        [self.contentView addSubview:ScrollBar];
        
        
         // 2,添加灰色圆点
        for (int i = 0;i<5 ; i++)
        {
            CGFloat X = (self.width-90*ScreenWidthRate)/4;
            UIImageView* selectedPiont = [[UIImageView alloc]initWithFrame:CGRectMake((40*ScreenWidthRate+X*i), 335*ScreenHeightRate, 10*ScreenHeightRate, 10*ScreenHeightRate)];
            selectedPiont.image = [UIImage imageNamed:@"risk05"];
            selectedPiont.tag = i+1;
            [self.contentView addSubview:selectedPiont];
    
        }
        
         // 3,添加大圆点
        self.currentPiont = [[UIImageView alloc]initWithFrame:CGRectMake(39*ScreenWidthRate, 332*ScreenHeightRate, 18*ScreenHeightRate, 18*ScreenHeightRate)];
        self.currentPiont.image = [UIImage imageNamed:@"risk05"];
        [self.contentView addSubview:self.currentPiont];
        
        
        
        // 提交按钮
        CGRect submitframe = CGRectMake(12*ScreenWidthRate, 400*ScreenHeightRate, self.width-24*ScreenWidthRate, 40*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-(12+50)*ScreenWidthRate, 450*ScreenHeightRate, 50*ScreenWidthRate, 20*ScreenHeightRate);
            
            skip.backgroundColor            = [UIColor clearColor];
            skip.titleLabel.font            = [UIFont HeitiSCWithFontSize:15];
            skip.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            /**
             *  富文本
             */
            NSString *originStr                      = @"跳过";
            NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString: originStr];
            [attributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, originStr.length)];
            [attributedStr addAttribute:NSForegroundColorAttributeName value:[@"999999" hexColor] range:NSMakeRange(0, 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, 0, self.width, 330*ScreenHeightRate)];
        questionView.backgroundColor = [UIColor clearColor];
        [self.scrollView addSubview:questionView];
        
        // 背景图片
        UIImageView *questionBG = [[UIImageView alloc]initWithFrame:CGRectMake(15*ScreenWidthRate, 15*ScreenHeightRate, self.width-30*ScreenWidthRate, 300*ScreenHeightRate)];
        questionBG.image = [UIImage imageNamed:@"questionBG"];
        questionBG.userInteractionEnabled = YES;
        [questionView addSubview:questionBG];
        
        // 问题
        UILabel *questionLabel        = [[UILabel alloc]initWithFrame:CGRectMake(20*ScreenWidthRate, 15*ScreenHeightRate, self.width-(30+30)*ScreenWidthRate, 70*ScreenHeightRate)];
        questionLabel.backgroundColor = [UIColor clearColor];
        questionLabel.textColor       = [@"333333" hexColor];
        questionLabel.text            = textArray[0];
    
            
        questionLabel.font        = [UIFont HeitiSCWithFontSize:15];
            
        
        
        questionLabel.textAlignment   = NSTextAlignmentLeft;
        questionLabel.numberOfLines   = 0;
        [questionBG addSubview:questionLabel];
        
        // 选项
        for (int index= 1; index <textArray.count;index++){
         
            UIButton *btn                = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame                    = CGRectMake(20*ScreenWidthRate, (90+(index-1)*40-2)*ScreenHeightRate, (15+4)*ScreenHeightRate, (15+4)*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(45*ScreenWidthRate, (90+(index-1)*40)*ScreenHeightRate, self.width-(30+45)*ScreenWidthRate, 15*ScreenHeightRate)];
            label.backgroundColor = [UIColor clearColor];
            label.textColor       = [@"333333" hexColor];
            label.text            = textArray[index];
            label.font            = [UIFont HeitiSCWithFontSize:14];
            label.textAlignment   = NSTextAlignmentLeft;
            label.numberOfLines   = 1;
            label.tag             = self.btnTag-100 + index;
            [questionBG addSubview:label];
            
            // bug : 选项文字可以点击
            UIButton *btnList   = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor clearColor];
            btnList.frame       = CGRectMake(45*ScreenWidthRate, (90+(index-1)*40)*ScreenHeightRate, self.width-(30+45)*ScreenWidthRate, 15*ScreenHeightRate);
            btnList.tag         = self.btnTag+100 + index;
            [btnList addTarget:self action:@selector(chooseQuestion:) forControlEvents:UIControlEventTouchUpInside];
            [questionBG addSubview:btnList];
    
    
        
        }
        // tag从 110,120,130,140,150开始
         self.btnTag = self.btnTag +10;
        
        // view的X值倍数
        self.XCount ++;
        
    }
    
    
    // 选择的题目
    - (void)chooseQuestion:(UIButton *)sender{
        
        if (sender.tag >200) {
            sender.tag = sender.tag-100;
        }
        
        // 第一页 pageNumber = 1  第一页题目数量和其他页题目数量不一样
        if (self.pageNumber == 1)
        {
            // 1,先全部设为非选中状态,字体原色
            for (int i = 1; i<=3; i++) {
                UIButton *btn = [self.view viewWithTag:110+i];
                btn.selected  = NO;
                
                UILabel *label  = [self.view viewWithTag:10+i];
                label.textColor = [@"333333" hexColor];
            }
            
            // 2,再单独设置选中的按钮
            UIButton *btn = [self.view viewWithTag:sender.tag];
            btn.selected = YES;
            
            UILabel *label  = [self.view viewWithTag:sender.tag -100];
            label.textColor = [@"3c77d6" hexColor];
            
            // 3,传递分数
            if (sender.tag == 111 ) {
                [self.scoreArray replaceObjectAtIndex:0 withObject:@1];
            }else if (sender.tag == 112){
                [self.scoreArray replaceObjectAtIndex:0 withObject:@3];
            }else if (sender.tag == 113){
               [self.scoreArray replaceObjectAtIndex:0 withObject:@5];
            }
            
           
            
            
        }else{
            /**
             *  第二页到第五页 pageNumber = 2,3,4,5
             */
        
            // 1,先全部设为非选中状态
            for (int i = 1; i<=5; i++) {
                UIButton *btn = [self.view viewWithTag:100+self.pageNumber*10+i];
                btn.selected  = NO;
                
                UILabel *label  = [self.view viewWithTag:self.pageNumber*10+i];
                label.textColor = [@"333333" hexColor];
    
            }
            
            // 2,再单独设置选中的按钮
            UIButton *btn = [self.view viewWithTag:sender.tag];
            btn.selected = YES;
            UILabel *label  = [self.view viewWithTag:sender.tag -100];
            label.textColor = [@"3c77d6" hexColor];
    
            
            // 3,传递分数   : 121-100-2*10 = 1分
            NSNumber *number = [NSNumber numberWithInteger:sender.tag-100-self.pageNumber*10];
            [self.scoreArray replaceObjectAtIndex:self.pageNumber-1 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 = 0;
        for (int i = 0;i <5;i++){
            
            if ([self.scoreArray[i] integerValue] != 0) { // 答完情况
                
                flag ++;
            }
        }
        
        
        if (flag == 5) {
            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 +1;
        
        // 改变右上角
        self.pageChange.text = [NSString stringWithFormat:@"%d-5",self.pageNumber];
    
       // 改变大圆点的位置
        CGFloat X = (self.width-90*ScreenWidthRate)/4;
        [UIView animateWithDuration:0.3 animations:^{
            self.currentPiont.frame = CGRectMake((39*ScreenWidthRate+X*(self.pageNumber -1)), 332*ScreenHeightRate, 18*ScreenHeightRate, 18*ScreenHeightRate);
        }];
        
        if ([self.scoreArray[self.pageNumber-1] intValue] == 0) {
            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[0] intValue];
        int s25 = [self.scoreArray[1] intValue]+[self.scoreArray[2] intValue]+[self.scoreArray[3] intValue]+[self.scoreArray[4] intValue];
        double all = s1/3.0 +s25*2/3.0;
        
        
        
        if (all < 7) {
            
            // @"稳健型";    // 1稳健型  2 平衡型    3积极型
            self.riskLevel = @"1";
        }else if (all > 11){
        
            //@"积极型";
            self.riskLevel = @"3";
        }else{
            
            //@"平衡型";
            self.riskLevel = @"2";
            
        }
        
        
        
        // 上传网络结果
        [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 = 0; 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-12-40, 35, 40, 20)];
        self.pageChange.backgroundColor = [UIColor clearColor];
        self.pageChange.textColor       = [@"ffffff" hexColor];
        self.pageChange.text            = @"1-5";
        self.pageChange.font            = [UIFont HeitiSCWithFontSize:13];
        self.pageChange.textAlignment   = NSTextAlignmentRight;
        self.pageChange.numberOfLines   = 1;
        [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 = 0;i<5 ; i++)
        {
            
        UIImageView *point = [self.view viewWithTag:i+1];
        [point removeFromSuperview];
        }
        
        
        self.pageChange.text  = @"1-5";
    
        
        // 初始化基本数据
        [self foundationData];
        
        // 创建界面
        [self buildView];
      
    
    
    }
    @end
  • 相关阅读:
    android.mk文件里的通配符
    vi常用命令
    如何用ndk-stack察看android崩溃堆栈
    初识lua
    c++标准库中几个常见的数据结构的区别和应用规则
    玩转ubuntu FAQ
    c++0x新特性实例(比较常用的)
    cocos2d-x的Android工程开启c++0x特性
    随笔 — 致2018.
    CPU或内存占用过高时,发生了什么
  • 原文地址:https://www.cnblogs.com/LiuChengLi/p/5085254.html
Copyright © 2020-2023  润新知