UIView changing its position in swift

前端 未结 4 2009
难免孤独
难免孤独 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)
    
            })
        }
    }
    

提交回复
热议问题