iOS cell实现侧滑功能

42 阅读1分钟
@objc func handlePan(gesture: UIPanGestureRecognizer) {

        let translation = gesture.translation(in: self)

        if translation.x < 0 {return}

        self.dayAniView.frame = CGRectMake(0, 4, translation.x, self.containerBgView.height)

      

        self.containerBgView.frame = CGRectMake(translation.x,4,self.contentView.frame.width,self.containerBgView.height)

        if gesture.state == .ended {

            self.dayAniView.frame = CGRectMake(0, 0, 0, self.contentView.height)

            self.containerBgView.frame = CGRectMake(0, 4, self.contentView.frame.width, self.containerBgView.height)

            if translation.x > 100 {

                operateDaily()

            }

        }

  


        // 使用Spring动画来提供反馈

        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {

            self.dayAniView.layoutIfNeeded()

            self.containerAnimationView.layoutIfNeeded()

        }, completion: nil)

    }

代理方法中处理与tableView的下滑手势冲突

override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

        if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {

            let velocity = panGestureRecognizer.velocity(in: self)

            return abs(velocity.x) > abs(velocity.y) // 检查水平速度是否大于垂直速度

        }

        return true

    }