Swift/UIView/drawrect - 如何在需要时更新 drawrect [英] Swift / UIView / drawrect - how to get drawrect to update when required

查看:19
本文介绍了Swift/UIView/drawrect - 如何在需要时更新 drawrect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学习 Swift 的新手,并且正在尝试运行一个非常简单的应用程序.我要做的就是在按下按钮时让 UIView.drawRect 更新.它在应用程序第一次加载时更新/绘制,然后什么都不做,无论我尝试什么.几天来我一直在反对这个问题,但我找不到任何帮助.

I'm new to learning Swift, and am trying to get an incredibly simple app to run. All I'm trying to do is get UIView.drawRect to update when I press a button. It updates/draws when the app first loads, and then nothing after that, whatever I try. I've been hitting my head against this for a couple of days, and nothing I can find helps.

我创建的:

  • 单视图应用

  • single view application

一个按钮,作为一个动作链接到视图控制器

one button, linked to view controller as an action

一个新的类,Test_View,继承了 UIView

a new class, Test_View, subclassing UIView

视图控制器代码:

class ViewController: UIViewController {

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

        var f = Test_View()

    }

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

    @IBAction func Button_Pressed(sender: AnyObject) {
        var f = Test_View()
        f.setNeedsDisplay()
        NSLog("Button pressed.")
    }

}

Test_View 代码:

class Test_View: UIView {

    override func drawRect(rect: CGRect) {
        let h = rect.height
        let w = rect.width
        var color:UIColor = UIColor.yellowColor()

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

        NSLog("drawRect has updated the view")            

    }

}

(注意:每次按下按钮都会更新日志,所以这不是问题.只是显示永远不会改变.另外,我尝试用随机坐标绘制矩形,所以不是它正在更新但我没有看到.)

(Note: the log is updated every time I press the button, so that's not the problem. It's just that the display never changes. Also, I've tried drawing the rectangle with random coordinates, so it's not that it's updating but I'm not seeing it.)

感谢您的帮助!

推荐答案

首先需要为 UIView 指定一个指定的初始化器(init with frame).然后使您的对象 f 成为类常量或变量(取决于您的需要),以便可以在项目范围内访问它.此外,您必须将其添加为视图的子视图.它看起来像这样:

First, you need to specify a designated initializer for UIView (init with frame). Then make your object f a class constant or variable (depending on your need) so it can be accessed within the scope of the project. Also, you must add it as a subview of your view. It looks like this:

import UIKit

class ViewController: UIViewController {
    let f = Test_View(frame: CGRectMake(0, 0, 50, 50))

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(f)
    }

    @IBAction func buttonPressed(sender: UIButton) {
        f.setNeedsDisplay()
    }
}

class Test_View: UIView {

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

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func draw(_ rect: CGRect) {
        let h = rect.height
        let w = rect.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()

        NSLog("drawRect has updated the view")

    }

}

这篇关于Swift/UIView/drawrect - 如何在需要时更新 drawrect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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