Swift/Uiview/Drawrect - How to Get Drawrect to Update When Required

Swift / UIView / drawrect - how to get drawrect to update when required

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 - how to get drawrect to update when required

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

}

}

drawRect won't update display

If you are creating drawExamples's instance in Storyboard, you have to override

init?(coder aDecoder: NSCoder)

or

awakeFromNib()

instead of

override init(frame: CGRect)

to make a timer. I prefer awakeFromNib.

So the code is like below.

class drawExamples: UIView {
var shapePosX : CGFloat = 0
var shapePosY : CGFloat = 0

override func awakeFromNib() {
var time1 = NSTimer.scheduledTimerWithTimeInterval(
1,
target: self,
selector: #selector(update),
userInfo: nil,
repeats: true)
}

func update() {
shapePosX = shapePosX + 1
shapePosY = shapePosY + 1
self.setNeedsDisplay()
}

iPhone SDK: How to trigger drawRect on UIView subclass after orientation change?

[myView setContentMode:UIViewContentModeRedraw];

You can set this in IB as well (i.e., set mode to "redraw")

In Swift, drawRect for custom view is not updating properly

I believe you are talking to 2 different OCIndicator instances.

I believe you have an OCIndicator (in your StoryBoard perhaps) hooked up to an IBOutlet in your ViewController.

In viewDidLoad you are creating a second instance of OCIndicator and assigning it to the local variable indicator and you are adding that to your subView which is covering up the first one. But then, you are setting fractionalHeight on self.indicator which is not the same indicator and you are telling that one that it needs to display, but it has been covered up by the one added in viewDidLoad.

So, get rid of:

var indicator: OCIndicator = OCIndicator(frame: CGRectMake(10, 30, 50, 400))
self.view.addSubview(indicator)

from viewDidLoad and see how that works.



Related Topics



Leave a reply



Submit