How to move the uiview from left to right and vice versa

前端 未结 5 1075
自闭症患者
自闭症患者 2021-01-01 05:43

Hi I am developing one application.In that i did the animation for one view to move from left to right and right to left and changing the values for labels contained in that

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 05:58

    Using Swift 4.2 and Swift 5

    Animate UIView from left to right or vice versa. 0 for Left to right and 1 for Right to left

    
    class AnimationView: UIView {
        enum Direction: Int {
            case FromLeft = 0
            case FromRight = 1
        }
    
        @IBInspectable var direction : Int = 0
        @IBInspectable var delay :Double = 0.0
        @IBInspectable var duration :Double = 0.0
        override func layoutSubviews() {
            initialSetup()
            UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.2, options: .curveEaseIn, animations: {
                if let superview = self.superview {
                               if self.direction == Direction.FromLeft.rawValue {
                                   self.center.x += superview.bounds.width
                               } else {
                                   self.center.x -= superview.bounds.width
                               }
                           }
            })
        }
        func initialSetup() {
            if let superview = self.superview {
                if direction == Direction.FromLeft.rawValue {
                 self.center.x -= superview.bounds.width
                } else {
                    self.center.x += superview.bounds.width
                }
    
            }
        }
    }
    
    

提交回复
热议问题