Swift 使用绘制的形状并更改其特征

Swift 使用绘制的形状并更改其特征,swift,swift3,uibezierpath,Swift,Swift3,Uibezierpath,在花了一整天的时间尝试如何做之后,我终于想出了一种非矩形按钮的方法 底部的三个按钮都在一个按钮上。hitTest然后计算出单击在哪个UIBezierPath中,然后调用一个函数 import UIKit class ViewController: UIViewController { let path = UIBezierPath() let path2 = UIBezierPath() let path3 = UIBezierPath() let path

在花了一整天的时间尝试如何做之后,我终于想出了一种非矩形按钮的方法

底部的三个按钮都在一个按钮上。hitTest然后计算出单击在哪个UIBezierPath中,然后调用一个函数

import UIKit

class ViewController: UIViewController {

    let path = UIBezierPath()
    let path2 = UIBezierPath()
    let path3 = UIBezierPath()
    let path4 = UIBezierPath()

    @IBAction func clicked(_ sender: Any) {
        print("Clicked")
    }

    @IBOutlet weak var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        drawShape()
        let gesture = UITapGestureRecognizer(target: self, action:  #selector (self.checkAction(sender:)))
        self.button.addGestureRecognizer(gesture)
    }

    func drawShape(){
        let buttonWidth = button.frame.width
        let buttonHeight = buttonWidth * 1.23
        path.move(to: CGPoint(x: 0, y: buttonHeight*0.85))
        path.addLine(to: CGPoint(x: 0, y: buttonHeight))
        path.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight))
        path.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.68))
        path.close()
        path2.move(to: CGPoint(x: 0, y: buttonHeight*0.68))
        path2.addLine(to: CGPoint(x: 0, y: buttonHeight*0.85))
        path2.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.68))
        path2.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.35))
        path2.close()
        path3.move(to: CGPoint(x: 0, y: buttonHeight*0.50))
        path3.addLine(to: CGPoint(x: 0, y: buttonHeight*0.68))
        path3.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.35))
        path3.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.06))
        path3.close()
        path4.move(to: CGPoint(x: 0, y: buttonHeight*0.45))
        path4.addLine(to: CGPoint(x: 0, y: buttonHeight*0.50))
        path4.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.06))
        path4.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.01))
        path4.close()

    }

    func checkAction(sender : UITapGestureRecognizer) {
        let location = sender.location(in: button)
        print(location)
        hitTest(tapLocation: location)
    }

    public func hitTest(tapLocation:CGPoint){
        if path.contains(tapLocation){
            print("Button3")
        }
        if path2.contains(tapLocation){
            print("Button2")
        }
        if path3.contains(tapLocation){
            print("Button1")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
然后在链接到按钮的文件中:

import UIKit
@IBDesignable

class PushButtonView: UIButton {
    override func draw(_ rect: CGRect) {
        let buttonWidth = self.frame.width
        let buttonHeight = buttonWidth * 1.23

        let color1 = hexStringToUIColor(hex: "#e0dfd5")
        let color2 = hexStringToUIColor(hex: "#ef6461")
        let color3 = hexStringToUIColor(hex: "#e4b363")
        let color4 = hexStringToUIColor(hex: "#313638")

        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: buttonHeight*0.85))
        path.addLine(to: CGPoint(x: 0, y: buttonHeight))
        path.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight))
        path.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.68))
        path.close()
        color4.setFill()
        path.fill()
        let path2 = UIBezierPath()
        path2.move(to: CGPoint(x: 0, y: buttonHeight*0.68))
        path2.addLine(to: CGPoint(x: 0, y: buttonHeight*0.85))
        path2.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.68))
        path2.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.35))
        path2.close()
        color3.setFill()
        path2.fill()
        let path3 = UIBezierPath()
        path3.move(to: CGPoint(x: 0, y: buttonHeight*0.50))
        path3.addLine(to: CGPoint(x: 0, y: buttonHeight*0.68))
        path3.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.35))
        path3.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.06))
        path3.close()
        color2.setFill()
        path3.fill()
        let path4 = UIBezierPath()
        path4.move(to: CGPoint(x: 0, y: buttonHeight*0.45))
        path4.addLine(to: CGPoint(x: 0, y: buttonHeight*0.50))
        path4.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.06))
        path4.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight*0.01))
        path4.close()
        color1.setFill()
        path4.fill()
    }

    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }

        if ((cString.characters.count) != 6) {
            return UIColor.gray
        }

        var rgbValue:UInt32 = 0
        Scanner(string: cString).scanHexInt32(&rgbValue)

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }


}
一切正常!这可能不是最好的方法,但在尝试了5个小时不同的事情后,这才是最有效的方法。但是,我现在遇到的问题是,我不知道如何操作我在外部文件中绘制的那些形状。我确信这个问题以前有人问过,也有人回答过,但我真的不知道该找什么

例如,如果它是javascript,我会假设在对象“button”中绘制的“path”将由self.button.path(或类似的东西)访问,然而,我的理解是Swift3不是这样工作的。我说得对吗?因此,如果我想用动画更改NOTES按钮/形状的背景色,我将如何获得这种效果:

UIView.animate(withDuration: 1, animations: {
    self.button.path.color = UIColor.red
}, completion: { finished in
    if(finished){
        //callFunction()
    }
})

这是可能的,还是一旦绘制了路径,它就不是一个可更改的对象,必须删除并重新绘制?

您可以向
PushButtonView
类添加属性,以允许视图控制器修改路径颜色。太棒了-这是可能的。我将开始谷歌搜索……你最好重构你的
PushButtonView
以使用
CAShapeLayer
s。这将使您能够灵活地设置UI动画,而无需依赖于
drawRect
。您可以向
PushButtonView
类添加属性,以允许视图控制器修改路径颜色。太棒了,这是可能的。我将开始谷歌搜索……你最好重构你的
PushButtonView
以使用
CAShapeLayer
s。这将使您能够灵活地设置UI动画,而无需依赖
drawRect