使用Xcode中的委托功能,在用户将手指从屏幕上释放时生成一张图像。

huangapple go评论62阅读模式
英文:

Using the delegate function in Xcode to produce an image when the user releases their finger off of the screen

问题

我正在尝试学习如何构建一个简单的应用程序,但一直遇到委托(delegate)的问题。
我有两个类,一个用于设置应用程序的原始图像,并允许用户移动它们。另一个类是 ViewController 类,我在其中编写代码以创建用户在应用程序上释放初始对象时出现的其他对象的委托。

在 ViewController 类中,我编写的代码如下:

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
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // 释放可重新生成的任何资源。
    }
}

我正在尝试让它与另一个类中的 "func endtouches" 函数一起工作,但我不确定如何做到这一点。任何帮助或指导都将非常感激。

英文:

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.

In the View Controller class the code I have written is:
enter image description here

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.

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.


}

}
}

答案1

得分: 1

这是一个简单的模式。

首先创建一个协议,就像您已经做的一样(但要大写,因为它是一个类型)。

protocol ImageProducer {
    func addImage()
}

创建将使用委托来完成某事的类。

class TouchHandler {
    var delegate: ImageProducer?
    
    func endTouches() {
        guard let del = delegate else {
            print("未设置委托")
            return
        }
        print("发送委托请求")
        del.addImage()
    }
}

提供委托协议的实现。 (这实际上不起作用,因为我没有图像。所以,除了处理程序/委托交互之外,忽略其他任何内容。)

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("未创建图像")
            return
        }
        print("创建图像视图")
        box = UIImageView(image: image)
    }
}
英文:

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)
    }
}

huangapple
  • 本文由 发表于 2020年1月4日 01:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定