如何在Xcode中制作滑动门动画

时间:2017-07-11 18:02:44

标签: ios swift xcode animation

我对Xcode很新,所以这可能是一个非常基本的东西,我只是没有找到,但我正在尝试创建一个应用程序,相互移动2个图像(至少一个宽度)当用户再次点击屏幕时,用户点击屏幕,然后让图像返回到原始位置,但是,我无法找到如何在特定方向,特定距离上移动图像。

我也是Stack Overflow的新手,所以如果我做错了,我很抱歉

1 个答案:

答案 0 :(得分:0)

当你看来还没有足够的Google搜索时,我就放弃了这个规则。一般规则是显示您尝试过的代码并指出您遇到问题的部分。

以下代码将实现您的尝试。

import UIKit

class slidingIamgesViewController: UIViewController {

    @IBOutlet weak var topImage: UIImageView!
    @IBOutlet weak var bottomImage: UIImageView!
    var doubleTap: Bool! = false

    //MARK: View LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
        let singleFingerTap = UITapGestureRecognizer(target: self, action: #selector(slidingIamgesViewController.handleSingleTap(_:)))
        self.view.addGestureRecognizer(singleFingerTap)
    }


    // MARK: gestutre recognizer
    func handleSingleTap(_ recognizer: UITapGestureRecognizer) {
        if (doubleTap) {
        UIView.animate(withDuration: 0.7, delay: 1.0, options: .curveEaseOut, animations: {
            var basketTopFrame = self.topImage.frame
            basketTopFrame.origin.y += basketTopFrame.size.height

            var basketBottomFrame = self.bottomImage.frame
            basketBottomFrame.origin.y -= basketBottomFrame.size.height

            self.topImage.frame = basketTopFrame
            self.bottomImage.frame = basketBottomFrame
        }, completion: { finished in
            print("Images Moved back!")
        })
            doubleTap = false
        } else {
            UIView.animate(withDuration: 0.7, delay: 1.0, options: .curveEaseOut, animations: {
                var basketTopFrame = self.topImage.frame
                basketTopFrame.origin.y -= basketTopFrame.size.height

                var basketBottomFrame = self.bottomImage.frame
                basketBottomFrame.origin.y += basketBottomFrame.size.height

                self.topImage.frame = basketTopFrame
                self.bottomImage.frame = basketBottomFrame
            }, completion: { finished in
                print("Images sperated!")
            })
            doubleTap = true
        }
    }

}

确保在故事板中添加了Tap Gesture Recognizer。