swift 封装的一个五星评分器,可以用于单纯展示评分,也可以用来手动打分。
gitHub地址:https://github.com/NinoWang/RatingBar
import UIKitprotocol ratingbarDelegate { func ratingChanged(ratingbar:ratingbar,newrating:float)}class ratingbar: UIVIEw { var delegate:ratingbarDelegate? var starrating:float? var lastrating:float? var starWIDth:CGfloat? var starHeight:CGfloat? var unSelectedImage:UIImage? var halfSelectedImage:UIImage? var fullSelectedImage:UIImage? var s1:UIImageVIEw? var s2:UIImageVIEw? var s3:UIImageVIEw? var s4:UIImageVIEw? var s5:UIImageVIEw? //是否是指示器 var isIndicator:Bool = false func setseletedState(deselectedname:String?,halfSelectedname:String?,fullSelectedname:String?,starSIDeLength:CGfloat,delegate:ratingbarDelegate){ self.delegate = delegate unSelectedImage = UIImage(named: deselectedname!) fullSelectedImage = UIImage(named: fullSelectedname!) halfSelectedImage = halfSelectedname == nil ? fullSelectedImage:UIImage(named: halfSelectedname!) starWIDth = 0 starHeight = 0 if (starHeight < starSIDeLength) { starHeight = starSIDeLength } if (starWIDth < starSIDeLength) { starWIDth = starSIDeLength } //控件宽度适配 var frame = self.frame var vIEwWIDth:CGfloat = starWIDth! * 5 if (frame.size.wIDth) > vIEwWIDth { vIEwWIDth = frame.size.wIDth } frame.size.wIDth = vIEwWIDth self.frame = frame starrating = 0 lastrating = 0 s1 = UIImageVIEw(image: unSelectedImage) s2 = UIImageVIEw(image: unSelectedImage) s3 = UIImageVIEw(image: unSelectedImage) s4 = UIImageVIEw(image: unSelectedImage) s5 = UIImageVIEw(image: unSelectedImage) //星星间距 let space:CGfloat = (vIEwWIDth - starWIDth!*5)/6 var starX = space let starY = (frame.height - starHeight!)/2 s1?.frame = CGRectMake(starX,starY,starWIDth!,starHeight!) starX = starX + starWIDth! + space s2?.frame = CGRectMake(starX,starHeight!) starX = starX + starWIDth! + space s3?.frame = CGRectMake(starX,starHeight!) starX = starX + starWIDth! + space s4?.frame = CGRectMake(starX,starHeight!) starX = starX + starWIDth! + space s5?.frame = CGRectMake(starX,starHeight!) starX = starX + starWIDth! + space s1?.userInteractionEnabled = false s2?.userInteractionEnabled = false s3?.userInteractionEnabled = false s4?.userInteractionEnabled = false s5?.userInteractionEnabled = false self.addSubvIEw(s1!) self.addSubvIEw(s2!) self.addSubvIEw(s3!) self.addSubvIEw(s4!) self.addSubvIEw(s5!) } //设置评分值 func displayrating(rating:float){ s1?.image = unSelectedImage s2?.image = unSelectedImage s3?.image = unSelectedImage s4?.image = unSelectedImage s5?.image = unSelectedImage if (rating >= 1) { s1?.image = halfSelectedImage } if (rating >= 2) { s1?.image = fullSelectedImage } if (rating >= 3) { s2?.image = halfSelectedImage } if (rating >= 4) { s2?.image = fullSelectedImage } if (rating >= 5) { s3?.image = halfSelectedImage } if (rating >= 6) { s3?.image = fullSelectedImage } if (rating >= 7) { s4?.image = halfSelectedImage } if (rating >= 8) { s4?.image = fullSelectedImage } if (rating >= 9) { s5?.image = halfSelectedImage } if (rating >= 10) { s5?.image = fullSelectedImage } starrating = rating lastrating = rating delegate?.ratingChanged(self,newrating: rating) } func rating() -> float{ return starrating! } //手势 overrIDe func touchesBegan(touches: Set<UItouch>,withEvent event: UIEvent?) { super.touchesBegan(touches,withEvent: event) } overrIDe func touchesEnded(touches: Set<UItouch>,withEvent event: UIEvent?) { super.touchesEnded(touches,withEvent: event) self.touchesrating(touches) } overrIDe func touchesMoved(touches: Set<UItouch>,withEvent event: UIEvent?) { super.touchesMoved(touches,withEvent: event) self.touchesrating(touches) } //触发 func touchesrating(touches:NSSet){ if(self.isIndicator == false){ return } let point:CGPoint = touches.anyObject()!.locationInVIEw(self) let space:CGfloat = (self.frame.size.wIDth - starWIDth!*5)/6 var newrating:float = 0 if (point.x >= 0 && point.x <= self.frame.size.wIDth) { if (point.x <= space+starWIDth!*0.5) { newrating = 1; }else if (point.x < space*2+starWIDth!){ newrating = 2; }else if (point.x < space*2+starWIDth!*1.5){ newrating = 3; }else if (point.x <= 3*space+2*starWIDth!){ newrating = 4; }else if (point.x <= 3*space+2.5*starWIDth!){ newrating = 5; }else if (point.x <= 4*space+3*starWIDth!){ newrating = 6; }else if (point.x <= 4*space+3.5*starWIDth!){ newrating = 7; }else if (point.x <= 5*space+4*starWIDth!){ newrating = 8; }else if (point.x <= 5*space+4.5*starWIDth!){ newrating = 9; }else { newrating = 10; } } if (newrating != lastrating){ self.displayrating(newrating) } }}总结
以上是内存溢出为你收集整理的swift 简单封装的一个五星评分器全部内容,希望文章能够帮你解决swift 简单封装的一个五星评分器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)