HCSStarRatingView
是一个用于提供用户基本星级评分界面的 UIControl
子类。
它支持所有设备分辨率,尽管它渲染星号时不需要图片(感谢 PaintCode),但如果你愿意,也可以提供自定义图片。
您也可以通过将 HCSStarRatingView.{h|m}
复制到您的项目或包括项目到您的项目/工作区中来手动安装,就像在 Sample
中所做的那样。
创建一个新实例,并设置您想要定制的属性。
HCSStarRatingView *starRatingView = [[HCSStarRatingView alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];
starRatingView.maximumValue = 10;
starRatingView.minimumValue = 0;
starRatingView.value = 0;
starRatingView.tintColor = [UIColor redColor];
[starRatingView addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:starRatingView];
HCSStarRatingView
也很适合自动布局,因此您可以设置一些约束而不是仅提供一个框架(检查示例项目)!
starRatingView.allowsHalfStars = YES;
starRatingView.value = 2.5f;
启用 accurateHalfStars
以获得更精确的评分(也适用于图像)!
starRatingView.accurateHalfStars = YES;
在 HCSStarRatingView
中使用自定义图片和设置属性一样简单。您只需要设置 emptyStarImage
和 filledStarImage
,但如果您的需要,您也可以提供半星图像到 halfStarImage
starRatingView.emptyStarImage = [UIImage imageNamed:@"heart-empty"];
starRatingView.halfStarImage = [UIImage imageNamed:@"heart-half"]; // optional
starRatingView.filledStarImage = [UIImage imageNamed:@"heart-full"];
如果您想以编程方式使用模板图像,只需确保它们具有正确的 渲染模式
starRatingView.emptyStarImage = [[UIImage imageNamed:@"heart-empty"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
starRatingView.halfStarImage = [[UIImage imageNamed:@"heart-half"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; // optional
starRatingView.filledStarImage = [[UIImage imageNamed:@"heart-full"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
HCSStarRatingView
也实现了 IB_DESIGNABLE
和 IBInspectable
,您可以在 Interface Builder 中完全自定义它。
注意:为了在 Interface Builder 中使用模板图像,您必须转到该图像属性在你的 Asset Catalog 并将 Render As
设置更改为 Template Image
。
对于有特殊需求的使用者,应该能够完全读取和与 HCSStarRatingView
互动,因为它完全支持 VoiceOver。
如果您或您的用户有其他特定需求,并且在使用此控件时遇到问题,请与我联系,以便我们一起解决!:-)
Hugo Sousa
HCSStarRatingView 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。