- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 swift 编程的新手,遇到了这个问题,老实说,我不知道如何解决这个问题。我在 Playground 上有一个 View Controller ,并且在该 View 上有一个按钮。我有一个名为 CanvasView 的类,它具有以下代码:
import UIKit
import PlaygroundSupport
public class CanvasView: UIView {
var lineColor: UIColor!
var lineWidth: CGFloat!
var path: UIBezierPath!
var touchPoint: CGPoint!
var startPoint: CGPoint!
public override func layoutSubviews() {
setupDefaultSettings()
}
// control touches
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
startPoint = touch.location(in: self)
path = UIBezierPath()
path.move(to: startPoint)
}
}
public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
touchPoint = touch.location(in: self)
}
path.addLine(to: touchPoint)
startPoint = touchPoint
drawShapeLayer()
}
// Function to clear the drawn path
func clearCanvas() {
guard let path = path else { return }
path.removeAllPoints()
layer.sublayers = nil
setNeedsDisplay()
}
// draw shape on layer
public func drawShapeLayer() {
guard let path = path else { return }
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = lineWidth
shapeLayer.fillColor = UIColor.clear.cgColor
layer.addSublayer(shapeLayer)
setNeedsDisplay()
}
public func setupDefaultSettings() {
lineColor = .black
lineWidth = 5
clipsToBounds = true
isMultipleTouchEnabled = false
}
}
如您所见,该类有一个名为 clearCanvas() 的方法,用于清除 Canvas 。在我的 View Controller 中,我创建了一个继承自 CanvasView 类的对象,并尝试使用按钮调用 clearCanvas 函数,但这给了我类 GameViewController 没有成员 drawView 的错误?即使它显示它确实像上面的 10 行。 GameViewController 代码:
import Foundation
import CoreML
import UIKit
import PlaygroundSupport
public class GameViewController : UIViewController {
public override func viewDidLoad() {
super.viewDidLoad()
//MARK: Variables and constants
let yellowColor = UIColor(red:1.00, green:0.92, blue:0.23, alpha:1.0)
var roundNumber = 1
var currentScore = 0
//MARK: Setting up the UI
//Setting up the initial view
let view = UIView()
self.view = view
view.backgroundColor = yellowColor
//Adding the view which can be drawn on
let drawView = CanvasView()
drawView.backgroundColor = UIColor.white
drawView.frame = CGRect(x: 12.5, y: 250, width: 350, height: 350)
drawView.layer.cornerRadius = 20
view.addSubview(drawView)
//Adding a button to start the round
let startRoundButton = UIButton()
startRoundButton.frame = CGRect(x: 0, y: 50, width: 100, height: 50)
view.addSubview(startRoundButton)
startRoundButton.addTarget(self, action: #selector(startRoundButtonPressed), for: .touchUpInside)
//Adding a button clear the canvas
let clearCanvasButton = UIButton()
let clearCanvasButtonImage = UIImage(named: "clearButton")
clearCanvasButton.setImage(clearCanvasButtonImage, for: .normal)
clearCanvasButton.frame = CGRect(x: 12.5, y: 620, width: 110, height: 27)
view.addSubview(clearCanvasButton)
clearCanvasButton.addTarget(self, action: #selector(clearCanvasButtonPressed), for: .touchUpInside)
//MARK: Functions
func clearDrawView() {
drawView.clearCanvas()
}
}
@objc func startRoundButtonPressed() {
}
@objc func clearCanvasButtonPressed() {
GameViewController.clearDrawView()
}
}
我确实复制了 CanvasView 类,所以可能有什么东西搞砸了那里的方法调用。如果有人可以帮助我解决这个(可能非常愚蠢的)问题,我将不胜感激,请记住这是在 mac 上的 Playground 上而不是在 xcode 中制作的。一百万!
最佳答案
您当前的 clearDrawView()
确实不是 viewDidLoad()
中的成员,而是一个嵌套函数。
将定义移动到类级别应该会有所帮助。但是,您还必须将 drawView
定义为成员(即在类级别)以使其对 clearDrawView()
可见:
public class GameViewController : UIViewController {
let drawView = CanvasView() // <--- move it here from viewDidLoad()
public override func viewDidLoad() {
super.viewDidLoad()
// do the necessary stuff, including drawView setup
}
func clearDrawView() {
drawView.clearCanvas()
}
@objc func clearCanvasButtonPressed() {
clearDrawView() // note: not "GameViewController.clearDrawView()" because it's an instance method
}
}
为了更好地理解检查Properties和 Methods在 Apple 文档中。
关于ios - Swift Playgrounds 类 Viewcontroller 没有成员 ""?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465649/
最初我读到你不能在操场上尝试触摸或手势。然后我发现 touchesMoved 或点击手势会使用react。但是,我现在正在测试 UIPinchGestureRecognizer。没有可以让您在操场上使
我最近尝试打开/编辑 .playgroundbook project在 Xcode 版本 8.3.2 (8E2002) 中,有点惊讶它没有作为“liveView Playground”打开(我意识到它
我想在 Xcode 6.0.1 中使用 playground。所以文件 -> 新建 -> Playground 我立即得到错误。 Playground 执行终止,因为 Playground 进程意外退
在 Xcode 7 playgrounds 中运行最简单的默认代码...得到以下错误 Playground execution failed: Execution was interrupted, r
我已经开始了 Swift 编程入门类(class),到目前为止进展顺利,但我无法使用 Mac 来运行 Xcode。我确实有 iPad 版 Swift Playgrounds,它允许我完成作业并测试它们
GuidedTour Playground包含交互式和非交互式元素以及格式良好的文本。文本似乎已被锁定,无法编辑。我发现它是一个很好的学习工具。是否可以编辑其内容或创建类似的内容? 最佳答案 Play
我在 Xcode Playground 和 Swift Playground 上运行这段代码: import SwiftUI import PlaygroundSupport struct Conte
我正在 Xcode Playground 上玩弄 REST API,我需要用 SHA1 散列一些东西。我找到的所有解决方案都依赖于 Common Crypto,而这似乎不能直接在 Swift play
我知道如何在常规应用程序项目中使用 Xcode 11 中的 SwiftUI,但我想知道即使我不能使用实时编辑器,是否也可以在 playgrounds 中使用它? 最佳答案 当然,这很简单: impor
我知道如何在常规应用程序项目中使用 Xcode 11 中的 SwiftUI,但我想知道即使我不能使用实时编辑器,是否也可以在 playgrounds 中使用它? 最佳答案 当然,这很简单: impor
新创建的 Xcode 10 playground 提供了一个播放按钮来逐步执行 playground。 如果我打开在 iPad 或以前版本的 Xcode 中创建的 Playground ,播放按钮会丢
我注意到 Xcode 10 中的 Playgrounds 不再允许使用已声明但未初始化的变量。例如: 虽然此代码可以在 Xcode 9 playground 中运行,但在 Xcode 10 playg
我已经更新到 Xcode 6,最新的出现在 AppStore 上。我正在运行 OS X Yosemite Beta 3,显然,我只能打开 iOS playground,列表中缺少 OS X。 我附上了
我在 xcode 中开发了一个 ios 应用程序,我想在 swift playground 中运行这个应用程序。所以我想将我的 .xcodeproj 项目转换为 .playground 格式。那么有什
我尝试使用以下代码行创建滤色器 let parameters = [kCIInputColorKey: CIColor(string: "0.5 0.7 0.3 1.0")] let colorFil
我尝试使用以下代码行创建滤色器 let parameters = [kCIInputColorKey: CIColor(string: "0.5 0.7 0.3 1.0")] let colorFil
每次我创建一个新的 playground 以测试一些代码时,Xcode 都会卡住并且不会运行代码。它只是在屏幕顶部显示“Running playground”或“Launching simulator
我在应用程序开发方面相对较新,但出于兴趣,我想在 Xcode 上的 Playground 项目上实现此应用程序: http://merowing.info/2015/11/the-beauty-of-
我正在尝试使用 GraphQL 记录我的 API。对于可读性问题,我想在多行中留下评论,但它似乎不适用于常规的 '\n' 换行符 """ Return:\n true : DB save succ
我是 Swift 世界的新手。 如何将带逗号的字符串转换为带小数的字符串? 代码使用点 (.) 就可以正常工作 问题是当我使用逗号 (,) ... 时:var Price 问题的根源是十进制法语键盘使
我是一名优秀的程序员,十分优秀!