Swift4.1 仿微信浮窗

相信大家都有用过了,业余时间模仿了一下

  • 浮窗的移动、隐藏


    显示、移动、移除部分



进入页面,显示,隐藏浮窗




正文

思想:把整个功能拆分为三个部分:

  • 1、浮窗按钮: UIButton
    这个按钮为浮窗功能的核心,主要包括: 通过touch系列事件处理移动浮窗、进入二级页面、隐藏浮窗
  • 2、取消浮窗: UIView
    这个View就是用来显示交互的,构成为一个ButtonCAShapeLayer
  • 3、一个Controller 用于显示内容
    这个就是内容的显示,其中包括了:自定义转场动画、浮窗的显示/隐藏逻辑。




一、 Button 的实现

主要就是监听三个方法:touchBegan,touchesMoved,touchesEnded

  • touchesMoved
    ①:中核心操作就是把当前的点转化为在父视图中的点
        guard let touch = touches.first, let superView = self.superview else { return }
        let point = touch.location(in: superView)
 
        self.frame.origin.x = point.x - frame.width * 0.5
        self.frame.origin.y = point.y - frame.height * 0.5

②:隐藏浮窗View的显示

// show hide floating window view
        UIView.animate(withDuration: 0.2) {
            hideWindowView.frame.origin = CGPoint(x: UIScreen.main.bounds.width - hideWindowView.frame.width, y: UIScreen.main.bounds.height - hideWindowView.frame.height)
            hideWindowView.moreRedBackground(isHiddenBtn)
        }


  • touchesEnded 中核心操作
    ①:修改按钮的x坐标
        let currentPointX = frame.origin.x + frame.width * 0.5
        let screenW = UIScreen.main.bounds.width
        
        let pointX = (currentPointX > screenW * 0.5) ? (screenW - frame.width) - margin : margin



②:判断是否移动了,如果没移动就触发进入浮窗方法

        guard let touch = touches.first, let superView = superview else { return }
        
        if lastPoint == touch.location(in: superView) { //push
            let controller = UIApplication.shared.keyWindow?.rootViewController
            var nav = controller?.navigationController
            if nav == nil {
                nav = controller?.childViewControllers.first?.navigationController
            }
            
            nav?.pushViewController(fwController, animated: true)
            return
        }

③:隐藏浮窗和浮窗按钮的显示与隐藏

// show hide floating window view
        UIView.animate(withDuration: 0.2) {
            hideWindowView.frame.origin = CGPoint(x: UIScreen.main.bounds.width, y: UIScreen.main.bounds.height)
            self.isHidden = isHiddenBtn
        }




二、隐藏浮窗View的实现

①:Button 通过titleEdgeInsetsimageEdgeInsets来自定义布局

//自定义按钮
class PQButton: UIButton {
    
    convenience init(_ textLayout: EIQMenuTextLayout){
        self.init(frame: .zero)
        self.textLayout = textLayout
    }
    
    enum EIQMenuTextLayout: Int {
        case top,left,bottom,right
    }
    public var spacing: CGFloat = 20
    public var textLayout: EIQMenuTextLayout = .right
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        guard let title = self.titleLabel?.text as NSString?, let titleFont = self.titleLabel?.font else { return }
        
        let imageSize = self.imageRect(forContentRect: self.frame)
        
        let titleSize = title.size(withAttributes: [NSAttributedStringKey.font : titleFont])
        
        var titleInsets: UIEdgeInsets = self.titleEdgeInsets
        var imageInsets: UIEdgeInsets = self.imageEdgeInsets
        switch textLayout {
        case .left:
            titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0,
                                       right: -(titleSize.width * 2 + spacing))
        case .top:
            titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
        case .bottom:
            titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
        default:
            break
        }
        
        self.titleEdgeInsets = titleInsets
        self.imageEdgeInsets = imageInsets
    }
}

②:通过view.layer.mask 去实现

创建CAShapeLayer

private lazy var maskLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.frame = self.bounds
        self.layer.mask = layer
        return layer
    }()



设置path

public func moreRedBackground(_ more: Bool){
        var radius = frame.width
        if more {
            radius += 5
        }
        let path = UIBezierPath(arcCenter: CGPoint(x: frame.width + 10, y: frame.height + 10), radius: radius, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: false)
        maskLayer.path = path.cgPath
    }




三、Controller的实现

只是实现Controller就没什么好说的,这里主要说的是转场动画部分

  • 第一步:实现代理
navigationController?.delegate = self
  • 第二步:实现代理协议方法
    这个代理方法中如果返回了 nil ,即表示使用系统自带的pop、push效果
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        if operation == .push {
            return TransitionAnimator(isPush: true, animation: .overlay)
        }
        
        if operation == .pop && !fwBtnIsHidden {
            return TransitionAnimator(isPush: false, animation: .overlay)
        }
        
        return nil
    }

ps:如果想实现交互式处理需要实现另外一个方法, 在该方法中返回处理对象

@available(iOS 7.0, *)
    optional public func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

UIViewControllerAnimatedTransitioning协议介绍:

    返回动画时长
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval

   动画具体处理  containerView为内容视图,所有显示的内容均要添加进去方可显示
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning)


更多详细,请看Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_X自主阅读 15,981评论 3 119
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,104评论 4 62
  • 产品/服务备注: B2B服务,为企事业单位提供节假日员工福利发放解决方案。包括商品规划,配送服务。 通常,企事业单...
    海思大叔阅读 212评论 2 0
  • 昨天写保罗艾伦花了很长时间,写完后也不甚满意,与众多商业大佬在诡谲多变的商场上翻云覆雨相比,他的人生没有太吸引人的...
    凡尔娜阅读 204评论 0 0
  • 最近脾气变得十分糟糕,很容易就把事情扩大化,像大家相处,别人事事得以你为中心,替自己考虑,这样的要求强加于他人身上...
    pinnocchio阅读 245评论 0 0