Swift3仿iPhone小白点吸附控件

支持拖拽,点击,静止一段时间会有一定透明度,GitHub地址:https://github.com/developerZGJ/ZGSuctionControl

效果图

像小白点这种控件一般来说都需要全局使用,因此我将这个对象创建成一个单例,方便调用并且保证该对象唯一。我这里的控件继承自UIView,给其添加了点击手势和拖拽手势。在拖拽和点击后让其不具有透明度,拖拽完成或者点击后隔4秒会具有透明度。为了防止频繁拖动或点击造成透明度频繁改变,我设置了canChangeAlpha变量进行判断。控件很简单,较为核心的就是判断控件的位置应该在哪里。
拖拽手势响应方法如下:

func handlePanGesture(pan:UIPanGestureRecognizer){
        if pan.view == nil{
            return
        }
        switch pan.state {
            
        case .began: self.alpha = 1
        
        case .changed:
            let translation = pan.translation(in: self.superview)
            pan.view?.center = CGPoint(x: lastX + translation.x, y: lastY + translation.y)
        case .ended:
            var stopPoint = CGPoint.init(x: 0, y: kScreenH / 2)
    //可以采用三目运算嵌套,但是影响阅读性,因此还是用if else
            if (pan.view!.center.x < kScreenW / 2.0) {
                if (pan.view!.center.y <= kScreenH/2.0) {
                    //左上
                    stopPoint = pan.view!.center.x  >= pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: btnWidth/2.0) : CGPoint(x: btnWidth/2.0,y: pan.view!.center.y)
                }else{
                    //左下
                    stopPoint = pan.view!.center.x  >= kScreenH - pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: kScreenH - btnWidth/2.0) : CGPoint(x:btnWidth/2.0,y: pan.view!.center.y)
                }
            }else{
                if (pan.view!.center.y <= kScreenH/2.0) {
                    //右上
                    stopPoint = kScreenW - pan.view!.center.x  >= pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: btnWidth/2.0) : CGPoint(x: kScreenW - btnWidth/2.0,y: pan.view!.center.y)
                }else{
                    //右下
                    stopPoint = kScreenW - pan.view!.center.x  >= kScreenH - pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: kScreenH - btnWidth/2.0) : CGPoint(x: kScreenW - btnWidth/2.0,y: pan.view!.center.y)
                }
            }
            
            if (stopPoint.x - btnWidth/2.0 <= 0) {
                stopPoint = CGPoint(x: btnWidth/2.0,y: stopPoint.y);
            }
            
            if (stopPoint.x + btnWidth/2.0 >= kScreenW) {
                stopPoint = CGPoint(x: kScreenW - btnWidth/2.0,y: stopPoint.y);
            }
            
            if (stopPoint.y - btnWidth/2.0 <= 0) {
                stopPoint = CGPoint(x: stopPoint.x,y: btnWidth/2.0);
            }
            
            if (stopPoint.y + btnWidth/2.0 >= kScreenH) {
                stopPoint = CGPoint(x: stopPoint.x,y: kScreenH - btnWidth/2.0);
            }
            
            lastX = stopPoint.x
            lastY = stopPoint.y
            
            weak var weakSelf = self
            UIView.animate(withDuration: 0.3, animations: { 
                pan.view!.center = stopPoint
            }, completion: { (finish) in
                weakSelf?.changeAlpha()
            })
            
        default:
            break
        }
    }

其中lastX和lastY是保存控件最终的中心位置坐标,初始值为控件初始位置的中心坐标。
封装后的完整代码如下,使用了Snapkit,view.addSubview(CustomerServiceView.shared)或者window?.addSubview(CustomerServiceView.shared)进行调用


import UIKit
import SnapKit

class CustomerServiceView: UIView {
    
    let kScreenH = UIScreen.main.bounds.height
    let kScreenW = UIScreen.main.bounds.width
    
    public static let shared = CustomerServiceView()
    
    fileprivate var canChangeAlpha = true
    
    let btnWidth = 50.fitScreenWidth()
    let btnHeight = 50.fitScreenHeight()
    
    fileprivate var lastX: CGFloat = 0
    fileprivate var lastY: CGFloat = 0
    
    private override init(frame: CGRect) {
        
        
        let x = UIScreen.main.bounds.width - btnWidth - 5
        let y = UIScreen.main.bounds.height / 2 + 20.fitScreenHeight()
        super.init(frame: CGRect(x: x, y: y, width: btnWidth, height: btnHeight))
        let backImageView = UIImageView()
        addSubview(backImageView)
        //        backImageView.image = UIImage(named: "111")
        backImageView.backgroundColor = UIColor.red
        
        backImageView.snp.remakeConstraints { (make) in
            make.top.right.left.bottom.equalTo(0)
        }
        lastX = center.x
        lastY = center.y
        let panRcognize = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(pan:)))
        self.addGestureRecognizer(panRcognize)
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTapGesture(tap:)))
        self.addGestureRecognizer(tap)
        self.alpha = 0.5
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func handleTapGesture(tap: UITapGestureRecognizer?){
        
        self.alpha = 1
        print("被点击")
        //点击响应事件之后需要调用changeAlpha改变透明度
    }
    
    func changeAlpha(){
        guard canChangeAlpha else {
            return
        }
        weak var weakSelf = self
        weakSelf?.canChangeAlpha = false
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3, execute: {
            UIView.animate(withDuration: 1, animations: {
                weakSelf?.alpha = 0.6
                weakSelf?.canChangeAlpha = true
            })
        })
    }
    
    func handlePanGesture(pan:UIPanGestureRecognizer){
        if pan.view == nil{
            return
        }
        switch pan.state {
            
        case .began: self.alpha = 1
            
        case .changed:
            let translation = pan.translation(in: self.superview)
            pan.view?.center = CGPoint(x: lastX + translation.x, y: lastY + translation.y)
        case .ended:
            var stopPoint = CGPoint.init(x: 0, y: kScreenH / 2)
            if (pan.view!.center.x < kScreenW / 2.0) {
                if (pan.view!.center.y <= kScreenH/2.0) {
                    //左上
                    stopPoint = pan.view!.center.x  >= pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: btnWidth/2.0) : CGPoint(x: btnWidth/2.0,y: pan.view!.center.y)
                }else{
                    //左下
                    stopPoint = pan.view!.center.x  >= kScreenH - pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: kScreenH - btnWidth/2.0) : CGPoint(x:btnWidth/2.0,y: pan.view!.center.y)
                }
            }else{
                if (pan.view!.center.y <= kScreenH/2.0) {
                    //右上
                    stopPoint = kScreenW - pan.view!.center.x  >= pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: btnWidth/2.0) : CGPoint(x: kScreenW - btnWidth/2.0,y: pan.view!.center.y)
                }else{
                    //右下
                    stopPoint = kScreenW - pan.view!.center.x  >= kScreenH - pan.view!.center.y ? CGPoint(x: pan.view!.center.x,y: kScreenH - btnWidth/2.0) : CGPoint(x: kScreenW - btnWidth/2.0,y: pan.view!.center.y)
                }
            }
            
            if (stopPoint.x - btnWidth/2.0 <= 0) {
                stopPoint = CGPoint(x: btnWidth/2.0,y: stopPoint.y)
            }
            
            if (stopPoint.x + btnWidth/2.0 >= kScreenW) {
                stopPoint = CGPoint(x: kScreenW - btnWidth/2.0,y: stopPoint.y)
            }
            
            if (stopPoint.y - btnWidth/2.0 <= 0) {
                stopPoint = CGPoint(x: stopPoint.x,y: btnWidth/2.0)
            }
            
            if (stopPoint.y + btnWidth/2.0 >= kScreenH) {
                stopPoint = CGPoint(x: stopPoint.x,y: kScreenH - btnWidth/2.0)
            }
            
            lastX = stopPoint.x
            lastY = stopPoint.y
            
            weak var weakSelf = self
            UIView.animate(withDuration: 0.3, animations: {
                pan.view!.center = stopPoint
            }, completion: { (finish) in
                weakSelf?.changeAlpha()
            })
            
        default:
            break
        }
    }
    
}

extension Int {
    
    func fitScreenHeight() -> CGFloat {
        let screenHeight = UIScreen.main.bounds.height - 64;
        let height : CGFloat = 667.0 - 64;
        let scale = screenHeight / height ;
        return scale * CGFloat(self);
    }
    
    func fitScreenWidth() -> CGFloat {
        let screenWidth = UIScreen.main.bounds.width;
        let width : CGFloat = 375;
        let scale = screenWidth / width ;
        return scale * CGFloat(self);
    }
    
}

extension CGFloat {
    
    func fitScreenHeight() -> CGFloat {
        let screenHeight = UIScreen.main.bounds.height - 64;
        let height : CGFloat = 667.0 - 64;
        let scale = screenHeight / height ;
        return scale * CGFloat(self);
    }
    
    func fitScreenWidth() -> CGFloat {
        let screenWidth = UIScreen.main.bounds.height;
        let width : CGFloat = 375;
        let scale = screenWidth / width ;
        return scale * CGFloat(self);
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 买了两年的效率手册,一直都没能充分利用起来。不知道该怎么记,记什么。所以一直随手写。之前也在印象笔记上记录一些。今...
    遁格阅读 181评论 0 0
  • 我邀请您读父母规100天,每天读一遍,读后发:“已读父母规,百日行动第8天!” 知道不等于做到,重复100天,内化...
    二丫_2747阅读 210评论 0 0
  • 文:酷啡生活 1 20几岁的时候为了房子这件事情跟姐姐吵过n多次。 曾经的我,觉得买房等于放弃梦想,一直坚持不买房...
    海兰生涯阅读 564评论 4 6