UIView changing its position in swift

淺唱寂寞╮ 提交于 2020-02-26 05:53:18

问题


How do I make a UIView slide 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.

I have currently tried this:

@IBOutlet weak var DynView: UIView!

@IBAction func btnUp(sender: AnyObject) {

}

回答1:


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

@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)
    })
}



回答2:


Hi create this extends if you want. For Swift

Create File Extends.Swift and add this code

/**
    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)

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



回答3:


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:

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)

        })
    }
}



回答4:


 // 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

}


来源:https://stackoverflow.com/questions/25694388/uiview-changing-its-position-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!