解决手势冲突问题:
通过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)
}
}