Swift 3:绘制一个矩形 [英] Swift 3: Drawing a rectangle

查看:218
本文介绍了Swift 3:绘制一个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经3天了,很快,我试图弄清楚如何绘制一个矩形。我对于语言太陌生,不知道要扩展的类以及要覆盖的方法,并且我已经查找了示例代码,但似乎没有任何工作(这归功于我使用swift 3)。 / p>

现在我正在尝试的是:

  import UIKit 

class ViewController:UIViewController {

覆盖func viewDidLoad(){
super.viewDidLoad()


let k = Draw (frame:CGRect(
origin:CGPoint(x:50,y:50),
size:CGSize(width:100,height:100)))

k。 draw(CGRect(
origin:CGPoint(x:50,y:50),
size:CGSize(width:100,height:100)));
}

重写func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何可以重新创建的资源。



$ b class Draw:UIView {

覆盖init(frame:CGRect){
super.init (frame:frame)
}

需要init?(coder aDecoder:NSCoder){
fatalError(init(coder :)还没有实现)
}

覆盖func draw(_ rect:CGRect){
let h = rect.height
let w = rect.width
var color:UIColor = UIColor。 yellow()

var drect = CGRect(x:(w * 0.25),y:(h * 0.25),width:(w * 0.5),height:(h * 0.5))
var bpath:UIBezierPath = UIBezierPath(rect:drect)

color.set()
bpath.stroke()

print(it ran)

NSLog(drawRect更新了视图)

}

}

这并没有做任何事情。帮助。

解决方案

为了查看视图,您需要创建一个并为其指定框架,以便知道它有多大使用它。



如果您将代码放在Playground中,然后添加以下行:

  let d = Draw(frame:CGRect(x:0,y:0,width:100,height:100))

您可以点击右侧的快速浏览,然后您会看到该视图。








您也可以添加在 ViewController 中查看 view 的子视图,然后在iPhone上看到它:

  override func viewDidLoad(){
super.viewDidLoad()

let k = Draw(frame :CGRect(
原点:CGPoint(x:50,y:50),
尺寸:CG大小(宽度:100,高度:100)))

//将视图添加到视图层次结构中,以便它显示在屏幕上
self.view.addSubview(k)

$ / code>

请注意,您从不会调用 draw(_:) code>直接。它由 Cocoa Touch 呼叫您显示视图。




I'm 3 days new to swift, and I'm trying to figure out how to draw a rectangle. I'm too new to the language to know the classes to extend and the methods to override, and I've looked around for sample code, but nothing seems to work (which I'm attributing to my use of swift 3).

What I'm trying now is:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        let k = Draw(frame: CGRect(
            origin: CGPoint(x: 50, y: 50),
            size: CGSize(width: 100, height: 100)))

        k.draw(CGRect(
            origin: CGPoint(x: 50, y: 50),
            size: CGSize(width: 100, height: 100)));
    }

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


}
class Draw: UIView {

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

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        let h = rect.height
        let w = rect.width
        var color:UIColor = UIColor.yellow()

        var drect = CGRect(x: (w * 0.25),y: (h * 0.25),width: (w * 0.5),height: (h * 0.5))
        var bpath:UIBezierPath = UIBezierPath(rect: drect)

        color.set()
        bpath.stroke()

        print("it ran")

        NSLog("drawRect has updated the view")

    }

}

And that's not doing anything. Help.

解决方案

In order to see the view, you need to create one and give it frame so that it knows how big to make it.

If you put your code in a Playground, and then add this line:

let d = Draw(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

You'll be able to click on the Quick View on the right, and then you'll see the view.


You can also add the view as a subview of view in your ViewController and then you'll see it on the iPhone:

override func viewDidLoad() {
    super.viewDidLoad()

    let k = Draw(frame: CGRect(
        origin: CGPoint(x: 50, y: 50),
        size: CGSize(width: 100, height: 100)))

    // Add the view to the view hierarchy so that it shows up on screen
    self.view.addSubview(k)
}

Note that you never call draw(_:) directly. It is called for you by Cocoa Touch to display the view.

这篇关于Swift 3:绘制一个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆