【问题标题】:How do i tell my UIView that the button in UIViewController has been pressed? (Swift)我如何告诉我的 UIView UIViewController 中的按钮已被按下? (迅速)
【发布时间】:2017-05-14 22:32:39
【问题描述】:

是否可以在UIViewControllerUIView 之间传递数据? 如果是,如何? 我在互联网上找不到任何解释它的东西。

例如,当您按下按钮 1 时,UIViewController 告诉UIView 该人按下了按钮 1,然后UIView 绘制了一个圆圈。但是如果你按下button2UIViewController 告诉 UIView 他现在按下了button2,然后UIView 消除了圆圈并绘制了一个三角形。

这些按钮是以编程方式构建的。

问题是如何在UIViewController 中的UIView 中调用我用drawRect 绘制的形状。

class ViewController: UIViewController { 
   @IBOutlet var UnitViewTabeller: UnitView!
   var btn: UIButton!

   override viewDidLoad {
     self.btn = UIButton(frame: CGRectMake(0.04 * view.bounds.width, 0.91 * view.bounds.height, 0.44 * view.bounds.width, 0.07 * view.bounds.height))  //set frame
            self.btn.layer.cornerRadius = 10
            self.btn.setTitle("0", forState: .Normal)  //set button title
            self.btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
            self.btn.backgroundColor = UIColor(red: 0.2745, green: 0.2784, blue: 0.2706, alpha: 1.0) //set button background color
            self.btn.tag = 0 // set button tag
            self.btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
            self.view.addSubview(btn) //add button in view
            self.btnArray.append(btw)
   }

   func btnclicked(sender: UIButton) {
       //draw the Circle from the UIView
   }


}    
    class CircleView: UIView {

    override func drawRect(rect: CGRect) {
        let Circle = UIGraphicsGetCurrentContext()
        let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
        CGContextSetLineWidth(Circle, 4.0)
        CGContextAddEllipseInRect(Circle, rectangle)
        CGContextStrokePath(Circle)
    }
}

【问题讨论】:

    标签: ios iphone swift uiview uiviewcontroller


    【解决方案1】:

    这是 Swift 3 中 ShapeView 的一个示例,它在设置其 shape 属性时重绘自身:

    class ShapeView: UIView {
        var shape: Shape = .blank {
            didSet {
                // calling setNeedsDisplay() triggers a redraw of ShapeView
                setNeedsDisplay()
            }
        }
    
        enum Shape {
            case circle
            case triangle
            case blank
        }
    
        override func draw(_ rect: CGRect) {
            switch shape {
            case .circle: drawCircle()
            case .triangle: drawTriangle()
            default: break
            }
        }
    
        func drawCircle() {
            let circle = UIGraphicsGetCurrentContext()
            let rectangle = CGRect(x: 0.2 * bounds.width, y: 0.15 * bounds.height, width: 0.6 * bounds.width, height: 0.6 * bounds.width)
            circle?.setLineWidth(4.0)
            circle!.addEllipse(in: rectangle)
            circle!.strokePath()
        }
    
        func drawTriangle() {
            // code to draw a triangle
        }
    }
    

    假设您有一个名为shapeViewShapeView 对象作为ViewController 的属性,您可以这样称呼它:

    func drawCircleButton(_ sender: UIButton) {
        shapeView.shape = .circle
    }
    
    func drawTriangleButton(_ sender: UIButton) {
        shapeView.shape = .triangle
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多