Swift
发布时间:2022-03-31 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了用 Swift 制作一个漂亮的汉堡按钮过渡动画,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
原文:How to build a nice Hamburger Button transition in Swift 汉堡按钮在界面设计中已经是老生常谈了,但是当我在dribbble看到这个 漂亮的过渡动画时,我决定试试用代码实现它。 这是 CreativeDash team 的原型图: 你可能已经注意到了,汉堡顶部和底部的线条是来自’X’字符,中间的线条变为外框。我知道这种效果可以用CA
原文:How to build a nice Hamburger Button transition in Swift
汉堡按钮在界面设计中已经是老生常谈了,但是当我在dribbble看到这个
漂亮的过渡动画时,我决定试试用
代码实现它。
你可能已经注意到了,汉堡顶部和
底部的线条是来自’X’字符,中@L_
607_5@线条变为外框。我知道这种@L_
674_6@可以用CAShapeLayer创建出来,但是首先我得为三条线分别创建
一个CGPath。
这种短直线可以手写出来:
- letshortstroke:CGPath={
@H_618_36@letpath=CGPathCreateMutable()
- CGPathMoveToPoint(path,nil,2,2)
@H_618_36@CGPathAddLineToPoint(path,28,2)
@H_618_36@returnpath
- }()
对于中@L_
607_5@线条,我用Sketch创建了path,从中间开始,然后无缝地过渡到圆形的外框。
接下来我导出这个 path 为 SVG
文件,然后将它导入到旧的 PaintCode1 中,
它将转换
文件为 UIBezierPath
代码片段。然后我按照说明改写了部分
代码,创建了
一个使用 CGPath 描述的对象:
letoutline:CGPath={
@H_
618_36@
letpath=CGPathCreateMutable()
CGPathMoveToPoint(path,10,27)
@H_
618_36@
CGPathAddCurveToPoint(path,12.00,27.00,28.02,40,27)
CGPathAddCurveToPoint(path,55.92,50.47,2.00,27,2)
@H_
618_36@
CGPathAddCurveToPoint(path,13.16,40.84,52.00,52)
@H_
618_36@
CGPathAddCurveToPoint(path,52,42.39,27)
@H_
618_36@
可能有第三方库能够帮助你从 SVG 文件直接加载为 CGPaths,但@R_944_9811@ path 我们这里只有这一个,所以用代码写也无所谓啦。
在我的 UIButton 子类中,我
添加了三个 CAShapeLayer
属性,并设置它们为
相应的 path:
self.top.path=shortstroke
@H_
618_36@
self.middle.path=outline
self.bottom.path=shortstroke
然后我设置了它们三个的样式:
layer.fillColor=nil
@H_
618_36@
layer.strokeColor=UIColor.whiteColor().CGColor
layer.lineWidth=4
@H_
618_36@
layer.miterLimit=4
layer.lineCap=kCALineCapRound
@H_
618_36@
layer.masksToBounds=true
为了正确地计算出它们的边界,我需要将画笔的大小
考虑在内。幸运的是,CGPathCreateCopyByStrokingPath 将创建沿用了原先的画笔的轮廓路径,因此它的边框会完全包含 CAShapeLayer 的
内容:
letboundingPath=CGPathCreateCopyByStrokingPath(layer.path,4,kCGLineCapRound,kCGLineJoinMiter,4)
@H_
618_36@
layer.bounds=CGPathGetPathBoundingBox(boundingPath)
由于外框将在稍后围绕其最右边的点旋转,我只好在 layers 布局时设置
相应的 anchorPoint:
self.top.anchorPoint=CGPointMake(28.0/30.0,0.5)
@H_
618_36@
self.top.position=CGPointMake(40,18)
@H_
618_36@
self.middle.position=CGPointMake(27,27)
self.middle.strokeStart=hamburgerstrokeStart
@H_
618_36@
self.middle.strokeEnd=hamburgerstrokeEnd
@H_
618_36@
self.bottom.anchorPoint=CGPointMake(28.0/30.0,0.5)
self.bottom.position=CGPointMake(40,36)
现在,当按钮改变状态时,它应当
显示三条线到新位置的动画。接下来的两个外笔划非常简单。对于顶部的线条,我首先将它向内移动4个points,以保持在中心,然后旋转至负45度,形成半个X:
vartransform=CATransform3DIdentity
@H_
618_36@
transform=CATransform3DTranslate(transform,-4,0)
transform=CATransform3DRotate(transform,-M_PI_4,1)
@H_
618_36@
letanimation=CABasicAnimation(keyPath:"transform")
@H_
618_36@
animation.fromValue=NSValue(CATransform3D:CATransform3DIdentity)
animation.toValue=NSValue(CATransform3D:transform)
@H_
618_36@
animation.timingFunction=CAMediaTimingFunction(controlPoints:0.25,-0.8,0.75,1.85)
animation.beginTime=CACurrentMediaTime()+0.25
@H_
618_36@
self.top.addAnimation(animation,forKey:"transform")
@H_
618_36@
self.top.transform=transform
首先,我必须弄清楚在两种状态下的这两个
属性正确的值。注意,即使是汉堡包状态下,笔画也不是从0开始。由现有路径延伸略微超过外笔划的左边缘,当应用到 timingFunction 后,我们就完美实现了预期的@L_
674_6@:
letmenustrokeStart:CGFloat=0.325
@H_
618_36@
letmenustrokeEnd:CGFloat=0.9
@H_
618_36@
lethamburgerstrokeStart:CGFloat=0.028
lethamburgerstrokeEnd:CGFloat=0.111
现在我们只需要创建动画,然后
添加它们到 layer 中:
letstrokeStart=CABasicAnimation(keyPath:"strokeStart")
@H_
618_36@
strokeStart.fromValue=hamburgerstrokeStart
strokeStart.toValue=menustrokeStart
@H_
618_36@
strokeStart.duration=0.5
strokeStart.timingFunction=CAMediaTimingFunction(controlPoints:0.25,-0.4,0.5,1)
@H_
618_36@
self.middle.addAnimation(strokeStart,forKey:"strokeStart")
@H_
618_36@
self.middle.strokeStart=menustrokeStart
@H_
618_36@
letstrokeEnd=CABasicAnimation(keyPath:"strokeEnd")
strokeEnd.fromValue=hamburgerstrokeEnd
@H_
618_36@
strokeEnd.toValue=menustrokeEnd
strokeEnd.duration=0.6
@H_
618_36@
strokeEnd.timingFunction=CAMediaTimingFunction(controlPoints:0.25,1)
@H_
618_36@
self.middle.addAnimation(strokeEnd,forKey:"strokeEnd")
self.middle.strokeEnd=menustrokeEnd
大佬总结
以上是大佬教程为你收集整理的用 Swift 制作一个漂亮的汉堡按钮过渡动画全部内容,希望文章能够帮你解决用 Swift 制作一个漂亮的汉堡按钮过渡动画所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。