编辑代码

import UIKit

class CoreGraphUIView : UIView{
     override func touchesBegan(_touches:Set<UITouch>, with event: UIEvent?){
         self.backgroundColor= UIColor.red
         print("Brgan:\(touches)")
     }
      override func touchesEnded(_touches:Set<UITouch>,with event: UIEvent?){
          self.backgroundColor= UIColor.red
          print("Began:\(touches)")
      }

      override func touchesMoved(_touches: Set<UITouch>,with event: UIEvent?){
          self.backgroundColor = UIColor.lightGray
          print("Ended:\(touches)")
      }
}

class ViewController:UIViewController{

    override func viewDidLoad(){
        super.viewDidLoad()
       // Do any additional aetup after loading the view.
       let height = self.view.frame.size.height
       let width= self.view.frame.size.width
       let graphFrame =CGRect(x:0,y:0,width: width,height: height)
       let grapView =  CoreGraphUIView(frame: graphFrame)
       graphView.backgroundColor =UIColor.white
       self.view.addSubview(graphView)
    }
}



//5.2.1
class Shape{
    //名称
    var name  :  String?
    //边数
    var sides  : Int?
    //左上角的位置坐标
    var origin   : CGPoint?
    //线条颜色
    var lineColor  :  UIColor= UIColor.red
    //填充颜色
    var fillColor : UIColor=UIColor.green
    //线条宽度
    var lineWidth :   CGPLoat=2
    //构造器函数init
    init(name : String, sides:Int,origin: CGPoint){
        self.naem=name
        self.sides=sides
        self.origin=origin
    }
    //自定义方法sayHello
    func sayHello(){
        print("Shape is \(name!),sides \(sides!),and originCord is (\origin!.x),\(origin!.y)")
    }
}


//5.2.2
//
import UIKit

class Shape{
    //名称
    var name :  String?
    //边数
     var sides  : Int?
    //左上角的位置坐标
    var origin   : CGPoint?
    //线条颜色
    var lineColor  :  UIColor= UIColor.red
    //填充颜色
    var fillColor : UIColor=UIColor.green
    //线条宽度
    var lineWidth :   CGPLoat=2
    //构造器函数init
    init(name : String, sides:Int,origin: CGPoint){
        self.naem=name
        self.sides=sides
        self.origin=origin
    }
    //自定义方法sayHello
    func sayHello(){
        print("Shape is \(name!),sides \(sides!),and originCord is (\origin!.x),\(origin!.y)")
    }
}

class  ViewController: UIViewController{
    override func viewDidLoad(){
        super.viewDidLoad()
        //Do any additional setup after loading the view.
        //此处调用Shape类,建立一个对象(实例)myShaple
        let myShape =Shape(name:"BaseShape",sides:0, origin: CGPoint(x: 0,y:0))
        myShape.sayHello()
    }
}


5.2.3
import UIKit

class Shape{
    //名称
    var name: String?
    //边数
       var sides  : Int?
    //左上角的位置坐标
    var origin   : CGPoint?
    //线条颜色
    var lineColor  :  UIColor= UIColor.red
    //填充颜色
    var fillColor : UIColor=UIColor.green
    //线条宽度
    var lineWidth :   CGPLoat=2
    //构造器函数init
    init(name : String, sides:Int,origin: CGPoint){
        self.naem=name
        self.sides=sides
        self.origin=origin
    }
    //自定义方法sayHello
    func sayHello(){
        print("Shape is \(name!),sides \(sides!),and originCord is (\origin!.x),\(origin!.y)")
    }
}

class CzfView: UIView{
    //成员变量(属性)shape,其类型为Shape
    var shape : Shape?
    //重载UIView的draw方法
    override func draw(_rect: CGRect){
        //判断shape变量是否为空值nil
        guard let s =shape else{
            return
        }
        //不为空,则调用shape这个实力方法
        s.sayHello()
    }
}

class ViewController: UIViewController{
     
     override func viewDidLoad(){
         super.viewDidLoad()
         //Do any additional step after loading the view, typically  from a nib.
         //此处建立了一个CzfView的实例myShape
         let myShape =Shape(name:"BaseShape",)
     }
}