(作業)文件、自定製視圖、代碼版 helloworld

文件、自定製視圖、代碼版 helloworld

這次的作業都比較簡單,也開始涉及到了iOS的UI,所以現在創建的工程至少要使用單視圖工程纔可以完成。具體步驟不再贅述,直接上代碼。

第一題:

let manager = FileManager.default
let document = manager.urls(for: .documentDirectory, in: .userDomainMask).first?.path
let file = document?.appending("/image") //圖片文件夾

//print(document!) //輸出Document路徑,方便查看
if manager.fileExists(atPath: file!) {  //若該文件夾存在
    let image = file?.appending("/bd.png")  //圖片文件
    if manager.fileExists(atPath: image!) {  //若該文件存在,則顯示到界面上
        do{
            let data = try Data(contentsOf: URL(fileURLWithPath: image!))
            let img = UIImage(data: data)
            let imageView = UIImageView(image: img)
            imageView.frame = CGRect(x: 0, y: 100, width: 400, height: 300)
            self.view.addSubview(imageView)
        } catch {
            print(error)
        }
    } else {  //若不存在,則從網絡下載一個圖片並保存爲圖片文件
        let url = URL(string: "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png")
        do{
            let data = try Data(contentsOf: url!)
            try data.write(to: URL(fileURLWithPath: image!), options: .atomicWrite)
            print("文件不存在,已成功創建文件")
        } catch {
            print(error)
        }
    }
} else {  //若該文件夾不存在,則創建該文件夾
    do {
        try manager.createDirectory(atPath: file!, withIntermediateDirectories: true, attributes: nil)
        print("文件夾不存在,已成功創建文件夾")
    } catch {
        print(error)
    }
}

因爲只是學習使用,所以爲了方便,這部分代碼可以直接寫到viewDidLoad方法中。接下來再貼一張在模擬器中運行的效果圖
圖片存在時的運行效果

第二題:

首先創建自定義的UIView

class MyView: UIView {

    override func draw(_ rect: CGRect) {
        let path = UIBezierPath(ovalIn: rect)

        UIColor.red.setStroke()  //紅色邊界
        path.stroke()

        UIColor.yellow.setFill()  //黃色填充
        path.fill()
    }

}

然後在ViewController中使用自定義的UIView創建一個橢圓和一個正圓

let oval = MyView(frame: CGRect(x: 100, y: 100, width: 150, height: 100))
oval.backgroundColor = UIColor.clear
self.view.addSubview(oval)

let circle = MyView(frame: CGRect(x: 100, y: 300, width: 100, height: 100))
circle.backgroundColor = UIColor.clear
self.view.addSubview(circle)

最後時模擬器運行的效果
第二題運行效果

第三題:

class ViewController: UIViewController {

    var label: UILabel!
    var btn: UIButton!

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

        label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 44))
        label.text = "hello Swift"
        label.font = UIFont.systemFont(ofSize: 22)
        label.textColor = UIColor.black
        self.view.addSubview(label)

        btn = UIButton(frame: CGRect(x: 100, y: 400, width: 50, height: 37))
        btn.setTitle("Click", for: .normal)
        btn.backgroundColor = UIColor.brown
        btn.setTitleColor(UIColor.black, for: .highlighted)
        self.view.addSubview(btn)
        btn.addTarget(self, action: #selector(cilcked), for: .touchUpInside)
    }

    @IBAction func cilcked() {
        if label.text! == "Clicked" {
            label.text = "hello Swift"
            btn.setTitle("Click", for: .normal)
        } else {
            label.text = "Clicked"
            btn.setTitle("hello", for: .normal)
        }
    }

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

}

因爲這裏涉及的代碼不只在viewDidLoad方法中,所以貼了整個ViewController的代碼。接下來看運行效果:
運行效果

發佈了44 篇原創文章 · 獲贊 19 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章