Swift 模仿 QQ tabbarItem 的未读消息效果

2016-03-02  本文已影响270人  Tpphha

先看效果

借鉴了这篇博客
原博客使用的 OC 写的
在其基础上增加了一些功能, 比如能随着移动, size 也随着改变

animate.gif

如何实现

中间路径实现代码

//MARK: - 中间图形路径
    private func path() -> UIBezierPath {
        let center1 = smallCircleView!.center
        let r1 = smallCircleView!.layer.cornerRadius
        let center2 = self.center
        let r2 = self.layer.cornerRadius
        let d = pointToPointDistanceWithPoint1(center1, point2: center2)
        let x1 = center1.x
        let x2 = center2.x
        let y1 = center1.y
        let y2 = center2.y
        let cosθ = (y2 - y1) / d
        let sinθ = (x2 - x1) / d
        
        let A = CGPointMake(x1 - r1 * cosθ, y1 + r1 * sinθ)
        let B = CGPointMake(x1 + r1 * cosθ, y1 - r1 * sinθ)
        let C = CGPointMake(x2 + r2 * cosθ, y2 - r2 * sinθ)
        let D = CGPointMake(x2 - r2 * cosθ, y2 + r2 * sinθ)
        let O = CGPointMake(A.x + d / 2 * sinθ, A.y + d / 2 * cosθ)
        let P = CGPointMake(B.x + d / 2 * sinθ, B.y + d / 2 * cosθ)
        
        let path = UIBezierPath()
        path.moveToPoint(A)
        path.addLineToPoint(B)
        path.addQuadCurveToPoint(C, controlPoint: P)
        path.addLineToPoint(D)
        path.addQuadCurveToPoint(A, controlPoint: O)
        
        return path
    }

//MARK: - 两点之间距离
    private func pointToPointDistanceWithPoint1(point1: CGPoint, point2: CGPoint) ->CGFloat {
        let xDistance = point2.x - point1.x
        let yDistance = point2.y - point1.y
        return sqrt(xDistance * xDistance + yDistance * yDistance)
    }

demo 地址

请点击这里

上一篇下一篇

猜你喜欢

热点阅读