HMSegmentedControl源代码解析(10)
- UID
- 1066743
|
HMSegmentedControl源代码解析(10)
rect = CGRectMake(ceilf(rect.origin.x), ceilf(rect.origin.y), ceilf(rect.size.width), ceilf(rect.size.height));
//不用label 用 catextlayer
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.frame = rect;
titleLayer.alignmentMode = kCAAlignmentCenter;
if ([UIDevice currentDevice].systemVersion.floatValue < 10.0 ) {
titleLayer.truncationMode = kCATruncationEnd;
}
titleLayer.string = [self attributedTitleAtIndex:idx];
titleLayer.contentsScale = [[UIScreen mainScreen] scale];
[self.scrollView.layer addSublayer:titleLayer];
// Vertical Divider
// 判断是否显示分割线
if (self.isVerticalDividerEnabled && idx > 0) {
CALayer *verticalDividerLayer = [CALayer layer];
verticalDividerLayer.frame = rectDiv;
verticalDividerLayer.backgroundColor = self.verticalDividerColor.CGColor;
[self.scrollView.layer addSublayer:verticalDividerLayer];
}
[self addBackgroundAndBorderLayerWithRect:fullRect];
}];
} else if (self.type == HMSegmentedControlTypeImages) {
//当segment的type 等于图片的时候
[self.sectionImages enumerateObjectsUsingBlock:^(id iconImage, NSUInteger idx, BOOL *stop) {
UIImage *icon = iconImage;
CGFloat imageWidth = icon.size.width;
CGFloat imageHeight = icon.size.height;
CGFloat y = roundf(CGRectGetHeight(self.frame) - self.selectionIndicatorHeight) / 2 - imageHeight / 2 + ((self.selectionIndicatorLocation == HMSegmentedControlSelectionIndicatorLocationUp) ? self.selectionIndicatorHeight : 0);
CGFloat x = self.segmentWidth * idx + (self.segmentWidth - imageWidth)/2.0f;
CGRect rect = CGRectMake(x, y, imageWidth, imageHeight);
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = rect;
if (self.selectedSegmentIndex == idx) {
if (self.sectionSelectedImages) {
UIImage *highlightIcon = [self.sectionSelectedImages objectAtIndex:idx];
imageLayer.contents = (id)highlightIcon.CGImage;
} else {
imageLayer.contents = (id)icon.CGImage;
}
} else {
imageLayer.contents = (id)icon.CGImage;
}
[self.scrollView.layer addSublayer:imageLayer];
// Vertical Divider
if (self.isVerticalDividerEnabled && idx>0) {
CALayer *verticalDividerLayer = [CALayer layer];
verticalDividerLayer.frame = CGRectMake((self.segmentWidth * idx) - (self.verticalDividerWidth / 2), self.selectionIndicatorHeight * 2, self.verticalDividerWidth, self.frame.size.height-(self.selectionIndicatorHeight * 4));
verticalDividerLayer.backgroundColor = self.verticalDividerColor.CGColor;
[self.scrollView.layer addSublayer:verticalDividerLayer];
}
[self addBackgroundAndBorderLayerWithRect:rect];
}];
} else if (self.type == HMSegmentedControlTypeTextImages){
//当segment的type 等于图文的时候
[self.sectionImages enumerateObjectsUsingBlock:^(id iconImage, NSUInteger idx, BOOL *stop) {
UIImage *icon = iconImage;
CGFloat imageWidth = icon.size.width;
CGFloat imageHeight = icon.size.height;
CGFloat stringHeight = [self measureTitleAtIndex:idx].height;
CGFloat yOffset = roundf(((CGRectGetHeight(self.frame) - self.selectionIndicatorHeight) / 2) - (stringHeight / 2));
CGFloat imageXOffset = self.segmentEdgeInset.left; // Start with edge inset
CGFloat textXOffset = self.segmentEdgeInset.left;
CGFloat textWidth = 0;
if (self.segmentWidthStyle == HMSegmentedControlSegmentWidthStyleFixed) {
imageXOffset = (self.segmentWidth * idx) + (self.segmentWidth / 2.0f) - (imageWidth / 2.0f);
textXOffset = self.segmentWidth * idx;
textWidth = self.segmentWidth;
} else if (self.segmentWidthStyle == HMSegmentedControlSegmentWidthStyleDynamic) {
// When we are drawing dynamic widths, we need to loop the widths array to calculate the xOffset
CGFloat xOffset = 0;
NSInteger i = 0;
for (NSNumber *width in self.segmentWidthsArray) {
if (idx == i) {
break;
}
xOffset = xOffset + [width floatValue];
i++;
}
imageXOffset = xOffset + ([self.segmentWidthsArray[idx] floatValue] / 2.0f) - (imageWidth / 2.0f); //(self.segmentWidth / 2.0f) - (imageWidth / 2.0f)
textXOffset = xOffset;
textWidth = [self.segmentWidthsArray[idx] floatValue];
}
CGFloat imageYOffset = roundf((CGRectGetHeight(self.frame) - self.selectionIndicatorHeight) / 2.0f);
CGRect imageRect = CGRectMake(imageXOffset, imageYOffset, imageWidth, imageHeight);
CGRect textRect = CGRectMake(textXOffset, yOffset, textWidth, stringHeight);
// Fix rect position/size to avoid blurry labels
textRect = CGRectMake(ceilf(textRect.origin.x), ceilf(textRect.origin.y), ceilf(textRect.size.width), ceilf(textRect.size.height));
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.frame = textRect;
titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.string = [self attributedTitleAtIndex:idx];
if ([UIDevice currentDevice].systemVersion.floatValue < 10.0 ) {
titleLayer.truncationMode = kCATruncationEnd;
}
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = imageRect;
if (self.selectedSegmentIndex == idx) {
if (self.sectionSelectedImages) {
UIImage *highlightIcon = [self.sectionSelectedImages objectAtIndex:idx];
imageLayer.contents = (id)highlightIcon.CGImage;
} else {
imageLayer.contents = (id)icon.CGImage;
}
} else {
imageLayer.contents = (id)icon.CGImage;
}
[self.scrollView.layer addSublayer:imageLayer];
titleLayer.contentsScale = [[UIScreen mainScreen] scale];
[self.scrollView.layer addSublayer:titleLayer];
[self addBackgroundAndBorderLayerWithRect:imageRect];
}];
}
// Add the selection indicators
// 添加选中的指示器
if (self.selectedSegmentIndex != HMSegmentedControlNoSegment) {
if (self.selectionStyle == HMSegmentedControlSelectionStyleArrow) {
if (!self.selectionIndicatorArrowLayer.superlayer) {
[self setArrowFrame];
[self.scrollView.layer addSublayer:self.selectionIndicatorArrowLayer];
}
} else {
if (!self.selectionIndicatorStripLayer.superlayer) {
self.selectionIndicatorStripLayer.frame = [self frameForSelectionIndicator];
[self.scrollView.layer addSublayer:self.selectionIndicatorStripLayer];
if (self.selectionStyle == HMSegmentedControlSelectionStyleBox && !self.selectionIndicatorBoxLayer.superlayer) {
self.selectionIndicatorBoxLayer.frame = [self frameForFillerSelectionIndicator];
[self.scrollView.layer insertSublayer:self.selectionIndicatorBoxLayer atIndex:0];
}
}
}
}
} |
|
|
|
|
|