IOS 星星评分控件-Swift3.0

本文介绍了如何在Swift3.0中创建一个星星评分控件,包括手势处理、图片准备和代码实现,强调了解决手势冲突问题的方法,并提供了完整的控件代码。
摘要由CSDN通过智能技术生成

解决手势冲突问题:
通过block

block

//starView.swift//
        weak var parentVc : ViewController! = nil//1.不增加引用计数
        parentVc.isScrollBlock(false)//2.
        parentVc.isScrollBlock(true)//3.
//viewcontroller//
var isScrollBlock:(Bool)->() = { bool in

    }
self.isScrollBlock = { bool in

            self.scrollView.isScrollEnabled = bool

        }

效果:

这里写图片描述

特点:点击、滑动、任意切
性能:忽略不计,不是最优模式、实现简单


思路:
1. 首先想到的是重写draw方法、实现比较耗时、而且性能不一定最优
2. 放置五个button、但是没有办法随意控制,比如:半星。而且要添加5个button、不断重复更改图片、对系统性能消耗可能并不是很好。
3. 放置两张Imagview,一个底层、一个顶层,通过设置imageview的填充方式,以及甚至frame来实现,并添加touch方法以及tap手势。(本文)

实现代码:

准备两张图片

这里写图片描述

这里写图片描述

let img1 = UIImageView()
    let img2 = UIImageView()

    func addImg1AndImg2() {
        img1.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 185, height: 28))
        img2.frame = CGRect.init(x: 0, y: 0, width: 0, height: self.frame.size.height)
        img1.image = UIImage.init(named: "p未选中")
        img2.image = UIImage.init(named: "p选中")
        img1.contentMode = UIViewContentMode.left
        img2.contentMode = UIViewContentMode.left
        img1.layer.masksToBounds = true
        img2.layer.masksToBounds = true
        img2.backgroundColor = UIColor.clear
        self.addSubview(img1)
        self.addSubview(img2)
        self.layer.masksToBounds = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(changeStar(tap:)))
        self.addGestureRecognizer(tap)

    }

UIViewContentMode.left保证图片不会变形并且依据left绘制

UITapGestureRecognizer因为touch方法在有scrollview时会发生手势冲突,所以最好还是添加tap手势

下面是touch手势以及tap手势de实现方法

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let allTouches: Set<AnyHashable>? = event?.allTouches
        //返回与当前接收者有关的所有的触摸对象
        let touch: UITouch? = allTouches?.first as? UITouch
        //视图中的所有对象
        let point: CGPoint? = touch?.location(in: touch?.view)
        //返回触摸点在视图中的当前坐标
        let x: CGFloat? = point?.x
        img2.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: x!, height: self.frame.size.height))
        print(#function)

    }
    func changeStar(tap:UITapGestureRecognizer) {
        let point: CGPoint? = tap.location(in: self)
        let x: CGFloat? = point?.x
        img2.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: x!, height: self.frame.size.height))
        print(#function)

    }

使用:


var star: StarView = StarView.init(width: 180, height: 26)
star.addImg1AndImg2()

完活

整个星星控件代码:

//
//  StarView.swift
//  StarView
//
//  Created by Quinnx on 2017/7/25.
//  Copyright © 2017年 xoxo_x. All rights reserved.
//

import UIKit

class StarView: UIView {

    let img1 = UIImageView()
    let img2 = UIImageView()

    func addImg1AndImg2() {
        img1.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 185, height: 28))
        img2.frame = CGRect.init(x: 0, y: 0, width: 0, height: self.frame.size.height)
        img1.image = UIImage.init(named: "p未选中")
        img2.image = UIImage.init(named: "p选中")
        img1.contentMode = UIViewContentMode.left
        img2.contentMode = UIViewContentMode.left
        img1.layer.masksToBounds = true
        img2.layer.masksToBounds = true
        img2.backgroundColor = UIColor.clear
        self.addSubview(img1)
        self.addSubview(img2)
        self.layer.masksToBounds = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(changeStar(tap:)))
        self.addGestureRecognizer(tap)

    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let allTouches: Set<AnyHashable>? = event?.allTouches
        //返回与当前接收者有关的所有的触摸对象
        let touch: UITouch? = allTouches?.first as? UITouch
        //视图中的所有对象
        let point: CGPoint? = touch?.location(in: touch?.view)
        //返回触摸点在视图中的当前坐标
        let x: CGFloat? = point?.x
        img2.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: x!, height: self.frame.size.height))
        print(#function)

    }
    func changeStar(tap:UITapGestureRecognizer) {
        let point: CGPoint? = tap.location(in: self)
        let x: CGFloat? = point?.x
        img2.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: x!, height: self.frame.size.height))
        print(#function)

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值