How to customize the slider effect of ios
This article introduces the knowledge of "how to customize the slider effect of ios". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, let's see the effect:
The code of the main implementation:
UIImage * thumbWithLevel (float aLevel) {float INSET_AMT = 1.5f; CGRect baseRect = CGRectMake (0,0,40,100); CGRect thumbRect = CGRectMake (0,40,40,20); UIGraphicsBeginImageContext (baseRect.size); CGContextRef context = UIGraphicsGetCurrentContext (); [[UIColor darkGrayColor] setFill]; CGContextAddRect (context, CGRectInset (thumbRect, INSET_AMT, INSET_AMT); CGContextFillPath (context); [UIColor whiteColor] setStroke]; CGContextSetLineWidth (context, 2) CGContextAddRect (context, CGRectInset (thumbRect, 2 * INSET_AMT, 2 * INSET_AMT); CGRect ellipseRect = CGRectMake (0,0,40,40); [[UIColor colorWithWhite:aLevel alpha:1] setFill]; CGContextAddEllipseInRect (context, ellipseRect); CGContextFillPath (context); NSString * numString = [NSString stringWithFormat:@ "% 0.2f", aLevel]; UIColor * textColor = (aLevel > 0.5)? [UIColor blackColor]: [UIColor whiteColor]; UIFont * font = [UIFont fontWithName:@ "Georgia" size:15]; NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init]; style.lineBreakMode = NSLineBreakByCharWrapping; style.alignment = NSTextAlignmentCenter; NSDictionary * attr = @ {NSFontAttributeName:font,NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:textColor}; [numString drawInRect:CGRectInset (ellipseRect, 0,6) withAttributes:attr]; [UIColor grayColor] setStroke]; CGContextSetLineWidth (context, 3) CGContextAddEllipseInRect (context, CGRectInset (ellipseRect, 2,2)); CGContextStrokePath (context); UIImage * theImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return theImage;}
Here we draw the picture through the method of context, which requires a little bit of performance, but we should not care about it now.
-(void) updateThumb {if ((self.value < 0.98) & & (ABS (self.value-previousValue) < 0.1f)) {return;} UIImage * customImg = thumbWithLevel (self.value); [self setThumbImage:customImg forState:UIControlStateHighlighted]; previousValue = self.value;}
It is more intuitive to change the above value through the value of the slider.
[self setThumbImage:simpleThumb () forState:UIControlStateNormal]; [self addTarget:self action:@selector (startDrag:) forControlEvents:UIControlEventTouchDown]; [self addTarget:self action:@selector (updateThumb) forControlEvents:UIControlEventValueChanged]; [self addTarget:self action:@selector (endDrag:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside]; and "how to customize the slider effect in ios" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!