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

좋은 웹페이지 즐겨찾기