UISlider를 사용자 정의하여 디자인을 변경하거나 표시를 하세요!
16426 단어 iOSUISliderObjective-C
슬라이더를 표시하고 싶습니다!
라는 의뢰가 와서 여러가지 슬라이더를 커스텀 했으므로, 삭과 여기에 메모해 둡니다.
기본은 UISlider를 상속하고 drawRect 내에서 골고루 쓰고 있습니다.
Git : htps : // 기주 b. 코 m / 우 뮤 w / H 마마 rkS ぃ
MarkSlider.h
#import <UIKit/UIKit.h>
@interface MarkSlider : UISlider
// スライダーline幅
@property (nonatomic) CGFloat lineWitdh;
// マーク背景色
@property (nonatomic) UIColor *markColor;
// マーク幅
@property (nonatomic) CGFloat markWidth;
// マークしたい場所の配列
// @[@0,@0.5,@1.0]
@property (nonatomic) NSArray *markPoints;
@end
MarkSlider.m
#import "MarkSlider.h"
@interface MarkSlider()
@property (nonatomic) CGFloat sideOffset;
@end
@implementation MarkSlider
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupDefaultSettings];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setupDefaultSettings];
}
return self;
}
- (void)setupDefaultSettings{
self.markColor = [UIColor orangeColor];
self.lineWitdh = 2.0f;
self.markWidth = 7.0f;
self.minimumTrackTintColor = [UIColor blueColor];
self.maximumTrackTintColor = [UIColor lightGrayColor];
self.sideOffset = 12.0f;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGRect innerRect = CGRectInset(rect, 1.0, 10.0);
UIGraphicsBeginImageContextWithOptions(innerRect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *minimumTrack = [self trackImageWithContext:&context innerRect:innerRect color:self.minimumTrackTintColor ];
[minimumTrack drawAtPoint:CGPointMake(0,0)];
[self addMarksOnContext:&context innerRect:innerRect];
[self setMinimumTrackImage:[UIGraphicsGetImageFromCurrentImageContext() resizableImageWithCapInsets:UIEdgeInsetsZero]
forState:UIControlStateNormal
];
UIImage *maximumTrack = [self trackImageWithContext:&context innerRect:innerRect color:self.maximumTrackTintColor];
[maximumTrack drawAtPoint:CGPointMake(0,0)];
[self addMarksOnContext:&context innerRect:innerRect];
[self setMaximumTrackImage:[UIGraphicsGetImageFromCurrentImageContext() resizableImageWithCapInsets:UIEdgeInsetsZero]
forState:UIControlStateNormal
];
UIGraphicsEndImageContext();
}
/**
* create bar image
*/
- (UIImage *)trackImageWithContext:(CGContextRef *)context innerRect:(CGRect)innerRect color:(UIColor *)color {
CGContextSetLineWidth(*context,_lineWitdh);
CGContextSetLineCap(*context, kCGLineCapRound);
CGContextMoveToPoint(*context, _sideOffset, CGRectGetHeight(innerRect)/2);
CGContextAddLineToPoint(*context, innerRect.size.width - _sideOffset, CGRectGetHeight(innerRect)/2);
CGContextSetStrokeColorWithColor(*context, [color CGColor]);
CGContextStrokePath(*context);
return [UIGraphicsGetImageFromCurrentImageContext() resizableImageWithCapInsets:UIEdgeInsetsZero];
}
/**
* add marks at bar
*/
- (void)addMarksOnContext:(CGContextRef *)context innerRect:(CGRect)innerRect {
for (NSNumber *point in _markPoints) {
float position = [point floatValue] * (innerRect.size.width - _sideOffset * 2) + _sideOffset;
CGContextSetLineWidth(*context, _markWidth);
CGContextSetLineCap(*context, kCGLineCapRound);
CGContextMoveToPoint(*context, position, CGRectGetHeight(innerRect)/2.0f);
CGContextAddLineToPoint(*context, position, CGRectGetHeight(innerRect)/2.0f);
CGContextSetStrokeColorWithColor(*context, [_markColor CGColor]);
CGContextStrokePath(*context);
}
}
@end
Reference
이 문제에 관하여(UISlider를 사용자 정의하여 디자인을 변경하거나 표시를 하세요!), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ebi-toro/items/f93a863c345400d46fdd텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)