5.9 C
New York
Thursday, March 13, 2025

ios – UISlider thumb to maneuver with out touching the thumb


I’ve a customized UISlider inside UICollectionViewCell. I would love to have the ability to transfer the slider thumb by touching anyplace on the cell (on the purple bit). For example, if I used to be to place my finger on the highest proper nook of the cell and slide down, I would love the thumb to slip downwards from its present place (-19.0 on this case), as a substitute of leaping up first.

enter image description here

Right here is my code:

@implementation CDCFaderSlider

- (id)initWithFrame:(CGRect)body {
    self = [super initWithFrame: frame];
    
    if (self) {
        [self setOpaque: true];
        [self setTransform: CGAffineTransformMakeRotation((CGFloat)(M_PI * -0.5f))];
        [self constructSlider];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder: aDecoder];
    
    if (self) {
        [self setTransform: CGAffineTransformMakeRotation((CGFloat)(M_PI * -0.5f))];
        [self constructSlider];
    }
    return self;
}

#pragma mark - UIControl contact occasion monitoring

- (BOOL)beginTrackingWithTouch:(UITouch *)contact withEvent:(UIEvent *)occasion {
    CGPoint touchPoint = [touch locationInView: self];
    
    if (CGRectContainsPoint(CGRectInset(self.thumbRect, -12.0, -12.0), touchPoint)) {
        [self positionAndUpdateValueView];
        [self fadeValueViewInAndOut: true];
    }
    return [super beginTrackingWithTouch: touch withEvent: event];
}

- (BOOL)continueTrackingWithTouch:(UITouch *)contact withEvent:(UIEvent *)occasion {
    [self positionAndUpdateValueView];
    return [super continueTrackingWithTouch: touch withEvent: event];
}

- (void)cancelTrackingWithEvent:(UIEvent *)occasion {
    [self fadeValueViewInAndOut: false];
    [super cancelTrackingWithEvent: event];
}

- (void)endTrackingWithTouch:(UITouch *)contact withEvent:(UIEvent *)occasion {
    [self fadeValueViewInAndOut: false];
    [super endTrackingWithTouch: touch withEvent: event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)occasion {
    [self fadeValueViewInAndOut: false];
    [super touchesEnded: touches withEvent: event];
}

- (BOOL) pointInside:(CGPoint)level withEvent:(UIEvent*)occasion {
    CGRect bounds = self.bounds;
    bounds = CGRectInset(bounds, 0, -70);
    return CGRectContainsPoint(bounds, level);
}

#pragma mark - Helper strategies

- (void)constructSlider {
    self.valueView = [[CDCFaderValueView alloc] initWithFrame: CGRectZero];
    self.valueView.backgroundColor = UIColor.clearColor;
    self.valueView.alpha = 0.0;
    self.valueView.rework = CGAffineTransformMakeRotation((CGFloat)(M_PI * 0.5));
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self addSubview: self.valueView];
    });
}

- (void)fadeValueViewInAndOut:(BOOL)aFadeIn {
    [UIView animateWithDuration: 0.5 animations:^{
        if (aFadeIn) {
            self.valueView.alpha = 0.8;
        } else {
            self.valueView.alpha = 0.0;
        }
    } completion:^(BOOL finished){
    }];
}

- (void)positionAndUpdateValueView {
    CGRect ThumbRect = self.thumbRect;
    CGRect popupRect = CGRectOffset(ThumbRect, (CGFloat)ground(ThumbRect.measurement.width * 0.2), (CGFloat) - ground(ThumbRect.measurement.top * 0.5));
    self.valueView.body = CGRectInset(popupRect, -30, -10);
    self.valueView.worth = faderDBValues[(NSInteger)self.value];
}

#pragma mark - Property

- (CGRect)thumbRect {
    CGRect trackRect = [self trackRectForBounds: self.bounds];
    CGRect thumbR = [self thumbRectForBounds: self.bounds trackRect: trackRect value: self.value];
    
    return thumbR;
}

- (void)sendAction:(SEL)motion to:(id)goal forEvent:(UIEvent *)occasion {
    if (self.depend > 4) {
        [super sendAction: action to: target forEvent: event];
        self.depend = 0;
    } else {
        self.depend++;
    }
    
    if (self.monitoring == 0) {
        [super sendAction: action to: target forEvent: event];
    }
}

@finish

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles