【问题标题】:creating combined views inside a view在视图中创建组合视图
【发布时间】:2017-10-04 04:32:17
【问题描述】:

我正在尝试创建一个中间有一个孔的圆形 UIView,然后将该视图与另一个视图的顶部相结合。但是我不确定我是否以正确的方式进行操作

这是我正在尝试做的事情(如草图中的顶部圆圈所示):

到目前为止,这就是我在操场上的做法:

import Foundation
import UIKit
import CoreGraphics


extension UIColor {
    static func getRandomColor() -> UIColor {
        //Generate between 0 to 1
        let red:CGFloat = CGFloat(drand48())
        let green:CGFloat = CGFloat(drand48())
        let blue:CGFloat = CGFloat(drand48())

        return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
    }
}

func makeCircularView(x: Int = 0 , y : Int = 0 , width: Double, height : Int, color: UIColor) -> UIView {
    let view : UIView = UIView(frame:CGRect(x: x, y: y, width: Int(width), height: height))
    view.alpha = 0.5
    view.layer.cornerRadius = view.bounds.size.width / 2
    view.backgroundColor = color
    view.layer.masksToBounds = true
    return view
}



let uiView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
uiView.backgroundColor = .red

uiView.clipsToBounds = true
uiView.layer.shadowRadius
uiView.layer.cornerRadius = 5
uiView



let cir : UIView
let circle2 : UIView
cir = makeCircularView(width : 30 , height : 30, color: UIColor.gray)
circle2 = makeCircularView( x:  Int(7.5) , y : Int(7.5), width: 15, height: 15, color: UIColor.clear)

cir.addSubview(circle2)
uiView.addSubview(cir)


cir.topAnchor.constraint(equalTo: uiView.topAnchor, constant: -20).isActive = true
cir.leadingAnchor.constraint(equalTo: uiView.leadingAnchor, constant: -10).isActive = true

【问题讨论】:

  • 你找到解决方案了吗?
  • 还没有,我今晚试试,然后我会更新你...
  • 我刚刚做了这件事
  • @HimanshuMoradiya 你能贴出你的代码吗?
  • 我刚刚制作了一个 .xib 文件并创建了包含您的 2 视图的文件文件

标签: ios swift uiview uibezierpath


【解决方案1】:

使用这个函数并在其中传递角视图。

func createView(_ myview : UIView)  {
        myview.backgroundColor = UIColor.clear
        myview.layer.cornerRadius = myview.frame.width/2 // assuming myview is squre


        myview.layer.borderColor = UIColor.white.cgColor
        myview.layer.borderWidth = 4
    }

输出

编辑

您可以执行以下任何操作,以获得集成图像

  1. 使用PaintCode,它会自动为你做层编码。 (您可以使用试用版 5 天)。
  2. 请您的设计师获取特定的集成图像,在UIImageview 中使用此图像

【讨论】:

  • 如果一个视图稍微在另一个视图之外,是否有任何方法可以合并视图?像上面的设计
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多