UIView changing its position in swift

前端 未结 4 2007
难免孤独
难免孤独 2021-01-31 10:21

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.

相关标签:
4条回答
  • 2021-01-31 10:25

    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)
    
            })
        }
    }
    
    0 讨论(0)
  • 2021-01-31 10:27

    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)
        })
    }
    
    0 讨论(0)
  • 2021-01-31 10:39

    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)
    
    0 讨论(0)
  • 2021-01-31 10:45
     // 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("                                                                    
    0 讨论(0)
提交回复
热议问题