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

查看:194
本文介绍了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

ViewController代码:

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

    }

}

(注意:每次按此按钮时,日志都会更新,因此没问题,只是显示永远不会改变。而且,我已经ed用随机坐标绘制矩形,所以不是不是正在更新,而是我没有看到它。)

(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指定一个指定的初始化程序(以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天全站免登陆