如何在swift

时间:2017-07-04 09:33:20

标签: ios swift uiimage

有人可以建议我如何创建两个圆形边框uiimage,如图所示。

我可以使用任何可用的库吗?

enter image description here

3 个答案:

答案 0 :(得分:2)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var secondView: UIView!
    @IBOutlet weak var imgView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        imgView.layer.cornerRadius = imgView.frame.size.height/2
         secondView.layer.cornerRadius = secondView.frame.size.height/2
        imgView.layer.borderWidth = 5.0
        imgView.layer.borderColor = UIColor.red.cgColor
        imgView.clipsToBounds = true
         secondView.clipsToBounds = true
        // Do any additional setup after loading the view, typically from a nib.
    }

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


}

enter image description here

答案 1 :(得分:1)

你可以使用下面的func

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.viewPicture.layer.cornerRadius = self.viewPicture.frame.size.height/2
    self.ivPicture.layer.cornerRadius = self.ivPicture.frame.size.height/2

}

override func viewWillAppear(_ animated: Bool) {
    self.addviewBGBezier(self.viewBG)
    self.addviewPicBezier(self.viewPicture)
}

func addviewBGBezier(_ viewMy : UIView)  {
    let layer = CAShapeLayer()
    let path = UIBezierPath()

    path.move(to: CGPoint(x: 0, y: viewMy.frame.size.height))
    path.addLine(to: CGPoint(x: viewMy.frame.size.width, y: 140))
    path.addLine(to: CGPoint(x: viewMy.frame.size.width, y: viewMy.frame.size.height))
    path.close()
    layer.path = path.cgPath
    layer.fillColor = UIColor.white.cgColor

    viewMy.layer.addSublayer(layer)
}

func addviewPicBezier(_ viewMy : UIView)  {
    let layerLower = CAShapeLayer()

    let center = CGPoint(x: viewMy.frame.size.width/2, y: viewMy.frame.size.height/2)
    print("cal center : \(center)")

    let path = UIBezierPath(arcCenter: center,
                            radius: viewMy.frame.size.width/2,
                            startAngle: CGFloat(-Double.pi/10.35), endAngle: CGFloat(Double.pi), clockwise: true)

    layerLower.path = path.cgPath
    layerLower.fillColor = UIColor.clear.cgColor
    layerLower.strokeColor = UIColor.cyan.cgColor
    layerLower.lineWidth = 20
    viewMy.layer.addSublayer(layerLower)

    let layerUpper = CAShapeLayer()

    let pathUpper = UIBezierPath(arcCenter: center,
                            radius: viewMy.frame.size.width/2,
                            startAngle: CGFloat(-Double.pi/10.35), endAngle: CGFloat(Double.pi), clockwise: false)

    layerUpper.path = pathUpper.cgPath
    layerUpper.fillColor = UIColor.clear.cgColor
    layerUpper.strokeColor = UIColor.cyan.cgColor
    layerUpper.lineWidth = 20
    viewMy.layer.addSublayer(layerUpper)

}

输出

enter image description here

在下面的行之后

layerLower.strokeColor = UIColor.white.cgColor

输出是:

enter image description here

答案 2 :(得分:0)

你不需要添加任何库,只需添加UIView并在该UIImage中进行循环。

您可以在代码下方使用它。

IBBackgroundView.layer.cornerRadius = (IBBackgroundView.frame.width / 2)
        IBBackgroundView.layer.borderColor = UIColor.white.cgColor
        IBBackgroundView.layer.borderWidth = 5.0
        IBBackgroundView.clipsToBounds = true
        IBImageView.layer.cornerRadius = IBImageView.frame.width / 2
        IBImageView.layer.borderColor = UIColor.blue.cgColor
        IBImageView.layer.borderWidth = 3.0
        IBImageView.clipsToBounds = true

You will able to see image as shown in screen shot