想练练手,所以封装了一个两种选择星星的评价样式的Demo,并且可以获取到点击的星星的索引值,方便记录值,上传数据时使用

首先创建View类,设计初始化方法,并且用到了枚举类型和代理方法

方式一:默认的有文字的5颗星评价的样式

case .TextType://有文字样式
            
            //计算每个星星的大小宽、高
            let itemWidthF:CGFloat = viewWidthF / 5.0
            let itemHeightF:CGFloat = viewHeightF / 2.0
            //将小的值赋值给星星
            let starWidth:CGFloat = 30.0;
            let textArr:NSArray = ["很差","凑合","一般","不错","完美"]
            
            //创建按钮和星星
            for i in 0...4 {
                let starImgV = UIImageView.init(frame: CGRect.init(x: (itemWidthF-starWidth)/2+itemWidthF*CGFloat(i), y: (itemHeightF-starWidth)/2, width: starWidth, height: starWidth))
                starImgV.tag = i+100
                starImgV.image = UIImage.init(named: "StarUnSelect")
                self.addSubview(starImgV)
                
                let butt = UIButton.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: 0, width: itemWidthF, height: itemHeightF));
                butt.tag = i+200
                butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                self.addSubview(butt)
                
                let textLabel = UILabel.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: itemHeightF, width: itemWidthF, height: itemHeightF))
                textLabel.text = textArr[i] as? String
                textLabel.adjustsFontSizeToFitWidth = true
                textLabel.textColor = UIColor.lightGray
                textLabel.font = UIFont.systemFont(ofSize: 13)
                textLabel.textAlignment = NSTextAlignment.center;
                self.addSubview(textLabel)
            }
            
            break

方式二:没有文字描述的评价程度,同时要注意视图的宽高大小问题

case .NoTextType:
            //无文字样式
            //计算每个星星的大小宽、高
            let itemWidthF:CGFloat = viewWidthF / CGFloat(aNum)
            let itemHeightF:CGFloat = viewHeightF
            //设置星星的大小
            var starWidth:CGFloat = 0.0
            if itemWidthF>30 {
                //如果大于,保持大小为30
                starWidth = 30
                //创建按钮和星星
                for i in 0...aNum-1 {
                    let starImgV = UIImageView.init(frame: CGRect.init(x: (itemWidthF-starWidth)/2+itemWidthF*CGFloat(i), y: (itemHeightF-starWidth)/2, width: starWidth, height: starWidth))
                    starImgV.tag = i+100
                    starImgV.image = UIImage.init(named: "StarUnSelect")
                    self.addSubview(starImgV)
                    
                    let butt = UIButton.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: 0, width: itemWidthF, height: itemHeightF));
                    butt.tag = i+200
                    butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                    self.addSubview(butt)
                    
                }
            }else{
                //如果小于,判断宽、高的大小,将小的值赋值给星星
                //三目运算
                //starWidth = itemWidthF<itemHeightF ? itemWidthF:itemHeightF
                
                if itemWidthF<=itemHeightF {
                    //如果宽小于高
                    starWidth = itemWidthF
                    //创建按钮和星星
                    for i in 0...aNum-1 {
                        let starImgV = UIImageView.init(frame: CGRect.init(x: starWidth*CGFloat(i), y: (itemHeightF-starWidth)/2, width: starWidth, height: starWidth))
                        starImgV.tag = i+100
                        starImgV.image = UIImage.init(named: "StarUnSelect")
                        self.addSubview(starImgV)
                        
                        let butt = UIButton.init(frame: CGRect.init(x: starWidth*CGFloat(i), y: 0, width: starWidth, height: itemHeightF));
                        butt.tag = i+200
                        butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                        self.addSubview(butt)
                    }
                }else{
                    //如果宽大于高
                    starWidth = itemHeightF
                    //创建按钮和星星
                    for i in 0...aNum-1 {
                        let starImgV = UIImageView.init(frame: CGRect.init(x: (itemWidthF-starWidth)/2+itemWidthF*CGFloat(i), y: 0, width: starWidth, height: starWidth))
                        starImgV.tag = i+100
                        starImgV.image = UIImage.init(named: "StarUnSelect")
                        self.addSubview(starImgV)
                        
                        let butt = UIButton.init(frame: CGRect.init(x: itemWidthF*CGFloat(i), y: 0, width: itemWidthF, height: itemHeightF));
                        butt.tag = i+200
                        butt.addTarget(self, action: #selector(buttClick), for: UIControlEvents.touchUpInside)
                        self.addSubview(butt)
                    }
                }
            }
            
            break

设计代理方法,并实现

//定义协议,知道点击的哪一个
protocol BHClickMarkStarDelegate:NSObjectProtocol{
    
    //定义一个可以记录显示的哪颗星星的方法
    func clickWhichStar(bhMarkView:BHMarkStarView,indexNum:Int)
    
}

//实现协议方法
    func clickWhichStar(bhMarkView: BHMarkStarView, indexNum: Int) {
        switch bhMarkView.tag {
        case 1:
            print("输出的是第。。。一。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 2:
            print("输出的是第。。。二。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 3:
            print("输出的是第。。。三。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 10:
            print("输出的是第。。。四。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 11:
            print("输出的是第。。。五。。。个视图的第...\(indexNum+1)...个星星")
            break
        case 12:
            print("输出的是第。。。六。。。个视图的第...\(indexNum+1)...个星星")
            break
        default:
            break
        }
    }

效果图:(源码下载:https://github.com/hbblzjy/SwiftMarkStartDemo

    

Swift基础之两种选择星星的评价样式并获取星星的索引值的更多相关文章

  1. 【Swfit】Swift与OC两种语法写单例的区别

    Swift与OC两种语法写单例的区别 例如写一个NetworkTools的单例 (1)OC写单例 + (instancetype)sharedNetworkTools { static id inst ...

  2. 运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。

    比较:>,<,=,>=,<=,<>(!=) 逻辑:AND,OR,NOT 范围:BETWEEN...AND... 范围:IN,NOT IN 判空:IS NULL, I ...

  3. JAVA基础之两种核心机制

    突然之间需要学习Java,学校里学的东西早就忘记了,得用最短的时间把Java知识理顺,重点还是J2EE,毕竟所有的ava项目中95%都是J2EE,还是先从基础的J2SE学起吧....... 首先是了解 ...

  4. Thead基础及两种创建方式

    今天本人给大家讲解一下多线程,如有不对的或者讲的不好的可以多多提出,我会进行相应的更改,先提前感谢提出意见的各位了!!! 说说多线程本人自己对它理解的定义:因计算机的CPU支持能够在同一时间执行多于一 ...

  5. iOS 获取版本号(Swift和OC两种)

    iOS获取应用版本号:version OC: [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVers ...

  6. Swift基础之两指拉动图片变大变小

    我们在使用APP的时候,有时会发现有些图片可以通过两指进行放大.缩小,今天就实现这样的一种效果,比较简单,不喜勿喷.... var imageVi:UIImageView! = nil    var ...

  7. javascript基础之两种函数的定义方法

    第一种方式:可以在函数定义之前调用也可以在函数定义之后调用: (0)函数的调用 add(,) //可以调用 (1)函数的定义: function add(x,y) { console.log(x+y) ...

  8. git代码回滚的两种选择

    回滚到指定commit,且保持该commit之前和之后的提交记录 使用git revert命令. git revert HEAD // 回滚到前一次 commit git revert HEAD^ / ...

  9. [Swift]二分法的两种方式

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

随机推荐

  1. MSIL实用指南-闭包的生成和调用

    闭包(Closure)是词法闭包(Lexical Closure)的简称.对闭包的具体定义有很多种说法,这些说法大体可以分为两类: 一种说法认为闭包是符合一定条件的函数,比如参考资源中这样定义闭包:闭 ...

  2. kafka知识体系-kafka设计和原理分析

    kafka设计和原理分析 kafka在1.0版本以前,官方主要定义为分布式多分区多副本的消息队列,而1.0后定义为分布式流处理平台,就是说处理传递消息外,kafka还能进行流式计算,类似Strom和S ...

  3. geotrellis使用(三十九)COG 写入更新

    前言 前面介绍过了如何在 ETL 的时候更新 Layer,使得能够在大数据量的时候完成 ETL 操作,同时前两篇文章也介绍了 COG 以及如何在 Geotrellis 中实现 COG 的读取.本文介绍 ...

  4. [LeetCode] Shopping Offers 购物优惠

    In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...

  5. [LeetCode] The Maze II 迷宫之二

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  6. Python 字符串常见的27个操作

    有字符串 mystr = "hello world itcast and itcastcpp",以下是常见的操作: 1. mystr.find(str, start=0, end= ...

  7. [LNOI 2014]LCA

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. 设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先. ...

  8. ●洛谷P3242 [HNOI2015]接水果

    题链: https://www.luogu.org/problemnew/show/P3242 题解: 整体二分,扫描线+树状数组. 详细的题解:http://blog.csdn.net/thy_as ...

  9. ●POJ 3608 Bridge Across Islands

    题链: http://poj.org/problem?id=3608 题解: 计算几何,求两个凸包间的最小距离,旋转卡壳 两个凸包间的距离,无非下面三种情况: 所以可以基于旋转卡壳的思想,去求最小距离 ...

  10. Linux上安装Libssh2

    由于项目需要使用libssh2,在安装时,遇到一些问题,发现网上的都是互相抄,把自己遇到的问题,记下来,希望可以帮助到别人,自己下次使用时候,也方便查找,节约时间. 安装的流程: 1.下载源码,wge ...