DZNSegmentedControl icon indicating copy to clipboard operation
DZNSegmentedControl copied to clipboard

when items is not in english, setSelectedSegmentIndex will has no effect

Open bellchen opened this issue 9 years ago • 4 comments

when i init control as follow

 _timeControl = [[DZNSegmentedControl alloc] initWithItems:@[NSLocalizedString(@"日榜", @""),
                                                                    NSLocalizedString(@"周榜", @""),
                                                                    NSLocalizedString(@"总榜", @"")]];
_timeControl.tintColor=FZLightColor;

when calling

 [self.timeControl setSelectedSegmentIndex:2 animated:YES];

segmentIndex's tintColor is not FZLightColor

when i init control as follow

_timeControl = [[DZNSegmentedControl alloc] initWithItems:@[NSLocalizedString(@"day", @""),
                                                                    NSLocalizedString(@"week", @""),
                                                                    NSLocalizedString(@"all", @"")]];
_timeControl.tintColor=FZLightColor;

that will effect when calling setSelectedSegmentIndex:animated

bellchen avatar Sep 11 '15 15:09 bellchen

I fix this issue by this way, i don't know whether it is right

- (void)setSelectedSegmentIndex:(NSInteger)segment animated:(BOOL)animated
{
    if (self.selectedSegmentIndex == segment || self.isTransitioning) {
        return;
    }

    [self unselectAllButtons];

    self.userInteractionEnabled = NO;

    _selectedSegmentIndex = segment;
    _transitioning = YES;
    void (^animations)() = ^void(){
        self.selectionIndicator.frame = [self selectionIndicatorRect];
    };

    void (^completion)(BOOL finished) = ^void(BOOL finished){
        self.userInteractionEnabled = YES;
        _transitioning = NO;
        [self.buttons[segment] setSelected:YES];//I add this line to fix 
    };

    if (animated) {
        CGFloat duration = (self.selectedSegmentIndex < 0.0f) ? 0.0f : self.animationDuration;
        CGFloat damping = !self.bouncySelectionIndicator ? : 0.65f;
        CGFloat velocity = !self.bouncySelectionIndicator ? : 0.5f;

        [UIView animateWithDuration:duration
                              delay:0.0f
             usingSpringWithDamping:damping
              initialSpringVelocity:velocity
                            options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut
                         animations:animations
                         completion:completion];
    }
    else {
        animations();
        completion(NO);
    }
}

the diff is in the completion block, i call ''' [self.buttons[segment] setSelected:YES];//I add this line to fix ''' to set the button selected

bellchen avatar Sep 11 '15 15:09 bellchen

Could your please fork this repo and submit a PR with the fix? Thanks!

dzenbot avatar Sep 15 '15 13:09 dzenbot

Is this related to https://github.com/dzenbot/DZNSegmentedControl/pull/56 ?

dzenbot avatar Sep 21 '15 15:09 dzenbot

yes, it is related to #56

bellchen avatar Sep 30 '15 06:09 bellchen