I'm working on a simple app that allows the user draw using one of four provided colors and share/export their work. To change the brush size, you must enter a "settings" view and adjust it via slider. The problem I'm having is that the value of the slider does not affect the size of the brush upon returning to the main view. Whatever I set the initial brush size to be, thats how it remains no matter how many times I mess with the slider.
The two views are managed by a NavigationViewController if that makes a difference.
import UIKit
class DrawingViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var buttonsStackView: UIStackView!
var lastPoint = CGPoint.zero
var red : CGFloat = 0.0
var green : CGFloat = 0.0
var blue : CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(DrawingViewController.appBecameActive), name: UIApplicationDidBecomeActiveNotification, object: nil)
blueTapped(UIButton())
}
func appBecameActive() {
self.buttonsStackView.hidden = false
}
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
print("Current Brush Size: \(SettingsViewController().brushSize)")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let point = touch.locationInView(self.imageView)
self.lastPoint = point
}
self.buttonsStackView.hidden = true
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let point = touch.locationInView(self.imageView)
//print("Point: \(point)")
drawBetweenPoints(self.lastPoint, secondPoint: point)
self.lastPoint = point
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let point = touch.locationInView(self.imageView)
//print(point)
drawBetweenPoints(self.lastPoint, secondPoint: point)
}
self.buttonsStackView.hidden = false
}
func drawBetweenPoints(firstPoint:CGPoint, secondPoint:CGPoint) {
UIGraphicsBeginImageContext(self.imageView.frame.size)
let context = UIGraphicsGetCurrentContext()
self.imageView.image?.drawInRect(CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height))
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y)
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y)
//randomTapped(UIButton())
CGContextSetRGBStrokeColor(context, self.red, self.green, self.blue, 1.0)
CGContextSetLineCap(context, .Round)
CGContextSetLineWidth(context, CGFloat(SettingsViewController().brushSize))
CGContextStrokePath(context)
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
func eraseEasel() {
self.imageView.image = nil
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "drawingToSettingsSegue" {
let settingsVC = segue.destinationViewController as! SettingsViewController
settingsVC.drawingVC = self
}
}
@IBAction func blueTapped(sender: AnyObject) {
self.red = 56 / 255
self.green = 109 / 255
self.blue = 229 / 255
}
@IBAction func greenTapped(sender: AnyObject) {
self.red = 37 / 255
self.green = 235 / 255
self.blue = 114 / 255
}
@IBAction func redTapped(sender: AnyObject) {
self.red = 229 / 255
self.green = 56 / 255
self.blue = 56 / 255
}
@IBAction func yellowTapped(sender: AnyObject) {
self.red = 249 / 255
self.green = 215 / 255
self.blue = 23 / 255
}
@IBAction func randomTapped(sender: AnyObject) {
self.red = CGFloat(arc4random_uniform(255)) / 255
self.green = CGFloat(arc4random_uniform(255)) / 255
self.blue = CGFloat(arc4random_uniform(255)) / 255
}
}
import UIKit
class SettingsViewController: UIViewController {
weak var drawingVC : DrawingViewController? = nil
@IBOutlet weak var brushSlider: UISlider!
var brushSize : Float = 15
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationController?.navigationBarHidden = false
}
override func viewWillAppear(animated: Bool) {
brushSlider.value = self.brushSize
}
@IBAction func eraseTapped(sender: AnyObject) {
self.drawingVC?.eraseEasel()
self.navigationController?.popViewControllerAnimated(true)
}
@IBAction func shareTapped(sender: AnyObject) {
if let image = drawingVC?.imageView.image {
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
}
@IBAction func brushSlider(sender: AnyObject) {
self.brushSize = brushSlider.value
print(self.brushSize)
}
}
You're always getting a new brush size, which is the default value.
SettingsViewController().brushSize
This line creates a new SettingsViewController
object and gets the brushSize
value from it which is 15.
Create an outlet at the top of your main view controller and use that one if you want to use an instance of the brush size that doesn't persist between app runs.
var settingsVC = SettingsViewController()
then replace all of the
SettingsViewController().brushSize
with
settingsVC.brushSize
However, even after you do this, you are instantiating your SettingsViewController
object from a storyboard. So that will be a completely difference instance than the one you create at the top of your main view controller until you segue to the settings view, unless it is an embed segue. If you want the initial brush size to always be 15, and to only update after you have been to the settings screen, then you can set your local reference in your prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "drawingToSettingsSegue" {
let settingsVC = segue.destinationViewController as! SettingsViewController
settingsVC.drawingVC = self
self.settingsVC = settingsVC
}
}
Good luck, hopefully this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.