199 lines
6.6 KiB
Objective-C
199 lines
6.6 KiB
Objective-C
//
|
|
// GIGAQuetionView.m
|
|
// GIGA
|
|
//
|
|
// Created by lianxiang on 2018/8/23.
|
|
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
|
//
|
|
|
|
#import "GIGAQuetionView.h"
|
|
#import "GiGaHelper.h"
|
|
#import "Masonry.h"
|
|
@interface GIGAQuetionView()
|
|
|
|
@property (nonatomic, weak) UIView *view;
|
|
@property (nonatomic, assign, readwrite) GIGAQuetionViewStle style;
|
|
@property (nonatomic, strong, readwrite) NSArray *answerArr;
|
|
@property (nonatomic, copy, readwrite) NSString *selectAnswer;
|
|
@property (nonatomic, copy, readwrite) NSString *qrnum;
|
|
|
|
@end
|
|
|
|
@implementation GIGAQuetionView
|
|
|
|
- (UIView *)view
|
|
{
|
|
if (!_view) {
|
|
UIView *view = [[UIView alloc] init];
|
|
view.backgroundColor = [UIColor blueColor];
|
|
[self addSubview:view];
|
|
_view = view;
|
|
}
|
|
return _view;
|
|
}
|
|
|
|
- (instancetype)init
|
|
{
|
|
if (self = [super init]) {
|
|
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)reloadViewWithFrame:(CGRect)frame style:(GIGAQuetionViewStle)style answerArray:(NSArray *)answerArray userAnswers:(NSArray *)userAnswers
|
|
{
|
|
self.style = style;
|
|
self.answerArr = answerArray;
|
|
if (_style == GIGAQuetionViewStyleTrueOrFalse) self.answerArr = @[@"正确", @"错误"];
|
|
|
|
_selectAnswer = @"";
|
|
_qrnum = @"";
|
|
|
|
if (userAnswers.count > 0) {
|
|
_selectAnswer = [userAnswers componentsJoinedByString:@"|"];
|
|
for (int i = 0; i < userAnswers.count; i++) {
|
|
if ([_qrnum isEqualToString:@""]) {
|
|
_qrnum = [NSString stringWithFormat:@"%d", [userAnswers[i] characterAtIndex:0] - 65];
|
|
}else {
|
|
_qrnum = [_qrnum stringByAppendingString:[NSString stringWithFormat:@"|%d", [userAnswers[i] characterAtIndex:0] - 65]];
|
|
}
|
|
}
|
|
}
|
|
|
|
[self removeView];
|
|
[self creatControlWithFrame:frame];
|
|
}
|
|
|
|
- (void)creatControlWithFrame:(CGRect)frame
|
|
{
|
|
CGFloat btnH = 25;
|
|
CGFloat padding = 10.f;
|
|
CGFloat labelY = 0;
|
|
UIFont *font = [UIFont systemFontOfSize:14.f];
|
|
|
|
for (int i = 0; i < _answerArr.count; i++) {
|
|
//去回车、换行
|
|
NSString *labelText = _answerArr[i];
|
|
labelText = [labelText stringByReplacingOccurrencesOfString:@"\r" withString:@""];
|
|
labelText = [labelText stringByReplacingOccurrencesOfString:@"\n" withString:@""];
|
|
|
|
|
|
//内容高度
|
|
//CGFloat labelH = [GiGaHelper sizeWithText:labelText font:font maxSize:CGSizeMake(frame.size.width - btnH - 5, MAXFLOAT)].height;
|
|
CGFloat labelH = [GiGaHelper getSpaceLabelHeight:labelText withFont:font maxSize:CGSizeMake(frame.size.width - btnH - 5, MAXFLOAT)];
|
|
|
|
//选项标签
|
|
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(btnH + 5, labelY + 4,frame.size.width - btnH - 5, labelH)];
|
|
[GiGaHelper setLabelSpace:label withValue:labelText withFont:font] ;
|
|
label.font = font;
|
|
label.numberOfLines = 0;
|
|
label.textColor = GIGAUIColorFromRGBA(0x616161);
|
|
[self.view addSubview:label];
|
|
|
|
labelY += MAX(labelH, btnH) + padding;
|
|
|
|
//选项框
|
|
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(label.frame) - 4, btnH, btnH)];
|
|
btn.tag = i + 100;
|
|
btn.showsTouchWhenHighlighted = NO;
|
|
[btn setImage:[UIImage imageNamed:@"exerc_answer_option_nor"] forState:UIControlStateNormal];
|
|
[btn setImage:[UIImage imageNamed:@"exerc_answer_option_sel"] forState:UIControlStateSelected];
|
|
[btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
|
|
[self.view addSubview:btn];
|
|
|
|
//标签点击区域
|
|
UIButton *labelBtn = [[UIButton alloc] initWithFrame:label.frame];
|
|
labelBtn.tag = i + 200;
|
|
[labelBtn addTarget:self action:@selector(labelBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
|
|
[self.view addSubview:labelBtn];
|
|
|
|
if ([_qrnum rangeOfString:[NSString stringWithFormat:@"%d", i]].location != NSNotFound) {
|
|
btn.selected = YES;
|
|
labelBtn.selected = YES;
|
|
}
|
|
}
|
|
|
|
self.view.frame = CGRectMake(0,0,frame.size.width, labelY - padding);
|
|
frame.size.height = labelY - padding;
|
|
self.frame = frame;
|
|
|
|
// [self setNeedsUpdateConstraints];
|
|
// [self mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
// make.top.mas_equalTo(self.superview.mas_top).offset(frame.origin.y + 10);
|
|
// make.left.mas_equalTo(self.superview.mas_left).offset(30);
|
|
// make.height.mas_equalTo(frame.size.height);
|
|
// make.width.mas_equalTo(frame.size.width - 30*2);
|
|
// }];
|
|
}
|
|
|
|
- (void)btnOnClick:(UIButton *)btn
|
|
{
|
|
[self selectOptionWithBtn:btn tag:100];
|
|
|
|
[self setBtnSelectedWithTag:100];
|
|
}
|
|
|
|
- (void)labelBtnOnClick:(UIButton *)labelBtn
|
|
{
|
|
[self selectOptionWithBtn:labelBtn tag:200];
|
|
|
|
[self setBtnSelectedWithTag:200];
|
|
}
|
|
|
|
- (void)selectOptionWithBtn:(UIButton *)btn tag:(int)tag
|
|
{
|
|
NSString *selStr = [NSString stringWithFormat:@"%c", (int)btn.tag - tag + 65];
|
|
|
|
if (_style == GIGAQuetionViewStyleTrueOrFalse || _style == GIGAQuetionViewStleSingleChoice) {
|
|
_selectAnswer = selStr;
|
|
_qrnum = [NSString stringWithFormat:@"%ld", btn.tag - tag];
|
|
for (int i = 0; i < _answerArr.count; i++) {
|
|
UIButton *btn = [self.view viewWithTag:i + tag];
|
|
btn.selected = NO;
|
|
}
|
|
btn.selected = YES;
|
|
|
|
}else if (_style == GIGAQuetionViewStleMutipleChoice) {
|
|
btn.selected = !btn.selected;
|
|
_selectAnswer = @"";
|
|
_qrnum = @"";
|
|
for (int i = 0; i < _answerArr.count; i++) {
|
|
UIButton *selBtn = [self.view viewWithTag:i + tag];
|
|
if (selBtn.selected) {
|
|
if ([_selectAnswer isEqualToString:@""]) {
|
|
_selectAnswer = [NSString stringWithFormat:@"%c", i + 65];
|
|
_qrnum = [NSString stringWithFormat:@"%d", i];
|
|
}else {
|
|
_selectAnswer = [_selectAnswer stringByAppendingString:[NSString stringWithFormat:@"|%@", [NSString stringWithFormat:@"%c", i + 65]]];
|
|
_qrnum = [_qrnum stringByAppendingString:[NSString stringWithFormat:@"|%d", i]];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)setBtnSelectedWithTag:(NSInteger)tag
|
|
{
|
|
NSInteger otTag = tag == 100 ? 200 : 100;
|
|
|
|
for (int i = 0; i < _answerArr.count; i++) {
|
|
UIButton *btn = [self.view viewWithTag:i + tag];
|
|
UIButton *otBtn = [self.view viewWithTag:i + otTag];
|
|
otBtn.selected = btn.selected;
|
|
}
|
|
}
|
|
|
|
- (void)removeView
|
|
{
|
|
[self.view removeFromSuperview];
|
|
self.view = nil;
|
|
}
|
|
|
|
- (void)setStyle:(GIGAQuetionViewStle)style
|
|
{
|
|
_style = style;
|
|
}
|
|
|
|
@end
|