我正在开发一个简单的应用程序,允许用户使用四种颜色中的一种进行绘制并共享/导出他们的工作。要更改画笔大小,您必须输入"设置"通过滑块查看和调整它。我遇到的问题是滑块的值在返回主视图时不会影响画笔的大小。无论我设置初始画笔大小是什么,这就是它如何保持无论多少次我搞乱滑块。
这两个视图由NavigationViewController管理,如果这会产生影响。
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)
}
}
答案 0 :(得分:1)
您总是会获得新的画笔大小,这是默认值。
SettingsViewController().brushSize
此行创建一个新的SettingsViewController
对象,并从中获取brushSize
值,即15。
在主视图控制器的顶部创建一个插座,如果要使用在应用程序运行之间不会持续存在的画笔大小的实例,请使用该插座。
var settingsVC = SettingsViewController()
然后替换所有
SettingsViewController().brushSize
带
settingsVC.brushSize
但是,即使执行此操作,您也要从故事板中实例化SettingsViewController
对象。这将是一个完全不同的实例,而不是您在主视图控制器顶部创建的实例,直到您转到设置视图,除非它是嵌入的segue。如果您希望初始画笔大小始终为15,并且仅在进入设置屏幕后进行更新,则可以在prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "drawingToSettingsSegue" {
let settingsVC = segue.destinationViewController as! SettingsViewController
settingsVC.drawingVC = self
self.settingsVC = settingsVC
}
}
祝你好运,希望这会有所帮助!