UIView changing its position in swift

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)

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)

        })
    }
}

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

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

}

Tags:

Ios

Swift

Xcode6