博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发下拉导航栏果冻效果
阅读量:6447 次
发布时间:2019-06-23

本文共 3454 字,大约阅读时间需要 11 分钟。

在简书上看到别人写的感觉效果很棒,抽时间进行了代码仿写,精简了一些东西添加了少许自己的注释,记录下来以便日后回忆,也希望对大家有所帮助。

#define MIN_HEIGHT 100@interface ViewController ()@property(strong,readwrite,nonatomic)CAShapeLayer *shapeLayer;//形变视图@property(assign,readwrite,nonatomic)float curveX;//拖动红点x坐标@property(assign,readwrite,nonatomic)float curveY;//拖动红点y坐标@property(strong,readwrite,nonatomic)UIView *curveView;//拖动红点@property(assign,readwrite,nonatomic)float mHeight;//手势移动相对高度@property(strong,readwrite,nonatomic)CADisplayLink *displayLink;@end复制代码
@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];       //初始化形变视图    _shapeLayer=[CAShapeLayer layer];    _shapeLayer.fillColor=[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1].CGColor;    [self.view.layer addSublayer:_shapeLayer];            //初始化拖动点    _curveX=self.view.frame.size.width/2;    _curveY=MIN_HEIGHT;    _curveView=[[UIView alloc]initWithFrame:CGRectMake(_curveX, _curveY, 3, 3)];    _curveView.backgroundColor=[UIColor redColor];    [self.view addSubview:_curveView];            _mHeight=100;    //添加手势    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];    self.view.userInteractionEnabled=YES;    [self.view addGestureRecognizer:pan];        //开启循环    _displayLink=[CADisplayLink displayLinkWithTarget:self selector:@selector(calculatePath)];    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];    _displayLink.paused=YES;        [self updateShapeLayerPath];}复制代码
-(void)panGestureAction:(UIPanGestureRecognizer *)pan{    if (pan.state==UIGestureRecognizerStateChanged) {                CGPoint point=[pan translationInView:self.view];                _mHeight=point.y*0.7+MIN_HEIGHT;//乘0.7可以使拖动效果更好        _curveX=self.view.frame.size.width/2+point.x;        _curveY=_mHeight>MIN_HEIGHT?_mHeight:MIN_HEIGHT;        _curveView.frame=CGRectMake(_curveX, _curveY, 3, 3);                [self updateShapeLayerPath];            }    else if (pan.state==UIGestureRecognizerStateEnded)    {       _displayLink.paused = NO;                [UIView animateWithDuration:1.0                              delay:0.0             usingSpringWithDamping:0.5              initialSpringVelocity:1                            options:UIViewAnimationOptionCurveEaseInOut                         animations:^{                                                          _curveView.frame = CGRectMake(self.view.frame.size.width/2.0, MIN_HEIGHT, 3, 3);//拖动结束红点恢复原来位置                                                      } completion:^(BOOL finished) {                                                          if(finished)                             {                                 _displayLink.paused = YES;                             }                                                      }];                }}复制代码

拖动结束根据_curveView.frame恢复原位置帧动画,计算导航形状

-(void)calculatePath{    _curveX=_curveView.center.x;    _curveY=_curveView.center.y;    [self updateShapeLayerPath];}复制代码

更新导航栏贝塞尔曲线

-(void)updateShapeLayerPath{    UIBezierPath *path=[UIBezierPath bezierPath];    [path moveToPoint:CGPointMake(0, 0)];    [path addLineToPoint:CGPointMake(self.view.frame.size.width, 0)];    [path addLineToPoint:CGPointMake(self.view.frame.size.width, MIN_HEIGHT)];    [path addQuadCurveToPoint:CGPointMake(0, MIN_HEIGHT) controlPoint:CGPointMake(_curveX, _curveY)];    [path closePath];        _shapeLayer.path=path.CGPath;    }@end复制代码

转载地址:http://qktwo.baihongyu.com/

你可能感兴趣的文章
修正锚点跳转位置 避免头部fixed固定部分遮挡
查看>>
Dubbo序列化多个CopyOnWriteArrayList对象变成同一对象的一个大坑!!
查看>>
linux下ping不通的解决方法
查看>>
利用ItextPdf、core-renderer-R8 来生成PDF
查看>>
irc操作小记
查看>>
JAVA 与 PHP 的不同和相同
查看>>
建立Ftp站点
查看>>
NavigationController的使用
查看>>
多线程编程之Windows环境下创建新线程
查看>>
ASP.Net MVC的开发模式
查看>>
groupbox 下的datagridview的列标题字体修改混乱
查看>>
HDU-3092 Least common multiple---数论+分组背包
查看>>
CentOS 7使用systemctl如何补全服务名称
查看>>
Unity3D NGUI 给button按钮添加单间事件
查看>>
C# 使用各种API
查看>>
密码的校验.大小写字母,数字,特殊字符中的至少3种
查看>>
ios 不同sdk4.3 6.0版本号,关于方法的兼容性的通用方法
查看>>
Shell编程学习总结
查看>>
070、如何定制Calico 网络policy(2019-04-15 周一)
查看>>
构建之法阅读笔记02
查看>>