pmick / NXTSegmentedControl

The styled swipe-able segmented control used in Next
MIT License
98 stars 8 forks source link

If you tap the unselected segment the animation is disrupted #6

Closed reggian closed 7 years ago

reggian commented 7 years ago

You can fix this by starting the animation from current state like so:

- (void)_moveThumbToSelectedSegment:(NSInteger)index animated:(BOOL)animated {

    CGRect newSelectionRect = [self _rectForSegmentAtIndex:index];
    if (animated) {
        CGPoint fromCenter = (self.thumb.layer.presentationLayer
                              ? self.thumb.layer.presentationLayer.position
                              : self.thumb.center);
        CGPoint newCenter = [self _centerForSegmentAtIndex:index];

        CABasicAnimation *thumbAnimation = [self _thumbUpdateAnimationWithFromCenter:fromCenter toCenter:newCenter];
        [self.thumb.layer addAnimation:thumbAnimation forKey:@"basic"];
        self.thumb.layer.position = newCenter;

        CABasicAnimation *maskAnimation = [self _thumbUpdateAnimationWithFromCenter:fromCenter toCenter:newCenter];
        [self.thumbShowLayer.layer addAnimation:maskAnimation forKey:@"position"];
        self.thumbShowLayer.layer.position = newCenter;
    } else {
        [self _moveThumbToNewFrame:newSelectionRect];
    }
}
pmick commented 7 years ago

Closing and released this fix with 1.0.7. Thanks for the issue and fix 👍