GiGaMaskTime/GIGA/Modules/Mask/Exercises/View/MaskQuestionView.m

220 lines
6.9 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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;
@end
@implementation MaskQuestionView
-(NSArray *)dataArr
{
if (!_dataArr) {
_dataArr = [NSArray new];
}
return _dataArr;
}
-(instancetype)initQuestionView:(CGRect)frame style:(MaskQuetionViewStyle)style titles:(NSArray *)titles{
self = [super initWithFrame:frame];
if (self) {
self.dataArr = titles;
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] init];
slider.frame = CGRectMake(0,self.frame.size.height,self.frame.size.height, 13);
[slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
self.poSlider = slider;
[self addSubview:slider];
self.poSlider.transform = CGAffineTransformMakeRotation(- M_PI/2);
}
-(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, 13) type:type];
[slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
self.slider = slider;
[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.backgroundColor = GIGARGB(181, 14, 14, 1);
yesBtn.frame = CGRectMake(self.center.x - 120/2, 40, 120, 34);
[yesBtn addTarget:self action:@selector(yesBtnAction) forControlEvents:UIControlEventTouchUpInside];
NSAttributedString *preTitle = [[NSAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
[yesBtn setAttributedTitle:preTitle forState:UIControlStateNormal];
yesBtn.layer.masksToBounds = YES;
yesBtn.layer.cornerRadius = 2;
[self addSubview:yesBtn];
UIButton *noBtn = [UIButton buttonWithType:UIButtonTypeCustom];
noBtn.backgroundColor = [UIColor whiteColor];
noBtn.frame = CGRectMake(self.center.x - 120/2,CGRectGetMaxY(yesBtn.frame) + 31, 120, 34);
[noBtn addTarget:self action:@selector(noBtnAction) forControlEvents:UIControlEventTouchUpInside];
NSAttributedString *noBtnTitle = [[NSAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTBOLD size:18],NSForegroundColorAttributeName:GIGARGB(181, 14, 14, 1)}];
[noBtn setAttributedTitle:noBtnTitle forState:UIControlStateNormal];
noBtn.layer.borderWidth = 1;
noBtn.layer.borderColor = GIGARGB(181, 14, 14, 1).CGColor;
noBtn.layer.masksToBounds = YES;
noBtn.layer.cornerRadius = 2;
[self addSubview:noBtn];
}
#pragma mark 是
-(void)yesBtnAction{
if (self.delegate && [self.delegate performSelector:@selector(yesOrNoChoiceModelAt:)]) {
[self.delegate yesOrNoChoiceModelAt:0];
}
}
#pragma mark 否
-(void)noBtnAction{
[self.delegate yesOrNoChoiceModelAt:1];
}
-(void)updateValue:(UISlider *)slider{
NSLog(@"value%f",slider.value);
[self anlysisAnswerWithSlider:slider.value];
}
#pragma mark 题目答案解析
-(void)anlysisAnswerWithSlider:(float)value{
if (_answerList.count <=2) {
return;
}
NSInteger totalCount = _answerList.count;
//设置权值 20 ~ 50
CGFloat sliderFormatValue = value*30 + 20;
if (self.poSlider) {
[self.poSlider showPopover];
self.poSlider.popover.textLabel.text = [NSString stringWithFormat:@"%.f",sliderFormatValue];
}
//从下到上 范围50 20
//每一份高度
int powSize = 30 / (totalCount - 1);
//第几份
NSInteger index = (sliderFormatValue - 20) / powSize;
if (self.delegate && [self.delegate performSelector:@selector(sliderChangeValueShouldChoiceModelAt:)]) {
[self.delegate sliderChangeValueShouldChoiceModelAt:index];
}
}
-(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