这篇文章主要介绍了iOS评分(评价)星星图打分功能,评分视图分为展示和评分两种,具体详情大家可以通过本文详细学习
下载地址:https://github.com/littleSunZheng/StarGradeView
//
// StarGradeView.h
// EcmCustomer
//
// Created by 郑鹏 on 2016/11/4.
// Copyright © 2016年 张进. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol StarGradeViewDelegate <NSObject>
- (void)didSelectedIndex:(NSString *)index;
@end
@interface StarGradeView : UIView
@property (nonatomic, assign) id <StarGradeViewDelegate> delegate;
// 视图frame 和 想有几个星星(取决于设计 5个常用 或者10个 )
- (instancetype)initWithFrame:(CGRect)frame withtNumberOfPart:(NSInteger)num;
@end
//
// StarGradeView.m
// EcmCustomer
//
// Created by 郑鹏 on 2016/11/4.
// Copyright © 2016年 张进. All rights reserved.
//
#import "StarGradeView.h"
@interface StarGradeView(){
UIView *_btnView;//放星星的背景view
UIView *_shouView;//放星星的背景view
CGFloat _height;//星星的高
NSInteger _btnNum;//按钮的数量
NSInteger _index;//第几个
}
@end
@implementation StarGradeView
- (instancetype)initWithFrame:(CGRect)frame withtNumberOfPart:(NSInteger)num{
self = [super initWithFrame:frame];
_height = frame.size.height;
_btnNum = num;
CGFloat selfW = frame.size.width;
CGFloat starW = frame.size.height;
_btnView = [[UIView alloc] initWithFrame:CGRectMake((selfW - starW*num)/2 , 0, starW*num, starW)];
for (int i = 0; i< num; i++) {
UIButton *starBtn = [UIButton buttonWithType:UIButtonTypeCustom];
starBtn.frame = CGRectMake(starW * i, 0, starW, starW);
[starBtn setImage:[UIImage imageNamed:@"star_off"] forState:UIControlStateNormal];
[starBtn setImage:[UIImage imageNamed:@"star_on"] forState:UIControlStateSelected];
starBtn.tag = 1991+i;
[starBtn setAdjustsImageWhenHighlighted:NO];
[_btnView addSubview:starBtn];
}
_shouView = [[UIView alloc] initWithFrame:CGRectMake(0 , 0, starW*num, starW)];
[_btnView addSubview:_shouView];
[self addSubview:_btnView];
return self;
}
//滑动需要的。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [self getTouchPoint:touches];
int j = (int)(point.x/_height);
_index = j;
for (NSInteger i = 0; i < _btnNum; i++) {
if (i<=j) {
UIButton *btn = [_btnView viewWithTag:i+1991];
btn.selected = YES;
}else{
UIButton *btn = [_btnView viewWithTag:i+1991];
btn.selected = NO;
}
}
}
//滑动需要的。
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [self getTouchPoint:touches];
int j = (int)(point.x/_height);
_index = j;
for (NSInteger i = 0; i < _btnNum; i++) {
if (i<=j) {
UIButton *btn = [_btnView viewWithTag:i+1991];
btn.selected = YES;
}else{
UIButton *btn = [_btnView viewWithTag:i+1991];
btn.selected = NO;
}
}
}
//滑动需要的。
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
if ([self.delegate respondsToSelector:@selector(didSelectedIndex:)]) {
[self.delegate didSelectedIndex:[NSString stringWithFormat:@"%ld",_index+1]];
}
}
//取到 手势 在屏幕上点的 位置point
- (CGPoint)getTouchPoint:(NSSet<UITouch *>*)touches{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:_shouView];
return point;
}
//如果点击的范围在 按钮的区域
- (BOOL)pointInBtn:(UIButton *)btn WithPoint:(CGPoint)point{
if (CGRectContainsPoint(btn.frame, point)) {
return YES;
}else{
return NO;
}
return nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
使用时:
StarGradeView *view = [[StarGradeView alloc] initWithFrame:CGRectMake(0, 100, 375, 40) withtNumberOfPart:5];
view.delegate = self;
[self.view addSubview:view];
//并实现代理方法
- (void)didSelectedIndex:(NSString *)index{
NSLog(@"%@",index);
}
注释:这里切图时注意:只要一个星星,并且要求是 正方形 星星图片有留白。看代码就明白为什么要这么切图。1是美观 2是 容易计算。
以上所述是小编给大家介绍的iOS评分(评价)星星图打分功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!
沃梦达教程
本文标题为:iOS评分(评价)星星图打分功能
猜你喜欢
- Android实现监听音量的变化 2023-03-30
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android studio实现动态背景页面 2023-05-23
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android实现轮询的三种方式 2023-02-17
- iOS 对当前webView进行截屏的方法 2023-03-01
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- Flutter实现底部和顶部导航栏 2022-08-31
