仿写ofo之下拉菜单效果

在项目不忙之余,心血来潮突然想仿写一个ofo,本人会把项目中写的控件及技术点罗列出来,与同僚共同学习的同时,也是对自己学习的总结。

本章节仅实现ofo底部扫码视图弹出效果。


效果图

贝塞尔曲线画圆角

首先底部视图上的弧度是通过Quart2D实现,首先在View引出drawRect方法。
drawRect方法提供UIView的重绘机制,可以在此方法中进行绘图,因此类教程颇多具体就不细说了。
上代码:

override func draw(_ rect: CGRect) {
    let ctx = UIGraphicsGetCurrentContext()
    // 此为移动到的位置
    ctx?.move(to: CGPoint(x: SCREEN_W, y: 50))
    // 添加贝塞尔曲线 control 参数表弧线坐标点
    ctx?.addQuadCurve(to: CGPoint(x: 0, y: 50), control: CGPoint(x: SCREEN_W/2, y: -33))
    // 从左下角至移动点画一条线
    ctx?.addLine(to: CGPoint(x: 0, y: self.height))
    // 从右下角至移动点画一条线
    ctx?.addLine(to: CGPoint(x: SCREEN_W, y: self.height))
    // 填充背景为白色
    ctx?.setFillColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1))
    // 设置线条阴影
    ctx?.setShadow(offset: CGSize(width: 0, height: 0), blur: 0.8, color: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))
    // 此处fill表示仅填充
    ctx?.drawPath(using: .fill)
}

拖动手势

此处功能很简单,在底部UIView上创建上划及下滑手势,因打开APP默认底部视图是展开所以此处设置topRecognizer.isEnabled默认为false

// MARK: - 上划手势
lazy var topRecognizer: UISwipeGestureRecognizer = {
    let topRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeFrom(recognizer:)))
    topRecognizer.isEnabled = false
    topRecognizer.direction = .up
    return topRecognizer
}()

// MARK: - 下划手势
lazy var downRecognizer: UISwipeGestureRecognizer = {
    let downRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeFrom(recognizer:)))
    downRecognizer.direction = .down
    return downRecognizer
}()

通过判断滑动手势来处理上划还是下划,并且与点击or方法公用

// MARK: - 手势滑动方法
@objc func handleSwipeFrom(recognizer: UISwipeGestureRecognizer) {
    if recognizer.direction == .up {
        changeArrow()
    } else if recognizer.direction == .down {
        changeArrow()
    }
}

// MARK: - 视图打开or回收
private func changeArrow() {
    if isHiddenBottomView == false {
        arrowButton.setImage(#imageLiteral(resourceName: "arrowup"), for: .normal)
    } else {
        arrowButton.setImage(#imageLiteral(resourceName: "arrowdown"), for: .normal)
    }
    topRecognizer.isEnabled = !isHiddenBottomView
    downRecognizer.isEnabled = isHiddenBottomView
    isHiddenBottomView = !isHiddenBottomView
    delegate?.didSelectedArrowButton(isHiddenBottomView: isHiddenBottomView)
}

didSelectedArrowButton代理方法为返回到背景时控制View约束的回调

// didSelectedArrowButton代理方法,控制点击`∧`or`∨` and 上划下滑
func didSelectedArrowButton(isHiddenBottomView: Bool) {
    updateBottomViewWithExpand(isExpanded: isHiddenBottomView, animated: true)
}

// 此方法控制底部视图约束
private func updateBottomViewWithExpand(isExpanded:Bool = false, animated:Bool = false, duration:TimeInterval = 0.25) {
    bottomView.snp.updateConstraints { (make) in
        make.left.right.equalTo(self)
        make.height.equalTo(280)
        if isExpanded == true {
            make.bottom.equalTo(snp.bottom).offset(210)
        } else {
            make.bottom.equalTo(snp.bottom)
        }
    }
    
    if animated == true {
        self.needsUpdateConstraints()
        self.updateConstraintsIfNeeded()
        
        UIView.animate(withDuration: duration, animations: {
            self.layoutIfNeeded()
        }, completion: { (completion) in
            self.bottomView.updateWithExpand(isExpanded: isExpanded, animated: true, duration: 0.2)
        })
    }
}

视图上划后扫码等按钮上移动画

updateBottomViewWithExpand方法中,在动画结束后引出bottomView(底部视图)updateWithExpand来更新bottomView(底部视图)的约束,从而实现扫码等按钮跟随底部视图上移而上移的动画效果。

open func updateWithExpand(isExpanded:Bool = false, animated:Bool = false, duration:TimeInterval = 0.25) {
    startButton.snp.updateConstraints { (make) in
        make.centerX.equalTo(self)
        make.width.equalTo(snp.width).multipliedBy(0.55)
        make.height.equalTo(startButton.snp.width)
        if isExpanded {
            make.top.equalTo(arrowButton.snp.bottom).offset(30)
        } else {
            make.top.equalTo(arrowButton.snp.bottom).offset(15)
        }
    }
    
    loginButton.snp.updateConstraints { (make) in
        make.left.equalTo(snp.left).offset(20)
        make.width.equalTo(30)
        make.height.equalTo(loginButton.snp.width)
        
        if isExpanded {
            make.bottom.equalTo(snp.bottom).offset(30)
        } else {
            make.bottom.equalTo(snp.bottom).offset(-15)
        }
    }
    
    newsButton.snp.updateConstraints { (make) in
        make.right.equalTo(snp.right).offset(-20)
        make.width.equalTo(30)
        make.height.equalTo(newsButton.snp.width)
        
        if isExpanded {
            make.bottom.equalTo(snp.bottom).offset(15)
        } else {
            make.bottom.equalTo(snp.bottom).offset(-15)
        }
    }
    
    if animated == true {
        self.needsUpdateConstraints()
        self.updateConstraintsIfNeeded()
        UIView.animate(withDuration: duration, animations: {
            self.layoutIfNeeded()
        })
    }
}

完整代码后续会进行上传~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,533评论 4 61
  • 学识有何用处,也许正如我们曾学过,明了知识的存在性之后,便更有可能活得敞亮,而非计较。也明白不是所有的知识都有用武...
    摄影师柳丁阅读 442评论 0 2
  • 接着昨天的说,今天的行情熄火了,跟今天是周五也有一定的关系,创业板还是不行, 今天开盘半小时我就知道今天可以好好上...
    有财不楠阅读 1,196评论 0 0
  • 有路不代表有方向 畢竟別人的開拓 僅供參考 卻不一定適合當下
    憨憨爹阅读 146评论 0 0
  • 看了今天的晨读材料,刻入脑子的是第二个故事。介绍自己的缺点,反而更能赢得信任! 敢于介绍自己的缺点...
    一缕阳光v阅读 966评论 0 2

友情链接更多精彩内容