ios UIView 迅速改变其位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25694388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:15:00  来源:igfitidea点击:

UIView changing its position in swift

iosswiftxcode6

提问by Joseandy10

How do I make a UIViewslide up with a touch of a button from its original position and them bring it back down with a touch of a button? Using Swift and Xcode 6.

我如何UIView从原始位置轻触按钮向上滑动,然后轻触按钮将其放回原处?使用 Swift 和 Xcode 6。

I have currently tried this:

我目前已经尝试过这个:

@IBOutlet weak var DynView: UIView!

@IBAction func btnUp(sender: AnyObject) {

}

回答by Thiago Magalhaes

You have to implement an animation changing the DynViewposition on click. Here's an example:

您必须实现一个动画来改变DynView点击位置。下面是一个例子:

@IBAction func btnUp(sender: AnyObject) {
    let xPosition = DynView.frame.origin.x
    let yPosition = DynView.frame.origin.y - 20 // Slide Up - 20px

    let width = DynView.frame.size.width
    let height = DynView.frame.size.height

    UIView.animateWithDuration(1.0, animations: {
        dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
    })
}

回答by YannSteph

Hi create this extends if you want. For Swift

嗨,如果你愿意,可以创建这个扩展。对于斯威夫特

Create File Extends.Swiftand addthis code

创建 File Extends.Swift添加此代码

/**
    Extension UIView
    by DaRk-_-D0G
*/
extension UIView {
    /**
    Set x Position

    :param: x CGFloat
    by DaRk-_-D0G
    */
    func setX(#x:CGFloat) {
        var frame:CGRect = self.frame
        frame.origin.x = x
        self.frame = frame
    }
    /**
    Set y Position

    :param: y CGFloat
    by DaRk-_-D0G
    */
    func setY(#y:CGFloat) {
        var frame:CGRect = self.frame
        frame.origin.y = y
        self.frame = frame
    }
    /**
    Set Width

    :param: width CGFloat
    by DaRk-_-D0G
    */
    func setWidth(#width:CGFloat) {
        var frame:CGRect = self.frame
        frame.size.width = width
        self.frame = frame
    }
    /**
    Set Height

    :param: height CGFloat
    by DaRk-_-D0G
    */
    func setHeight(#height:CGFloat) {
        var frame:CGRect = self.frame
        frame.size.height = height
        self.frame = frame
    }
}

For Use (inherits Of UIView)

供使用(UIView 的继承)

inheritsOfUIView.setX(x: 100)
button.setX(x: 100)
view.setY(y: 100)

回答by JP Aquino

I kinda combined the two most voted answers into one and updated to Swift 3. So basically created an extension that animates a view moving to a different position:

我有点将两个投票最多的答案合并为一个并更新到 Swift 3。 所以基本上创建了一个扩展,动画移动到不同位置的视图:

extension UIView {

    func slideX(x:CGFloat) {

        let yPosition = self.frame.origin.y

        let height = self.frame.height
        let width = self.frame.width

        UIView.animate(withDuration: 1.0, animations: {

            self.frame = CGRect(x: x, y: yPosition, width: width, height: height)

        })
    }
}

回答by iAleksandr

 // MARK: - Properties

var bottomViewHeight: CGFloat = 200
var isViewHide = false

private let bottomView: UIView = {
    let view = UIView()
    view.backgroundColor = .red
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

private let showHideButton: UIButton = {
    let button = UIButton()
    button.setTitle("Show / Hide", for: .normal)
    button.setTitleColor(.black, for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.addTarget(self, action: #selector(showHideButtonTapped(_:)), for: .touchUpInside)
    return button
}()

// MARK: - Lifecycle

override func loadView() {
    super.loadView()

    view.addSubview(bottomView)

    NSLayoutConstraint.activate([
        bottomView.heightAnchor.constraint(equalToConstant: bottomViewHeight),
        bottomView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        bottomView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        bottomView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
    ])

    view.addSubview(showHideButton)

    NSLayoutConstraint.activate([
        showHideButton.widthAnchor.constraint(equalToConstant: 200),
        showHideButton.heightAnchor.constraint(equalToConstant: 50),
        showHideButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
        showHideButton.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor)
    ])

}

override func viewDidLoad() {
    super.viewDidLoad()

    showHideView(isShow: isViewHide)

}

// MARK: - Selectors

@objc func showHideButtonTapped(_ sender: UIButton) {
    print(" HIDE / SHOW BUTTON")

    showHideView(isShow: isViewHide)

}

// MARK: - Functions

private func showHideView(isShow: Bool) {
    if isShow {
        UIView.animate(withDuration: 0.4) {
            self.bottomView.transform = CGAffineTransform(translationX: 0, y: self.bottomViewHeight)
        }
    } else {
        UIView.animate(withDuration: 0.4) {
            self.bottomView.transform = CGAffineTransform(translationX: 0, y: 0)
        }
    }

    isViewHide = !isViewHide

}