Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

点快了,indicator跑偏掉了,还有选中的状态没有变化,帮你修复了下 #10

Open
HCat opened this issue May 23, 2017 · 0 comments

Comments

@HCat
Copy link

HCat commented May 23, 2017

simulator screen shot 2017 5 24 7 53 41
simulator screen shot 2017 5 24 7 53 46
simulator screen shot 2017 5 24 7 53 53

贴上修复方式,供你参考,如果说有更好的方式,渴望有你的不同见解

- (void)segmentDidSelectAtIndex:(NSUInteger)newIndex didDeselectAtIndex:(NSUInteger)oldIndex {

// update UI
if (_contentType == YUSegmentedControlContentTypeText) {
    
    for (Item *item in _items) {
        NSInteger index = [_items indexOfObject:item];
        UILabel *deselectedLabel = (UILabel *)(item.view);
        deselectedLabel.attributedText = [[NSAttributedString alloc] initWithString:_contents[index] attributes:_attributesNormal];
    }
    
    UILabel *selectedLabel = (UILabel *)(_items[newIndex].view);
    selectedLabel.attributedText = [[NSAttributedString alloc] initWithString:_contents[newIndex] attributes:_attributesSelected];

// UILabel *deselectedLabel = (UILabel *)(_items[oldIndex].view);
// deselectedLabel.attributedText = [[NSAttributedString alloc] initWithString:_contents[oldIndex] attributes:_attributesNormal];
}
else {
if (_selectedImages) {
UIImageView *selectedImageView = (UIImageView *)(_items[newIndex].view);
selectedImageView.image = _selectedImages[newIndex];
UIImageView *deselectedImageView = (UIImageView *)(_items[oldIndex].view);
deselectedImageView.image = _contents[oldIndex];
}
}
// animation
[self moveIndicatorFromIndex:oldIndex toIndex:newIndex];
}

  • (void)moveIndicatorFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {

// CABasicAnimation *animation;
// CGPoint position = _indicator.position;
// CGFloat position_x = position.x + CGRectGetWidth(_indicator.bounds) * (toIndex - fromIndex);
// position.x = position_x;
//
// if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
// animation = [CASpringAnimation animationWithKeyPath:@"position.x"];
// } else {
// animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
// }
// animation.toValue = [NSValue valueWithCGPoint:position];
// animation.duration = 0.25;

// indicator animate
CGRect frame = _indicator.frame;
frame.origin.x = CGRectGetWidth(_indicator.bounds) * toIndex ;
NSLog(@"%f",frame.origin.x);
[UIView animateWithDuration:kAnimationDuration
                      delay:0.0
     usingSpringWithDamping:0.66
      initialSpringVelocity:3.0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     _indicator.frame = frame;
    
} completion:^(BOOL finished) {
    if (finished) {
        _selectedSegmentIndex = toIndex;
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}];

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant