Swift 3:绘制一个矩形

Jea*_*ean 11 graphics draw drawrect swift swift3

我快3天了,我正在试图弄清楚如何画一个矩形.我对语言太新了,不知道要扩展的类和覆盖的方法,我已经四处寻找示例代码,但似乎没有任何工作(我将其归因于我对swift 3的使用).

我现在正在尝试的是:

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")

    }

}
Run Code Online (Sandbox Code Playgroud)

那没有做任何事情.救命.

vac*_*ama 14

为了查看视图,您需要创建一个并给它一个框架,以便它知道它有多大.

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

let d = Draw(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
Run Code Online (Sandbox Code Playgroud)

您可以点击右侧的快速查看,然后您将看到该视图.

黄色正方形在操场


您还可以添加视图作为一个子视图view在你的ViewController,然后你会看到它在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)
}
Run Code Online (Sandbox Code Playgroud)

请注意,您从不draw(_:)直接打电话.Cocoa Touch为您调用它来显示视图.

iPhoneSE上的黄色方块


coa*_*ist 8

创建一个类,我把它放在一个单独的Swift 3文件中.

//
//  Plot_Demo.swift
//
//  Storyboard is not good in creating self adapting UI
//  Plot_Demo creates the drawing programatically.

import Foundation
import UIKit

public class Plot_Demo: UIView
{
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

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

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

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

        color.set()
        bpath.stroke()

        print("it ran")
        NSLog("drawRect has updated the view")
    }
}
Run Code Online (Sandbox Code Playgroud)

UIViewController对象中使用的示例:

override func viewDidLoad() {
    super.viewDidLoad()

    // Instantiate a new Plot_Demo object (inherits and has all properties of UIView)
    let k = Plot_Demo(frame: CGRect(x: 75, y: 75, width: 150, height: 150))

    // Put the rectangle in the canvas in this new object
    k.draw(CGRect(x: 50, y: 50, width: 100, height: 100))

    // view: UIView was created earlier using StoryBoard
    // Display the contents (our rectangle) by attaching it
    self.view.addSubview(k)
}
Run Code Online (Sandbox Code Playgroud)

在iPhone模拟器和iPhone上运行:

在此输入图像描述

使用XCode版本8.0(8A218a),Swift 3,目标iOS 10.0

  • 从`draw(_ :)`文档: - 你永远不应该直接调用这个方法.要使视图的一部分无效,从而导致重绘该部分,请调用`setNeedsDisplay()`或`setNeedsDisplay(_ :)`方法. (2认同)