iOS星级选择,可滑动的,可点击
来源:互联网 发布:外加剂掺量怎么算法 编辑:程序博客网 时间:2024/05/28 15:19
iOS 星级选择,可以设置星星的个数,星级间距,可以设置分数。
一:.h文件定义
@interface FSStarView : UIView/** * 设置控件分数 * score 分数 */@property(nonatomic,assign) CGFloat score;/** * Init FSStarView * * @param frame Rectangles * @param numberOfStar 星星个数 * @param space 星星间距 * * @return FSStarViewObject */- (instancetype)initWithFrame:(CGRect)frame NumberOfStar:(NSInteger)numberOfStar Space:(CGFloat)space;/** * 选择的block回调 */@property(nonatomic,copy) void(^CommentsStartViewBlock) (NSInteger);@end
二:.m文件定义
#import "FSStarView.h"#define kBACKGROUND_STAR @"xingxing-kong"#define kFOREGROUND_STAR @"xingxing"@interface FSStarView ()@property(nonatomic,assign) CGFloat space;@property (nonatomic, assign) NSInteger numberOfStar;@property (nonatomic, strong) UIView *starBackgroundView;@property (nonatomic, strong) UIView *starForegroundView;@end@implementation FSStarView- (instancetype)initWithFrame:(CGRect)frame NumberOfStar:(NSInteger)numberOfStar Space:(CGFloat)space{ self = [super initWithFrame:frame]; if (self) { _numberOfStar = numberOfStar; _space = space; self.starBackgroundView = [self buidlStarViewWithImageName:kBACKGROUND_STAR]; self.starForegroundView = [self buidlStarViewWithImageName:kFOREGROUND_STAR]; [self addSubview:self.starBackgroundView]; [self addSubview:self.starForegroundView]; UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)]; [self addGestureRecognizer:tapGR]; //移动手势 UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)]; [self addGestureRecognizer:panGR]; } return self;}#pragma mark - Buidl Star View/** * 通过图片构建星星视图 * * @param imageName 图片名称 * * @return 星星视图 */- (UIView *)buidlStarViewWithImageName:(NSString *)imageName{ CGRect frame = self.bounds; UIView *view = [[UIView alloc] initWithFrame:frame]; view.clipsToBounds = YES; for (int i = 0; i < self.numberOfStar; i ++){ UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]]; imageView.userInteractionEnabled = YES; imageView.frame = CGRectMake(i * (frame.size.height + _space), 0, frame.size.height, frame.size.height); [view addSubview:imageView]; } return view;}#pragma mark - Set Score/** * 设置控件分数 * * @param score 分数 */- (void)setScore:(CGFloat)score{ CGFloat pointX = score * self.frame.size.height + ((NSInteger)score)*_space; self.starForegroundView.frame = CGRectMake(0, 0, pointX, self.frame.size.height);}- (void)tapGR:(UITapGestureRecognizer *)tapGR{//点击手势对应的消息,如果有参数,参数就是手势本身 CGPoint point = [tapGR locationInView:tapGR.view]; NSLog(@"%f %f",point.x,point.y); if (point.x > ((self.frame.size.height + _space)*_numberOfStar)) { point.x = (self.frame.size.height + _space)*_numberOfStar; }else{ point.x = point.x; } NSInteger numInt = (point.x+_space)/(self.frame.size.height + _space); CGFloat numFloat = (point.x+_space)/(self.frame.size.height + _space); if (numFloat > numInt) { numInt++; }else{ numInt = numInt; } self.starForegroundView.width = numInt*(self.frame.size.height + _space); self.CommentsStartViewBlock(numInt);}- (void)panGR:(UIPanGestureRecognizer *)panGR{//点击手势对应的消息,如果有参数,参数就是手势本身 CGPoint point = [panGR locationInView:panGR.view]; NSLog(@"%f %f",point.x,point.y); if ((point.x>=0)&&(point.x<=(5*(self.frame.size.height + _space)))) { NSInteger num = (point.x+_space)/(self.frame.size.height + _space); self.starForegroundView.width = num*(self.frame.size.height + _space); self.CommentsStartViewBlock(num); }}@end
三:控件的使用
FSStarView *starView = [[FSStarView alloc] initWithFrame:FRAME(0, 0, 100, 15) NumberOfStar:5 Space:5]; starView.score = 3.0;//分值 [self.view addSubview:view]; starView.CommentsStartViewBlock = ^(NSInteger num){ NSLog(@"=======%ld", num); };
0 0
- iOS星级选择,可滑动的,可点击
- 自定义可滑动、可点击的开关
- 自定义可点击和滑动的按钮
- 可滑动可点击,不占全屏的自定义控件
- 自己绘制的可点击,可滑动,可动态添加的柱状图
- ios一个可滑动列表式的弹出选择框(封装过的)
- 横向滑动导航条可点击滑动
- iOS扩大按钮的可点击范围
- android 可滑动、点击切换的导航栏
- iOS 悬浮可拖动可点击按钮
- 可滑动的viewpagerIndicator
- Android实现可左右滑动的选择控件
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- 真正的画廊,左右条目可点击居中, 左右条目可滑动
- requestAnimationFrame
- Reverse Words in a String III
- ajax遍历json.put(String,String)
- 盈鹏飞嵌入式---OpenWRT在iMX6UL上面的移植与编译
- MarkDwon的使用以及基本语法
- iOS星级选择,可滑动的,可点击
- Oralce创建表用户和导入导出dmp文件
- hdu 1312 Red and Black
- MySQL数据库(5)
- require.js的用法
- 【react学习】关于react框架使用的一些细节要点的思考
- 欢迎使用CSDN-markdown编辑器
- HDU
- 250 ubuntu16.04/oh my zsh 开箱