Ios 从Swift中的不同类访问ViewController中的变量
我在ViewController中有一个滑块Ios 从Swift中的不同类访问ViewController中的变量,ios,swift,class,Ios,Swift,Class,我在ViewController中有一个滑块sliderLineSize和一个变量lineSize。UISliderSlidelLineSize更改lineSize。但是,lineSize实际用于附加到UIView的viewLine类的drawRect部分 问题: 如何将在ViewController中设置的变量lineSize传递到类viewLine,并将其用于drawRect? import UIKit class ViewController: UIViewController {
sliderLineSize
和一个变量lineSize
。UISliderSlidelLineSize
更改lineSize
。但是,lineSize
实际用于附加到UIView的viewLine
类的drawRect
部分
问题:
如何将在ViewController中设置的变量lineSize
传递到类viewLine
,并将其用于drawRect
?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
@IBOutlet weak var myImageView: UIImageView!
var lineSize: Int = 1
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
myImageView.alpha = 0.5
}
@IBAction func sliderLineSize(sender: UISlider) {
lineSize = Int(sender.value)
}
}
class viewLine: UIView {
let path=UIBezierPath()
var incrementalImage:UIImage?
var previousPoint:CGPoint = CGPoint.zero
var strokeColor:UIColor?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
incrementalImage?.drawInRect(rect)
path.lineWidth = lineSize
path.stroke()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: AnyObject? = touches.first
let currentPoint = touch!.locationInView(self)
path.moveToPoint(currentPoint)
previousPoint=currentPoint
self.setNeedsDisplay()
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: AnyObject? = touches.first
let currentPoint = touch!.locationInView(self)
let midPoint = self.midPoint(previousPoint, p1: currentPoint)
path.addQuadCurveToPoint(midPoint,controlPoint: previousPoint)
previousPoint=currentPoint
path.moveToPoint(midPoint)
self.setNeedsDisplay()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.drawBitmap()
self.setNeedsDisplay()
path.removeAllPoints()
}
func midPoint(p0:CGPoint,p1:CGPoint)->CGPoint {
let x=(p0.x+p1.x)/2
let y=(p0.y+p1.y)/2
return CGPoint(x: x, y: y)
}
func drawBitmap() {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 1)
strokeColor?.setStroke()
if((incrementalImage) == nil){
let rectPath:UIBezierPath = UIBezierPath(rect: self.bounds)
UIColor.whiteColor().setFill()
rectPath.fill()
}
incrementalImage?.drawAtPoint(CGPointZero)
path.stroke()
incrementalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
导入UIKit
类ViewController:UIViewController{
@ibvar myView:UIView!
@ibvar myImageView:UIImageView!
变量lineSize:Int=1
重写func viewDidLoad(){
super.viewDidLoad()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
myImageView.alpha=0.5
}
@iAction func SlidelLineSize(发送方:UISlider){
lineSize=Int(sender.value)
}
}
类视图线:UIView{
let path=UIBezierPath()
var增量图像:UIImage?
var previousPoint:CGPoint=CGPoint.zero
var strokeColor:UIColor?
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
重写func drawRect(rect:CGRect){
增量图像?.drawInRect(rect)
path.lineWidth=线宽
path.stroke()
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
让触摸:AnyObject?=首先触摸
让currentPoint=touch!.locationInView(自)
path.moveToPoint(当前点)
前一点=当前点
self.setNeedsDisplay()
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让触摸:AnyObject?=首先触摸
让currentPoint=touch!.locationInView(自)
设中点=自身中点(上一点,p1:currentPoint)
path.addQuadCurveToPoint(中点、控制点:上一个点)
前一点=当前点
路径。移动点(中点)
self.setNeedsDisplay()
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
self.drawBitmap()
self.setNeedsDisplay()
path.removeAllPoints()
}
func中点(p0:CGPoint,p1:CGPoint)->CGPoint{
设x=(p0.x+p1.x)/2
设y=(p0.y+p1.y)/2
返回点(x:x,y:y)
}
func drawBitmap(){
UIGraphicsBeginImageContextWithOptions(self.bounds.size,true,1)
strokeColor?.setStroke()
如果((递增图像)=nil){
让rectPath:UIBezierPath=UIBezierPath(rect:self.bounds)
UIColor.whiteColor().setFill()
rectPath.fill()
}
增量图像?绘图点(CGPointZero)
path.stroke()
incrementalImage=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
}
您应该创建Singleton模型类。可以从任何地方访问单例类。下面是如何在swift中创建单例类
class ApplicationModel {
class var sharedInstance: ApplicationModel {
get {
struct Static {
static var instance: ApplicationModel? = nil
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token, {
Static.instance = ApplicationModel()
})
return Static.instance!
}
}
var lineSize = 1
}
内部视图控制器
override func viewDidLoad() {
super.viewDidLoad()
//Instantiate ApplicationModel
//GET
let lineSize = ApplicationModel.sharedInstance.lineSize
//SET
ApplicationModel.sharedInstance.lineSize = 5
}
内视图线
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Access Application Model
//GET
let lineSize = ApplicationModel.sharedInstance.lineSize
//SET
ApplicationModel.sharedInstance.lineSize = 5
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
//Access应用程序模型
//得到
让lineSize=ApplicationModel.sharedInstance.lineSize
//设置
ApplicationModel.sharedInstance.lineSize=5
}
希望这有帮助 您应该创建单例模型类。可以从任何地方访问单例类。下面是如何在swift中创建单例类
class ApplicationModel {
class var sharedInstance: ApplicationModel {
get {
struct Static {
static var instance: ApplicationModel? = nil
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token, {
Static.instance = ApplicationModel()
})
return Static.instance!
}
}
var lineSize = 1
}
内部视图控制器
override func viewDidLoad() {
super.viewDidLoad()
//Instantiate ApplicationModel
//GET
let lineSize = ApplicationModel.sharedInstance.lineSize
//SET
ApplicationModel.sharedInstance.lineSize = 5
}
内视图线
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Access Application Model
//GET
let lineSize = ApplicationModel.sharedInstance.lineSize
//SET
ApplicationModel.sharedInstance.lineSize = 5
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
//Access应用程序模型
//得到
让lineSize=ApplicationModel.sharedInstance.lineSize
//设置
ApplicationModel.sharedInstance.lineSize=5
}
希望这有帮助 有两种主要方法可以做到这一点 备选案文1: 为您的
ViewLine
类提供自己的lineSize
属性:
class ViewLine: UIView {
var lineSize = 1
}
为ViewController
提供对ViewLine
的引用,并在ViewController
中更改时使用属性观察者更新ViewLine
中的属性:
class ViewController: UIViewController {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var viewLine: ViewLine!
var lineSize = 1 {
didSet {
viewLine.lineSize = lineSize
}
}
}
class ViewLine: UIView {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var controller: ViewController!
}
现在,您的ViewLine
类将拥有自己的lineSize
属性,可以从其drawRect
方法中直接访问该属性
备选案文2:
将您的视图线
a级引用提供给视图控制器
:
class ViewController: UIViewController {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var viewLine: ViewLine!
var lineSize = 1 {
didSet {
viewLine.lineSize = lineSize
}
}
}
class ViewLine: UIView {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var controller: ViewController!
}
现在,在drawRect
方法中,将path.lineWidth=lineSize
替换为path.lineWidth=controller.lineSize
基本上,一个类需要引用另一个类才能进行通信。有两种主要方法 备选案文1: 为您的
ViewLine
类提供自己的lineSize
属性:
class ViewLine: UIView {
var lineSize = 1
}
为ViewController
提供对ViewLine
的引用,并在ViewController
中更改时使用属性观察者更新ViewLine
中的属性:
class ViewController: UIViewController {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var viewLine: ViewLine!
var lineSize = 1 {
didSet {
viewLine.lineSize = lineSize
}
}
}
class ViewLine: UIView {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var controller: ViewController!
}
现在,您的ViewLine
类将拥有自己的lineSize
属性,可以从其drawRect
方法中直接访问该属性
备选案文2:
将您的视图线
a级引用提供给视图控制器
:
class ViewController: UIViewController {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var viewLine: ViewLine!
var lineSize = 1 {
didSet {
viewLine.lineSize = lineSize
}
}
}
class ViewLine: UIView {
// v~~~ be sure to connect this outlet in interface builder
@IBOutlet weak var controller: ViewController!
}
现在,在drawRect
方法中,将path.lineWidth=lineSize
替换为path.lineWidth=controller.lineSize
基本上,您的一个类需要引用另一个类,以便它们能够通信。您的ViewController没有viewLine的引用,或者视图中没有任何视图