UIView changing its position in swift

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

提交回复
热议问题