ios - 当我快速单击图像时将 UIButtons 移回

标签 ios swift

当我点击图像时,我需要将 UIButtons 移到 View 内,当我再次点击同一图像时,我需要将 UIButtons 移出屏幕。

当我点击一个 UIImage 时,4 个 UIButtons 在屏幕内移动,代码如下没有问题。

    func imageTapped(img: AnyObject)
        {UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
            self.imageReceivedTrashCan.center = CGPoint(x: 30, y: 530)
            self.imageReceivedRightArrow.center = CGPoint(x: 285, y: 530)
            self.imageReceivedForward.center = CGPoint(x: 160, y: 530)
            self.imageReceivedCross.center = CGPoint(x: 30, y: 30)
        },
        completion: { finished in
            print("Objects moved!")   
    })
}

但是,我找不到当我再次单击同一图像时如何将 UIButton 移回原始位置的方法。感谢您的帮助。

最佳答案

即使这看起来真的很老派,没有任何自动布局,我随机猜测你不会同时更改中心的其他地方和所有中心:

func imageTapped(img: AnyObject) {
   if (self.imageReceivedTrashCan.center == CGPoint(x: 30, y: 530)) {
      UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
            self.imageReceivedTrashCan.center = CGPoint(x: -30, y: -530)
            self.imageReceivedRightArrow.center = CGPoint(x: -285, y: -530)
            self.imageReceivedForward.center = CGPoint(x: -160, y: -530)
            self.imageReceivedCross.center = CGPoint(x: -30, y: -30)
        },
        completion: { finished in
            print("Objects moved!")   
    })
} else {
UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
            self.imageReceivedTrashCan.center = CGPoint(x: 30, y: 530)
            self.imageReceivedRightArrow.center = CGPoint(x: 285, y: 530)
            self.imageReceivedForward.center = CGPoint(x: 160, y: 530)
            self.imageReceivedCross.center = CGPoint(x: 30, y: 30)
        },
        completion: { finished in
            print("Objects moved!")   
    })
}
}

关于ios - 当我快速单击图像时将 UIButtons 移回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35724862/

相关文章:

ios - 调试 xcode 阶段?

ios - 如何给 SwiftUI arc 添加阴影?

ios - 在没有 MFMessageComposeViewController 的情况下发送 imessages

ios - 当模型在 @Published 属性内更新时,SwiftUI 触发函数

ios - 理解MongoDB最简单的方法iphone

ios - 按下滑动删除按钮时应用程序崩溃

ios - 如何将已编程的按钮声明为 var

ios - 通过在嵌入式容器中的 Collection View 中选择一个单元格来更改 View Controller 中的值/图像

ios - 使用 AVPlayerViewController 设置视频的大小

ios - 设置最大行可编辑 TextView - Swift/iOS