Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ TQStarRatingView

IOS 星星评分视图控件,点击和滑动评分。

### 我增加了什么功能

- delegate 可以用于设置星星范围控制

```
- (BOOL)starRatingView:(TQStarRatingView *)view shouldShowScore:(float)score;
```
- 属性fillType,用于控制星星显示粒度

```
/**
填充粒度
*/
typedef enum : NSUInteger {
StartFillTypeAll, //自由填充 粒度是0.01
StartFillTypeHalf, //半颗星
StartFillTypeFull, //一颗星
} StartFillType;
```

#### 控件效果

![Image text](http://github.com/TinyQ/TQStarRatingView/raw/master/READMEIMAGE/TQStarRatingView.gif)
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified TQStarRatingView/TQStarRating/Image/backgroundStar@2x.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified TQStarRatingView/TQStarRating/Image/foregroundStar@2x.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 18 additions & 4 deletions TQStarRatingView/TQStarRating/TQStarRatingView.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@
@protocol StarRatingViewDelegate <NSObject>

@optional
-(void)starRatingView:(TQStarRatingView *)view score:(float)score;

- (void)starRatingView:(TQStarRatingView *)view score:(float)score;

//可以用于设置星星范围控制
- (BOOL)starRatingView:(TQStarRatingView *)view shouldShowScore:(float)score;

@end

@interface TQStarRatingView : UIView
/**
填充粒度
*/
typedef enum : NSUInteger {
StartFillTypeAll, //自由填充 粒度是0.1
StartFillTypeHalf, //半颗星
StartFillTypeFull, //一颗星
} StartFillType;

@property (nonatomic, readonly) int numberOfStar;
@interface TQStarRatingView : UIView

@property (nonatomic, weak) id <StarRatingViewDelegate> delegate;
@property (nonatomic, readonly) int numberOfStar;
//设置星星填充的最小粒度 默认是StartFillTypeAll
@property (nonatomic, assign) StartFillType fillType;

/**
* 初始化TQStarRatingView
Expand Down Expand Up @@ -54,4 +68,4 @@

#define kBACKGROUND_STAR @"backgroundStar"
#define kFOREGROUND_STAR @"foregroundStar"
#define kNUMBER_OF_STAR 5
#define kNUMBER_OF_STAR 5
40 changes: 37 additions & 3 deletions TQStarRatingView/TQStarRating/TQStarRatingView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ - (id)initWithFrame:(CGRect)frame numberOfStar:(int)number
return self;
}

- (void)commonInit
{
- (void)commonInit{
self.fillType = StartFillTypeAll;

self.starBackgroundView = [self buidlStarViewWithImageName:kBACKGROUND_STAR];
self.starForegroundView = [self buidlStarViewWithImageName:kFOREGROUND_STAR];
[self addSubview:self.starBackgroundView];
Expand Down Expand Up @@ -187,7 +188,40 @@ - (void)changeStarForegroundViewWithPoint:(CGPoint)point
}

NSString * str = [NSString stringWithFormat:@"%0.2f",p.x / self.frame.size.width];
float score = [str floatValue];
CGFloat score = [str floatValue];

if (self.fillType == StartFillTypeFull) {
//which star is touch on
NSUInteger part = (1.0 / _numberOfStar) * 100;
NSUInteger present = score * 100;
NSUInteger index = present / part;
if (present % part != 0) {
index++;
}
score = index * part / 100.f;
}else if (self.fillType == StartFillTypeHalf){
NSUInteger part = (1.0 / _numberOfStar) * 100;
NSUInteger present = score * 100;
NSUInteger index = present / part;
NSUInteger gws = 0;
if (present % part != 0) {
int tmp = present*1.f / part*1.f * 10;
gws = tmp % 10;
}
if (gws >= 5) {
score = (index+1.0) * part / 100.f;
}else{
score = (index+0.5) * part / 100.f;
}
}

BOOL shouldShow = YES;
if ([self.delegate respondsToSelector:@selector(starRatingView:shouldShowScore:)]) {
shouldShow = [self.delegate starRatingView:self shouldShowScore:score];
}
if (!shouldShow) return ;


p.x = score * self.frame.size.width;
self.starForegroundView.frame = CGRectMake(0, 0, p.x, self.frame.size.height);

Expand Down
9 changes: 1 addition & 8 deletions TQStarRatingView/TQStarRatingView.xcodeproj/project.pbxproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
58F0AEF517CDD614009FE3CB /* TQViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 58F0AEF417CDD614009FE3CB /* TQViewController.m */; };
58F0AEF817CDD614009FE3CB /* TQViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 58F0AEF617CDD614009FE3CB /* TQViewController.xib */; };
58F0AF0117CDD6A3009FE3CB /* TQStarRatingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 58F0AF0017CDD6A3009FE3CB /* TQStarRatingView.m */; };
58F0AF0717CDDA9D009FE3CB /* backgroundStar.png in Resources */ = {isa = PBXBuildFile; fileRef = 58F0AF0317CDDA9D009FE3CB /* backgroundStar.png */; };
58F0AF0817CDDA9D009FE3CB /* backgroundStar@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 58F0AF0417CDDA9D009FE3CB /* backgroundStar@2x.png */; };
58F0AF0917CDDA9D009FE3CB /* foregroundStar.png in Resources */ = {isa = PBXBuildFile; fileRef = 58F0AF0517CDDA9D009FE3CB /* foregroundStar.png */; };
58F0AF0A17CDDA9D009FE3CB /* foregroundStar@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 58F0AF0617CDDA9D009FE3CB /* foregroundStar@2x.png */; };
/* End PBXBuildFile section */

Expand All @@ -44,9 +42,7 @@
58F0AEF717CDD614009FE3CB /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/TQViewController.xib; sourceTree = "<group>"; };
58F0AEFF17CDD6A3009FE3CB /* TQStarRatingView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TQStarRatingView.h; sourceTree = "<group>"; };
58F0AF0017CDD6A3009FE3CB /* TQStarRatingView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TQStarRatingView.m; sourceTree = "<group>"; };
58F0AF0317CDDA9D009FE3CB /* backgroundStar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = backgroundStar.png; sourceTree = "<group>"; };
58F0AF0417CDDA9D009FE3CB /* backgroundStar@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "backgroundStar@2x.png"; sourceTree = "<group>"; };
58F0AF0517CDDA9D009FE3CB /* foregroundStar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = foregroundStar.png; sourceTree = "<group>"; };
58F0AF0617CDDA9D009FE3CB /* foregroundStar@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "foregroundStar@2x.png"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -132,9 +128,7 @@
58F0AF0217CDDA9D009FE3CB /* Image */ = {
isa = PBXGroup;
children = (
58F0AF0317CDDA9D009FE3CB /* backgroundStar.png */,
58F0AF0417CDDA9D009FE3CB /* backgroundStar@2x.png */,
58F0AF0517CDDA9D009FE3CB /* foregroundStar.png */,
58F0AF0617CDDA9D009FE3CB /* foregroundStar@2x.png */,
);
path = Image;
Expand Down Expand Up @@ -197,9 +191,7 @@
58F0AEF017CDD614009FE3CB /* Default@2x.png in Resources */,
58F0AEF217CDD614009FE3CB /* Default-568h@2x.png in Resources */,
58F0AEF817CDD614009FE3CB /* TQViewController.xib in Resources */,
58F0AF0717CDDA9D009FE3CB /* backgroundStar.png in Resources */,
58F0AF0817CDDA9D009FE3CB /* backgroundStar@2x.png in Resources */,
58F0AF0917CDDA9D009FE3CB /* foregroundStar.png in Resources */,
58F0AF0A17CDDA9D009FE3CB /* foregroundStar@2x.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -337,6 +329,7 @@
58F0AEFD17CDD614009FE3CB /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Empty file modified TQStarRatingView/TQStarRatingView/Default-568h@2x.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified TQStarRatingView/TQStarRatingView/Default.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified TQStarRatingView/TQStarRatingView/Default@2x.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified TQStarRatingView/TQStarRatingView/TQAppDelegate.h
100644 → 100755
Empty file.
Empty file modified TQStarRatingView/TQStarRatingView/TQAppDelegate.m
100644 → 100755
Empty file.
Empty file modified TQStarRatingView/TQStarRatingView/TQStarRatingView-Info.plist
100644 → 100755
Empty file.
Empty file modified TQStarRatingView/TQStarRatingView/TQStarRatingView-Prefix.pch
100644 → 100755
Empty file.
Empty file modified TQStarRatingView/TQStarRatingView/TQViewController.h
100644 → 100755
Empty file.
9 changes: 9 additions & 0 deletions TQStarRatingView/TQStarRatingView/TQViewController.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ - (void)viewDidLoad
_starRatingView.delegate = self;
[self.view addSubview:_starRatingView];

_starRatingView.fillType = StartFillTypeHalf;
}

-(void)starRatingView:(TQStarRatingView *)view score:(float)score
Expand All @@ -34,6 +35,14 @@ -(void)starRatingView:(TQStarRatingView *)view score:(float)score
[self.nibStarRatingView setScore:score withAnimation:YES];
}


- (BOOL)starRatingView:(TQStarRatingView *)view shouldShowScore:(float)score{
if (score <= 0.f) {
return NO;
}
return YES;
}

- (IBAction)scoreButtonTouchUpInside:(id)sender
{
//设置分数。参数需要在0-1之间。
Expand Down
Empty file modified TQStarRatingView/TQStarRatingView/en.lproj/InfoPlist.strings
100644 → 100755
Empty file.
Empty file modified TQStarRatingView/TQStarRatingView/en.lproj/TQViewController.xib
100644 → 100755
Empty file.
Empty file modified TQStarRatingView/TQStarRatingView/main.m
100644 → 100755
Empty file.