Skip to content

Commit

Permalink
Merge pull request #1 from TalkingJourney/develop
Browse files Browse the repository at this point in the history
add center toast style
  • Loading branch information
自由流水 authored Jan 13, 2018
2 parents cef124c + e7dfcac commit 510da61
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 138 deletions.
38 changes: 28 additions & 10 deletions SCIndexView/SCIndexView.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ - (void)showIndicator:(BOOL)animated
if (!self.indicator.hidden || self.currentSection < 0 || self.currentSection >= self.subTextLayers.count) return;

CATextLayer *textLayer = self.subTextLayers[self.currentSection];
self.indicator.center = CGPointMake(self.bounds.size.width - self.indicator.bounds.size.width / 2 - self.configuration.indicatorRightMargin, textLayer.position.y);
if (self.configuration.indexViewStyle == SCIndexViewStyleDefault) {
self.indicator.center = CGPointMake(self.bounds.size.width - self.indicator.bounds.size.width / 2 - self.configuration.indicatorRightMargin, textLayer.position.y);
}
self.indicator.text = textLayer.string;

if (animated) {
Expand Down Expand Up @@ -346,20 +348,36 @@ - (UILabel *)indicator
{
if (!_indicator) {
_indicator = [UILabel new];
_indicator.backgroundColor = self.configuration.indicatorBackgroundColor;
_indicator.layer.backgroundColor = self.configuration.indicatorBackgroundColor.CGColor;
_indicator.textColor = self.configuration.indicatorTextColor;
_indicator.font = self.configuration.indicatorTextFont;
_indicator.textAlignment = NSTextAlignmentCenter;
_indicator.hidden = YES;

CGFloat indicatorRadius = self.configuration.indicatorHeight / 2;
CGFloat sinPI_4_Radius = sin(M_PI_4) * indicatorRadius;
_indicator.bounds = CGRectMake(0, 0, (4 * sinPI_4_Radius), 2 * indicatorRadius);

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [self drawIndicatorPath].CGPath;
_indicator.layer.mask = maskLayer;

switch (self.configuration.indexViewStyle) {
case SCIndexViewStyleDefault:
{
CGFloat indicatorRadius = self.configuration.indicatorHeight / 2;
CGFloat sinPI_4_Radius = sin(M_PI_4) * indicatorRadius;
_indicator.bounds = CGRectMake(0, 0, (4 * sinPI_4_Radius), 2 * indicatorRadius);

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [self drawIndicatorPath].CGPath;
_indicator.layer.mask = maskLayer;
}
break;

case SCIndexViewStyleCenterToast:
{
_indicator.bounds = CGRectMake(0, 0, self.configuration.indicatorHeight, self.configuration.indicatorHeight);
_indicator.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
_indicator.layer.cornerRadius = self.configuration.indicatorCornerRadius;
}
break;

default:
break;
}
}
return _indicator;
}
Expand Down
38 changes: 24 additions & 14 deletions SCIndexView/SCIndexViewConfiguration.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, SCIndexViewStyle) {
SCIndexViewStyleDefault = 0, // 指向点
SCIndexViewStyleCenterToast, // 中心提示弹层
};

@interface SCIndexViewConfiguration : NSObject

@property (nonatomic, assign, readonly) SCIndexViewStyle indexViewStyle; // 索引元素之间间隔距离

@property (nonatomic, strong) UIColor *indicatorBackgroundColor; // 指示器背景颜色
@property (nonatomic, strong) UIColor *indicatorTextColor; // 指示器文字颜色
@property (nonatomic, strong) UIFont *indicatorTextFont; // 指示器文字字体
@property (nonatomic, assign) CGFloat indicatorHeight; // 指示器高度
@property (nonatomic, assign) CGFloat indicatorRightMargin; // 指示器距离右边屏幕距离
@property (nonatomic, assign) CGFloat indicatorRightMargin; // 指示器距离右边屏幕距离(default有效)
@property (nonatomic, assign) CGFloat indicatorCornerRadius; // 指示器圆角半径(centerToast有效)

@property (nonatomic, strong) UIColor *indexItemBackgroundColor; // 索引元素背景颜色
@property (nonatomic, strong) UIColor *indexItemTextColor; // 索引元素文字颜色
Expand All @@ -17,18 +25,20 @@
@property (nonatomic, assign) CGFloat indexItemRightMargin; // 索引元素距离右边屏幕距离
@property (nonatomic, assign) CGFloat indexItemsSpace; // 索引元素之间间隔距离

+ (instancetype)configuration;
+ (instancetype)configurationWithIndicatorBackgroundColor:(UIColor *)indicatorBackgroundColor
indicatorTextColor:(UIColor *)indicatorTextColor
indicatorTextFont:(UIFont *)indicatorTextFont
indicatorHeight:(CGFloat)indicatorHeight
indicatorRightMargin:(CGFloat)indicatorRightMargin
indexItemBackgroundColor:(UIColor *)indexItemBackgroundColor
indexItemTextColor:(UIColor *)indexItemTextColor
indexItemSelectedBackgroundColor:(UIColor *)indexItemSelectedBackgroundColor
indexItemSelectedTextColor:(UIColor *)indexItemSelectedTextColor
indexItemHeight:(CGFloat)indexItemHeight
indexItemRightMargin:(CGFloat)indexItemRightMargin
indexItemsSpace:(CGFloat)indexItemsSpace;
+ (instancetype)configurationWithIndexViewStyle:(SCIndexViewStyle)indexViewStyle;
+ (instancetype)configurationWithIndexViewStyle:(SCIndexViewStyle)indexViewStyle
indicatorBackgroundColor:(UIColor *)indicatorBackgroundColor
indicatorTextColor:(UIColor *)indicatorTextColor
indicatorTextFont:(UIFont *)indicatorTextFont
indicatorHeight:(CGFloat)indicatorHeight
indicatorRightMargin:(CGFloat)indicatorRightMargin
indicatorCornerRadius:(CGFloat)indicatorCornerRadius
indexItemBackgroundColor:(UIColor *)indexItemBackgroundColor
indexItemTextColor:(UIColor *)indexItemTextColor
indexItemSelectedBackgroundColor:(UIColor *)indexItemSelectedBackgroundColor
indexItemSelectedTextColor:(UIColor *)indexItemSelectedTextColor
indexItemHeight:(CGFloat)indexItemHeight
indexItemRightMargin:(CGFloat)indexItemRightMargin
indexItemsSpace:(CGFloat)indexItemsSpace;

@end
91 changes: 66 additions & 25 deletions SCIndexView/SCIndexViewConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,86 @@
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}

@interface SCIndexViewConfiguration ()

@property (nonatomic, assign) SCIndexViewStyle indexViewStyle; // 索引元素之间间隔距离

@end

@implementation SCIndexViewConfiguration

+ (instancetype)configuration
@synthesize indexViewStyle = _indexViewStyle;

+ (instancetype)configurationWithIndexViewStyle:(SCIndexViewStyle)indexViewStyle
{
return [self configurationWithIndicatorBackgroundColor:SCGetColor(200, 200, 200, 1)
indicatorTextColor:[UIColor whiteColor]
indicatorTextFont:[UIFont systemFontOfSize:38]
indicatorHeight:50
indicatorRightMargin:40
indexItemBackgroundColor:[UIColor clearColor]
indexItemTextColor:[UIColor darkGrayColor]
indexItemSelectedBackgroundColor:SCGetColor(40, 170, 40, 1)
indexItemSelectedTextColor:[UIColor whiteColor]
indexItemHeight:15
indexItemRightMargin:5
indexItemsSpace:5];
UIColor *indicatorBackgroundColor, *indicatorTextColor;
UIFont *indicatorTextFont;
CGFloat indicatorHeight;
switch (indexViewStyle) {
case SCIndexViewStyleDefault:
{
indicatorBackgroundColor = SCGetColor(200, 200, 200, 1);
indicatorTextColor = [UIColor whiteColor];
indicatorTextFont = [UIFont systemFontOfSize:38];
indicatorHeight = 50;
}
break;

case SCIndexViewStyleCenterToast:
{
indicatorBackgroundColor = SCGetColor(200, 200, 200, 0.8);
indicatorTextColor = [UIColor whiteColor];
indicatorTextFont = [UIFont systemFontOfSize:60];
indicatorHeight = 120;
}
break;

default:
return nil;
break;
}

return [self configurationWithIndexViewStyle:indexViewStyle
indicatorBackgroundColor:indicatorBackgroundColor
indicatorTextColor:indicatorTextColor
indicatorTextFont:indicatorTextFont
indicatorHeight:indicatorHeight
indicatorRightMargin:40
indicatorCornerRadius:10
indexItemBackgroundColor:[UIColor clearColor]
indexItemTextColor:[UIColor darkGrayColor]
indexItemSelectedBackgroundColor:SCGetColor(40, 170, 40, 1)
indexItemSelectedTextColor:[UIColor whiteColor]
indexItemHeight:15
indexItemRightMargin:5
indexItemsSpace:5];
}

+ (instancetype)configurationWithIndicatorBackgroundColor:(UIColor *)indicatorBackgroundColor
indicatorTextColor:(UIColor *)indicatorTextColor
indicatorTextFont:(UIFont *)indicatorTextFont
indicatorHeight:(CGFloat)indicatorHeight
indicatorRightMargin:(CGFloat)indicatorRightMargin
indexItemBackgroundColor:(UIColor *)indexItemBackgroundColor
indexItemTextColor:(UIColor *)indexItemTextColor
indexItemSelectedBackgroundColor:(UIColor *)indexItemSelectedBackgroundColor
indexItemSelectedTextColor:(UIColor *)indexItemSelectedTextColor
indexItemHeight:(CGFloat)indexItemHeight
indexItemRightMargin:(CGFloat)indexItemRightMargin
indexItemsSpace:(CGFloat)indexItemsSpace
+ (instancetype)configurationWithIndexViewStyle:(SCIndexViewStyle)indexViewStyle
indicatorBackgroundColor:(UIColor *)indicatorBackgroundColor
indicatorTextColor:(UIColor *)indicatorTextColor
indicatorTextFont:(UIFont *)indicatorTextFont
indicatorHeight:(CGFloat)indicatorHeight
indicatorRightMargin:(CGFloat)indicatorRightMargin
indicatorCornerRadius:(CGFloat)indicatorCornerRadius
indexItemBackgroundColor:(UIColor *)indexItemBackgroundColor
indexItemTextColor:(UIColor *)indexItemTextColor
indexItemSelectedBackgroundColor:(UIColor *)indexItemSelectedBackgroundColor
indexItemSelectedTextColor:(UIColor *)indexItemSelectedTextColor
indexItemHeight:(CGFloat)indexItemHeight
indexItemRightMargin:(CGFloat)indexItemRightMargin
indexItemsSpace:(CGFloat)indexItemsSpace
{
SCIndexViewConfiguration *configuration = [self new];
if (!configuration) return nil;

configuration.indexViewStyle = indexViewStyle;
configuration.indicatorBackgroundColor = indicatorBackgroundColor;
configuration.indicatorTextColor = indicatorTextColor;
configuration.indicatorTextFont = indicatorTextFont;
configuration.indicatorHeight = indicatorHeight;
configuration.indicatorRightMargin = indicatorRightMargin;
configuration.indicatorCornerRadius = indicatorCornerRadius;

configuration.indexItemBackgroundColor = indexItemBackgroundColor;
configuration.indexItemTextColor = indexItemTextColor;
Expand Down
6 changes: 6 additions & 0 deletions SCIndexViewDemo/SCIndexViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
1A3B56772009F0B40073C98F /* SectionItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A3B56742009F0B40073C98F /* SectionItem.m */; };
1A3B567E2009F2D30073C98F /* NSObject+YYModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A3B567B2009F2D30073C98F /* NSObject+YYModel.m */; };
1A3B567F2009F2D30073C98F /* YYClassInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A3B567C2009F2D30073C98F /* YYClassInfo.m */; };
B874C548200A53CB0040DA53 /* SCIndexViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B874C547200A53CB0040DA53 /* SCIndexViewController.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -44,6 +45,8 @@
1A3B567B2009F2D30073C98F /* NSObject+YYModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+YYModel.m"; sourceTree = "<group>"; };
1A3B567C2009F2D30073C98F /* YYClassInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYClassInfo.m; sourceTree = "<group>"; };
1A3B567D2009F2D30073C98F /* NSObject+YYModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+YYModel.h"; sourceTree = "<group>"; };
B874C546200A53CB0040DA53 /* SCIndexViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SCIndexViewController.h; sourceTree = "<group>"; };
B874C547200A53CB0040DA53 /* SCIndexViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCIndexViewController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -85,6 +88,8 @@
1A3B56422009EE370073C98F /* AppDelegate.m */,
1A3B56442009EE370073C98F /* ViewController.h */,
1A3B56452009EE370073C98F /* ViewController.m */,
B874C546200A53CB0040DA53 /* SCIndexViewController.h */,
B874C547200A53CB0040DA53 /* SCIndexViewController.m */,
1A3B56472009EE370073C98F /* Main.storyboard */,
1A3B564A2009EE370073C98F /* Assets.xcassets */,
1A3B564C2009EE370073C98F /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -197,6 +202,7 @@
1A3B567F2009F2D30073C98F /* YYClassInfo.m in Sources */,
1A3B56712009F01F0073C98F /* SCIndexViewConfiguration.m in Sources */,
1A3B56432009EE370073C98F /* AppDelegate.m in Sources */,
B874C548200A53CB0040DA53 /* SCIndexViewController.m in Sources */,
1A3B56722009F01F0073C98F /* SCIndexView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
24 changes: 13 additions & 11 deletions SCIndexViewDemo/SCIndexViewDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -19,28 +18,31 @@
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="ATY-XI-gEt" kind="relationship" relationship="rootViewController" id="Gh2-oy-vc5"/>
<segue destination="KtG-l5-xRF" kind="relationship" relationship="rootViewController" id="b9A-Xv-f1z"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="tkF-1N-I3X" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-792" y="146"/>
</scene>
<!--View Controller-->
<scene sceneID="B6s-rZ-j3N">
<scene sceneID="b9d-3V-oz2">
<objects>
<viewController id="ATY-XI-gEt" customClass="ViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="VFA-16-QkW">
<tableViewController id="KtG-l5-xRF" customClass="ViewController" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="fps-kx-b6V">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<viewLayoutGuide key="safeArea" id="l9Z-yO-oQQ"/>
</view>
<navigationItem key="navigationItem" id="lqW-S4-kZq"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="zNq-jq-7q8" userLabel="First Responder" sceneMemberID="firstResponder"/>
<connections>
<outlet property="dataSource" destination="KtG-l5-xRF" id="xNN-B6-J9g"/>
<outlet property="delegate" destination="KtG-l5-xRF" id="deB-6E-vjq"/>
</connections>
</tableView>
<navigationItem key="navigationItem" id="wFu-Yt-dvh"/>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="IN4-L7-5vg" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-68" y="145"/>
<point key="canvasLocation" x="-132" y="145"/>
</scene>
</scenes>
</document>
9 changes: 9 additions & 0 deletions SCIndexViewDemo/SCIndexViewDemo/SCIndexViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#import <UIKit/UIKit.h>
#import "SCIndexViewConfiguration.h"

@interface SCIndexViewController : UIViewController

@property (nonatomic, assign) SCIndexViewStyle indexViewStyle;

@end
Loading

0 comments on commit 510da61

Please sign in to comment.