iOS快速矩形

iOS快速矩形,ios,swift,uibezierpath,Ios,Swift,Uibezierpath,我正在尝试将一个矩形形状(如图所示的曲线)添加到现有的UIView中 这是我实现的代码: func drawRect(rect: CGRect) { let y:CGFloat = 20 let curveTo:CGFloat = 0 let myBezier = UIBezierPath() myBezier.move(to: CGPoint(x: 0, y: y)) myBezier.addQuadCurve(to: CGPoint(x: rec

我正在尝试将一个矩形形状(如图所示的曲线)添加到现有的UIView中

这是我实现的代码:

func drawRect(rect: CGRect) {

    let y:CGFloat = 20
    let curveTo:CGFloat = 0

    let myBezier = UIBezierPath()
    myBezier.move(to: CGPoint(x: 0, y: y))
    myBezier.addQuadCurve(to: CGPoint(x: rect.width, y: y), controlPoint: CGPoint(x: rect.width / 2, y: curveTo))
    myBezier.addLine(to: CGPoint(x: rect.width, y: rect.height))
    myBezier.addLine(to: CGPoint(x: 0, y: rect.height))
    myBezier.close()
    let context = UIGraphicsGetCurrentContext()
    context!.setLineWidth(4.0)
    UIColor.yellow.setFill()
    myBezier.fill()
}
不过,当我在模拟器中打开矩形路径时,却什么也没有,只是想尝试显示一个矩形路径。我相信这是因为我的其他元素被叠加在上面,对吗?我的全部代码是

谢谢你的帮助


查看您的代码。UIViewController没有覆盖的消息drawRect。您应该创建从UIView派生的自定义类,并覆盖其中的消息drawRect。

这可以变得更加灵活,但它可能适合您的需要。如果没有,这可能是一个很好的起点,让你得到你想要的

如果您没有研究过自定义组件/UIView子类化/使用IBInspectable和IBDesignable,这可能没有多大意义,因此您可能需要阅读:)

示例应用程序:

编辑:示例现在包括用于演示通过代码更改图像的按钮


Edit2:示例应用程序现在有一个备用故事板,显示如何在不使用自定义子类的情况下“路径剪裁”图像。

在视图添加到viewController的视图后,您可能需要调用
setNeedsDisplay()
为什么决定将curveTo设置为0?你的rect.height值是多少?您使用的视图层次结构将有助于查看完整的代码。。。1) 您的drawRect()函数位于viewDidLoad()函数中—绝对不是您想要的位置。2) drawRect()是UIView的一种方法-您正试图将其放入UIViewControllerHi,我检查了您的Github项目,但我不确定您是如何选择要舍入的图像的。我已经实现了您提供的相同文件,但我不确定如何选择此效果的图像视图。如果查看Interface Builder中的属性,您应该会看到图像属性和“roundingValue”属性。您可以像添加普通UIImageView一样向其添加图像-在IB中,或者通过在代码中设置.image属性。在attributes inspector或size inspector中?我对示例项目做了一些更改,因此可能会更清晰一些(它现在有3个图像可供选择-或者您可以添加自己的图像)。您可以在属性检查器中或通过代码设置图像。我添加了显示我的项目与您的项目的图像。您在ImageView上的自定义类的选项似乎比我多?
//
//  RoundedBottomImageView.swift
//  SW3IBDesign
//
//  Created by Don Mag on 2/27/17.
//  Copyright © 2017 DonMag. All rights reserved.
//

import UIKit

@IBDesignable

class RoundedBottomImageView: UIView {

    var imageView: UIImageView!

    @IBInspectable var image: UIImage? {
        didSet { self.imageView.image = image }
    }

    @IBInspectable var roundingValue: CGFloat = 0.0 {
        didSet {
            self.setNeedsLayout()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        doMyInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        doMyInit()
    }

    func doMyInit() {

        imageView = UIImageView()
        imageView.backgroundColor = UIColor.red
        imageView.contentMode = UIViewContentMode.scaleToFill
        addSubview(imageView)

    }

    override func layoutSubviews() {
        super.layoutSubviews()
        imageView.frame = self.bounds

        let rect = self.bounds
        let y:CGFloat = rect.size.height - roundingValue
        let curveTo:CGFloat = rect.size.height

        let myBezier = UIBezierPath()
        myBezier.move(to: CGPoint(x: 0, y: y))
        myBezier.addQuadCurve(to: CGPoint(x: rect.width, y: y), controlPoint: CGPoint(x: rect.width / 2, y: curveTo))
        myBezier.addLine(to: CGPoint(x: rect.width, y: 0))
        myBezier.addLine(to: CGPoint(x: 0, y: 0))
        myBezier.close()

        let maskForPath = CAShapeLayer()
        maskForPath.path = myBezier.cgPath
        layer.mask = maskForPath

    }

}