[英]Using the delegate function in Xcode to produce an image when the user releases their finger off of the screen
I am trying to learn to build a simple app and i keep running into the issue of delegate.我正在尝试学习构建一个简单的应用程序,但我一直遇到委托问题。 I have two classes one which is setting up the original images for the app and allowing them to be moved around by the user.
我有两类,一类是为应用程序设置原始图像并允许用户移动它们。 The other class is the ViewController class which is where I am writing the code for the delegation to create the other object that appears when the user releases the initial object on the app.
另一个类是 ViewController 类,我正在为委托编写代码以创建当用户释放应用程序上的初始对象时出现的另一个对象。
In the View Controller class the code I have written is: enter image description here在 View Controller 类中我写的代码是:在此处输入图片描述
I am trying to get it to work with the function "func endtouches" in the other class however I am unsure of how to do this.我试图让它与另一个类中的函数“func endtouches”一起工作,但是我不确定如何做到这一点。
Any help or guidance would be extremely appreciated任何帮助或指导将不胜感激
**edit **编辑
import UIKit
protocol AppearBall {
func addImage()
} }
class ViewController: UIViewController, AppearBall{
func addImage() {
img = UIImage(contentsOfFile: "ShoeboxImageView.png")
}
var delegate: AppearBall?
@IBOutlet var img: UIImage!
@IBOutlet weak var ShoeboxImageView: DragImageView!
override func viewDidLoad() {
super.viewDidLoad()
ShoeboxImageView?.myDelegate = self
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} } } }
Here's a simple pattern.这是一个简单的模式。
Start by creating a protocol, as you have.首先创建一个协议,就像你一样。 (But uppercase because it's a type.)
(但大写,因为它是一种类型。)
protocol ImageProducer {
func addImage()
}
Create the class that will use a delegate to accomplish something.创建将使用委托完成某事的类。
class TouchHandler {
var delegate: ImageProducer?
func endTouches() {
guard let del = delegate else {
print("No delegate set")
return
}
print("Sending delegate request")
del.addImage()
}
}
Provide an implementation of the delegate protocol.提供委托协议的实现。 (This doesn't actually work because I don't have images. So, ignore anything other than handler/delegate interaction.)
(这实际上不起作用,因为我没有图像。因此,忽略处理程序/委托交互以外的任何内容。)
class Worker: ImageProducer {
var box: UIImageView?
let img: UIImage?
init() {
img = UIImage(contentsOfFile: "somefile.png")
}
func startInteraction() {
let handler = TouchHandler()
handler.delegate = self
handler.endTouches()
}
func addImage() {
guard let image = img else {
print("Image didn't get created")
return
}
print("Creating image view")
box = UIImageView(image: image)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.