This commit is contained in:
lianxiang
2018-09-12 20:30:22 +08:00
parent a2de9505ee
commit 7d0027f58d
21 changed files with 805 additions and 110 deletions
@@ -11,11 +11,11 @@ typedef NS_ENUM(NSInteger,GIGaQuestionSliderType)
{
GIGaQuestionSliderTypeGray = 0,//灰
GIGaQuestionSliderTypeGradient ,//变色
GIGaQuestionSliderTypeIndicator //带指示器
};
@interface GIGaQuestionSlider : UISlider
- (instancetype)initWithFrame:(CGRect)frame type:(GIGaQuestionSliderType)type;
@end
@@ -24,9 +24,10 @@
self.minimumValue = 0.0;
self.maximumValue = 1.0;
self.value = 0.5;
[self creatSubView:type];
}
return self;
}
-(void)creatSubView:(GIGaQuestionSliderType)type{
@@ -37,12 +38,14 @@
{
[self createUI];
}
break;
case GIGaQuestionSliderTypeGradient:
{
[self creatGradient];
}
break;
default:
break;
}
@@ -70,8 +73,6 @@
[self.layer addSublayer:gradientLayer];
}
-(void)createUI{
self.minimumValue = 0.0;
@@ -82,6 +83,7 @@
}
-(CGRect)trackRectForBounds:(CGRect)bounds
{
bounds.origin.x = bounds.origin.x;
@@ -12,7 +12,7 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="GiGaAnswerViewCell" id="KGk-i7-Jjw" customClass="GiGaAnswerViewCell">
<tableViewCell contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="GiGaAnswerViewCell" id="KGk-i7-Jjw" customClass="GiGaAnswerViewCell">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
@@ -11,13 +11,23 @@
typedef NS_ENUM(NSInteger,MaskQuetionViewStyle)
{
MaskQuetionViewStleSliderNormal = 0,// 滑块正常
MaskQuetionViewStleSliderGradient ,//滑块变色
MaskQuetionViewStleYesOrNO // 是 否
MaskQuetionViewStleSliderGradient =1 ,//滑块变色
MaskQuetionViewStleYesOrNO =2, // 是 否
MaskQuetionViewStyleSliderShowIndicator =3,//滑动带指示器
};
@protocol QeustionViewDelegate<NSObject>;
@optional
-(void)sliderChangeValueShouldChoiceModelAt:(NSInteger)index;
-(void)yesOrNoChoiceModelAt:(NSInteger)index;
@end
@interface MaskQuestionView : UIView
@property(nonatomic,assign) MaskQuetionViewStyle *style;
@property(nonatomic,assign) id<QeustionViewDelegate>delegate;
@property(nonatomic,assign) MaskQuetionViewStyle style;
@property(nonatomic,strong) NSArray *answerList;
-(instancetype)initQuestionView:(CGRect)frame style:(MaskQuetionViewStyle)style titles:(NSArray *)titles;
@@ -9,11 +9,15 @@
#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
@@ -29,11 +33,10 @@
-(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;
}
@@ -43,19 +46,25 @@
case MaskQuetionViewStleSliderNormal:
{
[self creatNormalStyle:titles];
[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:
@@ -64,10 +73,25 @@
}
}
-(void)creatNormalStyle:(NSArray *)titles{
-(void)creatPopSlider{
[self creatTab];
GIGaQuestionSlider *slider = [[GIGaQuestionSlider alloc] initWithFrame:CGRectMake(0,self.frame.size.height,self.frame.size.height, 13) type:GIGaQuestionSliderTypeGray];
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];
@@ -77,14 +101,88 @@
-(void)layoutSubviews{
[super layoutSubviews];
self.slider.frame = CGRectMake(90, 0, self.slider.frame.size.width, self.frame.size.height);
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);
GILog(@"formatvalue:%.2f",slider.value*60);
[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{
@@ -0,0 +1,15 @@
//
// NYPopover.h
// NYReader
//
// Created by Cassius Pacheco on 21/12/12.
// Copyright (c) 2012 Nyvra Software. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface NYPopover : UIView
@property (nonatomic, strong) UILabel *textLabel;
@end
@@ -0,0 +1,134 @@
//
// NYPopover.m
// NYReader
//
// Created by Cassius Pacheco on 21/12/12.
// Copyright (c) 2012 Nyvra Software. All rights reserved.
//
#import "NYPopover.h"
@implementation NYPopover
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.font = [UIFont boldSystemFontOfSize:13];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.adjustsFontSizeToFitWidth = YES;
self.opaque = NO;
[self addSubview:self.textLabel];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
CGFloat y = (frame.size.height - 26) / 3;
if (frame.size.height < 38)
y = 0;
self.textLabel.frame = CGRectMake(0, y, frame.size.width, 26);
}
- (void)drawRect:(CGRect)rect
{
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* gradientColor = [UIColor colorWithRed: 0.267 green: 0.303 blue: 0.335 alpha: 1];
UIColor* gradientColor2 = [UIColor colorWithRed: 0.04 green: 0.04 blue: 0.04 alpha: 1];
UIColor* shadowColor2 = [UIColor colorWithRed: 0.524 green: 0.553 blue: 0.581 alpha: 0.3];
//// Gradient Declarations
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)gradientColor.CGColor,
(id)gradientColor2.CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
//// Shadow Declarations
UIColor* innerShadow = shadowColor2;
CGSize innerShadowOffset = CGSizeMake(0, 1.5);
CGFloat innerShadowBlurRadius = 0.5;
//// Frames
CGRect frame = self.bounds;
//// Subframes
CGRect frame2 = CGRectMake(CGRectGetMinX(frame) + floor((CGRectGetWidth(frame) - 11) * 0.51724 + 0.5), CGRectGetMinY(frame) + CGRectGetHeight(frame) - 9, 11, 9);
//// Bezier Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMinY(frame) + 4.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMaxY(frame) - 11.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMaxX(frame) - 4.5, CGRectGetMaxY(frame) - 7.5) controlPoint1: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMaxY(frame) - 9.29) controlPoint2: CGPointMake(CGRectGetMaxX(frame) - 2.29, CGRectGetMaxY(frame) - 7.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame2) + 10.64, CGRectGetMinY(frame2) + 1.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame2) + 5.5, CGRectGetMinY(frame2) + 8)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame2) + 0.36, CGRectGetMinY(frame2) + 1.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 4.5, CGRectGetMaxY(frame) - 7.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMaxY(frame) - 11.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 2.29, CGRectGetMaxY(frame) - 7.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMaxY(frame) - 9.29)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMinY(frame) + 4.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 4.5, CGRectGetMinY(frame) + 0.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 0.5, CGRectGetMinY(frame) + 2.29) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 2.29, CGRectGetMinY(frame) + 0.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMaxX(frame) - 4.5, CGRectGetMinY(frame) + 0.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMinY(frame) + 4.5) controlPoint1: CGPointMake(CGRectGetMaxX(frame) - 2.29, CGRectGetMinY(frame) + 0.5) controlPoint2: CGPointMake(CGRectGetMaxX(frame) - 0.5, CGRectGetMinY(frame) + 2.29)];
[bezierPath closePath];
CGContextSaveGState(context);
[bezierPath addClip];
CGRect bezierBounds = bezierPath.bounds;
CGContextDrawLinearGradient(context, gradient,
CGPointMake(CGRectGetMidX(bezierBounds), CGRectGetMinY(bezierBounds)),
CGPointMake(CGRectGetMidX(bezierBounds), CGRectGetMaxY(bezierBounds)),
0);
CGContextRestoreGState(context);
////// Bezier Inner Shadow
CGRect bezierBorderRect = CGRectInset([bezierPath bounds], -innerShadowBlurRadius, -innerShadowBlurRadius);
bezierBorderRect = CGRectOffset(bezierBorderRect, -innerShadowOffset.width, -innerShadowOffset.height);
bezierBorderRect = CGRectInset(CGRectUnion(bezierBorderRect, [bezierPath bounds]), -1, -1);
UIBezierPath* bezierNegativePath = [UIBezierPath bezierPathWithRect: bezierBorderRect];
[bezierNegativePath appendPath: bezierPath];
bezierNegativePath.usesEvenOddFillRule = YES;
CGContextSaveGState(context);
{
CGFloat xOffset = innerShadowOffset.width + round(bezierBorderRect.size.width);
CGFloat yOffset = innerShadowOffset.height;
CGContextSetShadowWithColor(context,
CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
innerShadowBlurRadius,
innerShadow.CGColor);
[bezierPath addClip];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(bezierBorderRect.size.width), 0);
[bezierNegativePath applyTransform: transform];
[[UIColor grayColor] setFill];
[bezierNegativePath fill];
}
CGContextRestoreGState(context);
[[UIColor blackColor] setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
@end
@@ -0,0 +1,21 @@
//
// NYSliderPopover.h
// NYReader
//
// Created by Cassius Pacheco on 21/12/12.
// Copyright (c) 2012 Nyvra Software. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "NYPopover.h"
@interface NYSliderPopover : UISlider
@property (nonatomic, strong) NYPopover *popover;
- (void)showPopover;
- (void)showPopoverAnimated:(BOOL)animated;
- (void)hidePopover;
- (void)hidePopoverAnimated:(BOOL)animated;
@end
@@ -0,0 +1,187 @@
//
// NYSliderPopover.m
// NYReader
//
// Created by Cassius Pacheco on 21/12/12.
// Copyright (c) 2012 Nyvra Software. All rights reserved.
//
#import "NYSliderPopover.h"
#import "NYPopover.h"
@implementation NYSliderPopover
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
#pragma mark -
#pragma mark UISlider methods
- (NYPopover *)popover
{
if (_popover == nil) {
//Default size, can be changed after
[self addTarget:self action:@selector(updatePopoverFrame) forControlEvents:UIControlEventValueChanged];
_popover = [[NYPopover alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - 32, 40, 32)];
[self updatePopoverFrame];
_popover.alpha = 0;
[self.superview addSubview:_popover];
[self thumbimageView];
[self createUI];
}
return _popover;
}
- (void)setValue:(float)value
{
[super setValue:value];
[self updatePopoverFrame];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self updatePopoverFrame];
[self showPopoverAnimated:YES];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self hidePopoverAnimated:YES];
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self hidePopoverAnimated:YES];
[super touchesCancelled:touches withEvent:event];
}
#pragma mark -
#pragma mark - Popover methods
- (void)updatePopoverFrame
{
//Inspired in Collin Ruffenach's ELCSlider https://github.com/elc/ELCSlider/blob/master/ELCSlider/ELCSlider.m#L53
CGFloat minimum = self.minimumValue;
CGFloat maximum = self.maximumValue;
CGFloat value = self.value;
if (minimum < 0.0) {
value = self.value - minimum;
maximum = maximum - minimum;
minimum = 0.0;
}
CGFloat x = self.frame.origin.x;
CGFloat maxMin = (maximum + minimum) / 2.0;
x += (((value - minimum) / (maximum - minimum)) * self.frame.size.width) - (self.popover.frame.size.width / 2.0);
if (value > maxMin) {
value = (value - maxMin) + (minimum * 1.0);
value = value / maxMin;
value = value * 11.0;
x = x - value;
} else {
value = (maxMin - value) + (minimum * 1.0);
value = value / maxMin;
value = value * 11.0;
x = x + value;
}
CGRect popoverRect = self.popover.frame;
popoverRect.origin.x = x;
popoverRect.origin.y = self.frame.origin.y - popoverRect.size.height - 1;
self.popover.frame = popoverRect;
}
- (void)showPopover
{
[self showPopoverAnimated:NO];
}
- (void)showPopoverAnimated:(BOOL)animated
{
if (animated) {
[UIView animateWithDuration:0.25 animations:^{
self.popover.alpha = 1.0;
}];
} else {
self.popover.alpha = 1.0;
}
}
- (void)hidePopover
{
[self hidePopoverAnimated:NO];
}
- (void)hidePopoverAnimated:(BOOL)animated
{
if (animated) {
[UIView animateWithDuration:0.25 animations:^{
self.popover.alpha = 0;
}];
} else {
self.popover.alpha = 0;
}
}
-(void)createUI{
self.minimumValue = 0.0;
self.maximumValue = 1.0;
self.value = 0.5;
self.minimumTrackTintColor = GIGARGB(216,216, 216, 1);
self.maximumTrackTintColor = GIGARGB(216,216, 216, 1);
}
-(CGRect)trackRectForBounds:(CGRect)bounds
{
bounds.origin.x = bounds.origin.x;
bounds.origin.y = bounds.origin.y;
bounds.size.height = bounds.size.height;
bounds.size.width = bounds.size.width;
return bounds;
}
-(void)thumbimageView{
[self setThumbImage:[self OriginImage:[UIImage imageNamed:@"btn_slider_thumb"] scaleToSize:CGSizeMake(40, 40)] forState:UIControlStateNormal];
[self setThumbImage:[self OriginImage:[UIImage imageNamed:@"btn_slider_thumb"] scaleToSize:CGSizeMake(40, 40)] forState:UIControlStateHighlighted];
}
-(UIImage*) OriginImage:(UIImage*)image scaleToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);//size为CGSize类型,即你所需要的图片尺寸
[image drawInRect:CGRectMake(0,0, size.width, size.height)];
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
@end