var leftView: UIView? var minX: CGFloat? var midX: CGFloat? var maxX:CGFloat? /* 抽屉 */ func drawerView(){ self.view.isMultipleTouchEnabled = true self.view.isUserInteractionEnabled = true self.view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.pan(_:)))) let make = CGRect(x: -UIScreen.main.bounds.width*3/5, y: 0, width: UIScreen.main.bounds.width*3/5, height: UIScreen.main.bounds.height) leftView = UIView(frame: make) leftView?.backgroundColor = UIColor.brown self.view.addSubview(leftView!) minX = leftView?.center.x maxX = minX! + make.width midX = (maxX!-minX!)/2 + minX! }func pan(_ pan: UIPanGestureRecognizer){ switch pan.state{ case UIGestureRecognizerState.began: PRint("----began----") case UIGestureRecognizerState.changed: print("----Changed----") let tran = pan.translation(in: self.view) let newC = (leftView?.center.x)! + tran.x if newC >= minX && newC <= maxX{ leftView?.center = CGPoint(x: newC, y: (leftView?.center.y)!) } pan.setTranslation(CGPoint.zero, in: self.view) case UIGestureRecognizerState.ended: UIView.animate(withDuration: 0.2, animations: { () -> Void in if self.leftView!.center.x > self.minX && self.leftView!.center.x < self.midX{ self.leftView!.center = CGPoint(x: self.minX!, y: self.leftView!.center.y) }else if self.leftView!.center.x >= self.midX && self.leftView!.center.x < self.maxX{ self.leftView!.center = CGPoint(x: self.maxX!, y: self.leftView!.center.y) } }, completion: { (finish) -> Void in }) default: break } }
新闻热点
疑难解答