简体   繁体   English

当用户将手指从屏幕上松开时,使用 Xcode 中的委托函数生成图像

[英]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.

相关问题 检测用户何时将手指抬离UIScrollView - Detecting when user lifts finger off UIScrollView 离子2-手指不在屏幕上时不会触发滚动事件 - Ionic 2 - scroll event not fired when finger is off the screen SwiftUI:当用户从 longPressGesture 释放屏幕时如何运行操作? - SwiftUI: How to run action when user releases screen from a longPressGesture? 用户在屏幕上移动手指时在轴上的动画/旋转/平移视图 - Animate/Rotation/translation view on axis when user move finger on screen iOS:获取用户松开手指后 UISlider 的最终值 - iOS: Get the final value of UISlider after user releases the finger 如何找出一个人何时将手指离开屏幕? - how to find out when a person has left his finger off the screen? 使用 Vision 框架和面部移出屏幕时在 Xcode 中获取控制台垃圾邮件 - Getting console spam in Xcode when using Vision framework and face moves off screen 用户搜索时未调用AVPlayerViewController委托函数 - AVPlayerViewController delegate function not called when user seek 拖动手指时取消选择UIView“按钮” - Deselect UIView “button” when dragging finger off of it 用户将手指放在屏幕上时立即触发事件 - Fire an event as soon as user places a finger on screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM