311 lines
10 KiB
Objective-C
311 lines
10 KiB
Objective-C
//
|
||
// MaskQuestionView.m
|
||
// GIGA
|
||
//
|
||
// Created by lianxiang on 2018/9/11.
|
||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||
//
|
||
|
||
#import "MaskQuestionView.h"
|
||
#import "GIGaQuestionSlider.h"
|
||
#import "GiGaAnswerViewCell.h"
|
||
#import "NYSliderPopover.h"
|
||
#import "QustionAnswer.h"
|
||
|
||
@interface MaskQuestionView()<UITableViewDelegate,UITableViewDataSource>
|
||
@property(nonatomic,strong) NSArray *dataArr;
|
||
@property(nonatomic,strong) UITableView *tableView;
|
||
@property(nonatomic,strong) GIGaQuestionSlider *slider;
|
||
@property(nonatomic,strong) NYSliderPopover *poSlider;
|
||
@property(nonatomic,strong) UIButton *noBtn;
|
||
@property(nonatomic,strong) UIButton *yesBtn;
|
||
|
||
@end
|
||
|
||
@implementation MaskQuestionView
|
||
-(NSArray *)dataArr
|
||
{
|
||
if (!_dataArr) {
|
||
_dataArr = [NSArray new];
|
||
}
|
||
return _dataArr;
|
||
|
||
}
|
||
|
||
-(instancetype)initQuestionView:(CGRect)frame style:(MaskQuetionViewStyle)style titles:(NSArray *)titles current:(ExercisesModel *)currentModel{
|
||
|
||
self = [super initWithFrame:frame];
|
||
if (self) {
|
||
self.dataArr = titles;
|
||
self.currentModel = currentModel;
|
||
self.answerList = currentModel.answerList;
|
||
self.style = style;
|
||
[self creatSubs:style titles:titles];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
-(void)creatSubs:(MaskQuetionViewStyle)style titles:(NSArray *)titles{
|
||
switch (style) {
|
||
|
||
case MaskQuetionViewStleSliderNormal:
|
||
{
|
||
[self creatNormalStyle:titles style:GIGaQuestionSliderTypeGray];
|
||
}
|
||
break;
|
||
|
||
case MaskQuetionViewStleSliderGradient:
|
||
{
|
||
[self creatNormalStyle:titles style:GIGaQuestionSliderTypeGradient];
|
||
}
|
||
break;
|
||
|
||
case MaskQuetionViewStleYesOrNO:
|
||
{
|
||
[self creatYseNoView];
|
||
}
|
||
break;
|
||
|
||
case MaskQuetionViewStyleSliderShowIndicator:
|
||
{
|
||
[self creatPopSlider];
|
||
}
|
||
break;
|
||
default:
|
||
|
||
break;
|
||
}
|
||
}
|
||
|
||
-(void)creatPopSlider{
|
||
|
||
[self creatTab];
|
||
NYSliderPopover *slider = [[NYSliderPopover alloc] initWithFrame:CGRectMake(0,self.frame.size.height,self.frame.size.height, 14)];
|
||
// slider.frame = CGRectMake(0,self.frame.size.height,self.frame.size.height, 14);
|
||
[slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
|
||
self.poSlider = slider;
|
||
[self addSubview:slider];
|
||
self.poSlider.value = self.currentModel.slidervalue;
|
||
self.poSlider.transform = CGAffineTransformMakeRotation(- M_PI/2);
|
||
//NSLog(@"popslider%@",self.poSlider);
|
||
|
||
}
|
||
|
||
-(void)creatNormalStyle:(NSArray *)titles style:(GIGaQuestionSliderType)type{
|
||
[self creatTab];
|
||
[self creatSliderType:type];
|
||
}
|
||
|
||
-(void)creatSliderType:(GIGaQuestionSliderType)type{
|
||
|
||
GIGaQuestionSlider *slider = [[GIGaQuestionSlider alloc] initWithFrame:CGRectMake(0,self.frame.size.height,self.frame.size.height, 14) type:type];
|
||
[slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
|
||
self.slider = slider;
|
||
[self.slider setValue:self.currentModel.slidervalue];
|
||
[self addSubview:slider];
|
||
self.slider.transform = CGAffineTransformMakeRotation(- M_PI/2);
|
||
|
||
}
|
||
|
||
-(void)layoutSubviews{
|
||
|
||
[super layoutSubviews];
|
||
|
||
if (self.poSlider) {
|
||
self.poSlider.frame = CGRectMake(90, 0, self.poSlider.frame.size.width, self.frame.size.height);
|
||
}
|
||
|
||
if (self.slider) {
|
||
self.slider.frame = CGRectMake(90, 0, self.slider.frame.size.width, self.frame.size.height);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
-(void)creatYseNoView{
|
||
|
||
UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
yesBtn.frame = CGRectMake(self.center.x - 120/2, 40, 120, 34);
|
||
[yesBtn addTarget:self action:@selector(yesBtnAction:) forControlEvents:UIControlEventTouchUpInside];
|
||
yesBtn.layer.borderWidth = 1;
|
||
yesBtn.layer.borderColor = GIGARGB(181, 14, 14, 1).CGColor;
|
||
yesBtn.layer.masksToBounds = YES;
|
||
yesBtn.layer.cornerRadius = 2;
|
||
self.yesBtn = yesBtn;
|
||
[self addSubview:yesBtn];
|
||
|
||
UIButton *noBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
noBtn.frame = CGRectMake(self.center.x - 120/2,CGRectGetMaxY(yesBtn.frame) + 31, 120, 34);
|
||
[noBtn addTarget:self action:@selector(noBtnAction:) forControlEvents:UIControlEventTouchUpInside];
|
||
noBtn.layer.borderWidth = 1;
|
||
noBtn.layer.borderColor = GIGARGB(181, 14, 14, 1).CGColor;
|
||
noBtn.layer.masksToBounds = YES;
|
||
noBtn.layer.cornerRadius = 2;
|
||
self.noBtn = noBtn;
|
||
[self addSubview:noBtn];
|
||
//slidervalue 题目 是(是 否)类型 对应答案下标
|
||
if (self.currentModel.slidervalue == 1 ) {
|
||
|
||
[self selectNOBtnState:self.noBtn];
|
||
}else{
|
||
[self selctYesBtnState:self.yesBtn];
|
||
}
|
||
|
||
}
|
||
|
||
#pragma mark 是
|
||
-(void)yesBtnAction:(UIButton *)btn{
|
||
|
||
[self selctYesBtnState:btn];
|
||
if (self.delegate && [self.delegate performSelector:@selector(yesOrNoChoiceModelAt:)]) {
|
||
[self.delegate yesOrNoChoiceModelAt:0];
|
||
}
|
||
}
|
||
|
||
#pragma mark 否
|
||
-(void)noBtnAction:(UIButton *)btn{
|
||
|
||
[self selectNOBtnState:btn];
|
||
if (self.delegate && [self.delegate performSelector:@selector(yesOrNoChoiceModelAt:)]) {
|
||
[self.delegate yesOrNoChoiceModelAt:1];
|
||
}
|
||
}
|
||
//
|
||
-(void)selctYesBtnState:(UIButton *)btn{
|
||
NSAttributedString *preTitle = [[NSAttributedString alloc] initWithString:@"是" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
|
||
[btn setAttributedTitle:preTitle forState:UIControlStateNormal];
|
||
btn.backgroundColor = GIGARGB(181, 14, 14, 1);
|
||
|
||
NSAttributedString *noBtnTitle = [[NSAttributedString alloc] initWithString:@"否" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:GIGARGB(181, 14, 14, 1)}];
|
||
[self.noBtn setAttributedTitle:noBtnTitle forState:UIControlStateNormal];
|
||
self.noBtn.backgroundColor = [UIColor whiteColor];
|
||
|
||
}
|
||
|
||
-(void)selectNOBtnState:(UIButton *)btn{
|
||
NSAttributedString *preTitle = [[NSAttributedString alloc] initWithString:@"否" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
|
||
[btn setAttributedTitle:preTitle forState:UIControlStateNormal];
|
||
btn.backgroundColor = GIGARGB(181, 14, 14, 1);
|
||
|
||
NSAttributedString *yesTitle = [[NSAttributedString alloc] initWithString:@"是" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:GIGARGB(181, 14, 14, 1)}];
|
||
|
||
[self.yesBtn setAttributedTitle:yesTitle forState:UIControlStateNormal];
|
||
self.yesBtn.backgroundColor = [UIColor whiteColor];
|
||
}
|
||
|
||
-(void)updateValue:(UISlider *)slider{
|
||
NSLog(@"value%f",slider.value);
|
||
// 2 ,3 ,5 题slider 滑动时自动滑动到答案
|
||
|
||
if (self.poSlider) {
|
||
//1 范围匹配取值
|
||
[self anlysisAnswerWithSlider:slider.value];
|
||
}else{
|
||
// 2 ,3 ,5 题slider 滑动时自动滑动到答案
|
||
// 范围0 ~ 100
|
||
//每一份高度
|
||
NSInteger totalCount = _answerList.count;
|
||
CGFloat powSize = 100.f / (totalCount -1);
|
||
//当前值
|
||
CGFloat currentValue = slider.value * 100.f;
|
||
//答案游标
|
||
int count = currentValue / powSize;
|
||
//最终自动滑到答案slider对应的value
|
||
int index = 0 ;
|
||
index = (int)_answerList.count - 1 -count;
|
||
if (index < 0) {
|
||
index = 0;
|
||
}
|
||
NSLog(@"%d",index);
|
||
float value = 1.f/(totalCount -1) * count;
|
||
NSLog(@"%f",value);
|
||
[self anlysisAnswerWithSlider:value];
|
||
[self sliderAutoChoiceAnswerValue:value];
|
||
}
|
||
|
||
}
|
||
|
||
-(void)sliderAutoChoiceAnswerValue:(float)value{
|
||
|
||
[self.slider setValue:value];
|
||
|
||
}
|
||
|
||
#pragma mark 题目答案解析
|
||
-(void)anlysisAnswerWithSlider:(float)value{
|
||
|
||
if (_answerList.count <=2) {
|
||
return;
|
||
}
|
||
|
||
NSInteger totalCount = _answerList.count;
|
||
if (self.poSlider) {
|
||
[self.poSlider showPopover];
|
||
//设置权值 20 ~ 60
|
||
NSString * valueF = [NSString stringWithFormat:@"%.2f",value];
|
||
CGFloat sliderFormatValue = [valueF floatValue]*40.f + 20.f;
|
||
if (sliderFormatValue == 20.f) {
|
||
self.poSlider.popover.textLabel.text = @"20-";
|
||
}else if (sliderFormatValue == 60.f){
|
||
self.poSlider.popover.textLabel.text = @"60+";
|
||
}else{
|
||
self.poSlider.popover.textLabel.text = [NSString stringWithFormat:@"%.f",sliderFormatValue];
|
||
}
|
||
|
||
}
|
||
|
||
// 范围0 ~ 100
|
||
//每一份高度
|
||
CGFloat powSize = 100.f / (totalCount -1);
|
||
//当前值
|
||
CGFloat currentValue = value * 100.f;
|
||
|
||
//答案游标 1开始
|
||
int count = currentValue / powSize;
|
||
|
||
//答案数组下标 与slider值逆序对应(slider 0 对应 数组最大下标)
|
||
int index = 0 ;
|
||
index = (int)_answerList.count - 1 -count;
|
||
if (index < 0) {
|
||
index = 0;
|
||
}
|
||
|
||
if (self.delegate && [self.delegate performSelector:@selector(sliderChangeValueShouldChoiceModelAt: slider:)]) {
|
||
[self.delegate sliderChangeValueShouldChoiceModelAt:index slider:value];
|
||
}
|
||
|
||
}
|
||
|
||
-(void)creatTab{
|
||
|
||
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(127,0, self.frame.size.width - 127, self.frame.size.height) style:UITableViewStylePlain];
|
||
self.tableView.delegate = self;
|
||
self.tableView.dataSource = self;
|
||
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
|
||
self.tableView.scrollEnabled = NO;
|
||
[self addSubview:self.tableView];
|
||
|
||
}
|
||
|
||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
||
return self.dataArr.count;
|
||
}
|
||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
|
||
|
||
return (self.frame.size.height - 20) / (self.dataArr.count -1);
|
||
|
||
}
|
||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
||
GiGaAnswerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GiGaAnswerViewCell"];
|
||
if (!cell) {
|
||
cell = [[[NSBundle mainBundle] loadNibNamed:@"GiGaAnswerViewCell" owner:self options:nil] lastObject];
|
||
}
|
||
cell.answerTitle.text =self.dataArr[indexPath.row];
|
||
return cell;
|
||
|
||
}
|
||
|
||
|
||
@end
|