撤消/重做图像进度

发布于 2025-02-01 03:46:25 字数 922 浏览 5 评论 0原文

我有一个更改图片的功能,并且有两个取消/返回按钮 我为此写了3个功能 添加 撤消 重做 问题是,当我按2次撤消然后重做时,我的图像重做是两个步骤向前

我的代码

 public var undoHistory: [UIImage] = []
public var redoHistory: [UIImage] = []

public func undoImage() -> UIImage? {
    guard !(undoHistory.count == 1) else {
        return undoHistory.last
    }
    
    if let lastImage = undoHistory.last {
        redoHistory.append(lastImage)
        undoHistory.removeLast()
    }
    return undoHistory.last
}

public func addImageToHistory(_ image: UIImage) {
    undoHistory.append(image)

    guard !redoHistory.isEmpty else {
        return
    }
    redoHistory.removeAll()
}

public func redoImage() -> UIImage? {
    guard !(redoHistory.count == 1) else {
        return redoHistory.last
    }
    
    if let lastImage = redoHistory.last {
        redoHistory.removeLast()
        undoHistory.append(lastImage)
    }
    return redoHistory.last
}

I have a functionality that changes the picture, and there are two cancel/return buttons
i wrote 3 functions for this
add
undo
redo
The problem is that when I press 2 times undo and then redo, my image redo is two steps forward

My code

 public var undoHistory: [UIImage] = []
public var redoHistory: [UIImage] = []

public func undoImage() -> UIImage? {
    guard !(undoHistory.count == 1) else {
        return undoHistory.last
    }
    
    if let lastImage = undoHistory.last {
        redoHistory.append(lastImage)
        undoHistory.removeLast()
    }
    return undoHistory.last
}

public func addImageToHistory(_ image: UIImage) {
    undoHistory.append(image)

    guard !redoHistory.isEmpty else {
        return
    }
    redoHistory.removeAll()
}

public func redoImage() -> UIImage? {
    guard !(redoHistory.count == 1) else {
        return redoHistory.last
    }
    
    if let lastImage = redoHistory.last {
        redoHistory.removeLast()
        undoHistory.append(lastImage)
    }
    return redoHistory.last
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文