#define SYS_DEVICE_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SYS_DEVICE_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define MIN_HEIGHT 100
@property (nonatomic,strong) CAShapeLayer *shapeLayer;
#pragma mark --果冻效果
@property (nonatomic,assign) CGFloat mHeight;
@property (nonatomic,assign) CGFloat curveX; //r5点x坐标
@property (nonatomic,assign) CGFloat curveY;
@property (nonatomic,strong) UIView *curceView;
// NSTimer 我们通常会用在背景计算,更新一些数值资料,而如果牵涉到画面的更新,动画过程的演变,我们通常会用CADisplayLink
@property (nonatomic,strong) CADisplayLink *displayLink;
@property (nonatomic,assign) BOOL isAnimating;
//下拉刷新的果冻效果
-(void)effectOfJelly{
[self addObserver:self forKeyPath:kX options: NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:kY options:NSKeyValueObservingOptionNew context:nil];
[self configShapeLayer];
[self confiCurveView];
[self confifAction];
}
//初始化shapeLayer
-(void)configShapeLayer{
_shapeLayer = [CAShapeLayer layer];
_shapeLayer.fillColor = ([UIColor colorWithRed:57/255.0 green:67/255.0 blue:89/255.0 alpha:1]).CGColor;
[self.layer addSublayer:_shapeLayer];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if ([keyPath isEqualToString:kX] || [keyPath isEqualToString:kY]) {
[self updateShapeLayerPath];
}
}
-(void)confifAction{
_mHeight = 100;//手势移动时相对高度
_isAnimating = NO;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];
self.userInteractionEnabled = YES;
[self addGestureRecognizer:pan];
// CADisplayLink默认每秒运行60次calculatePath是算出在运行期间_curveView的坐标,从而确定_shapeLayer的形状
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(calculatePath)];
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
_displayLink.paused = YES;//控制CADisplayLink的运行
_displayLink.frameInterval = 2;//cadisplayLink每隔一帧运行一次,每秒运行30次 ,默认为1
}
-(void)confiCurveView{
self.curveX = SYS_DEVICE_WIDTH / 2.0;
self.curveY = MIN_HEIGHT;
_curceView = [[UIView alloc] initWithFrame:CGRectMake(_curveX, _curveY, 3, 3)];
_curceView.backgroundColor = [UIColor redColor];
[self addSubview:_curceView];
}
-(void)handlePanAction:(UIPanGestureRecognizer *)sender{
if (!_isAnimating) {
if (sender.state == UIGestureRecognizerStateChanged) {
//手势移动时,_shaprLayer跟着手势向下扩展区域
CGPoint point = [sender translationInView:self];
//这部分代码使r5点跟着手势走
_mHeight = point.y * 0.7 + MIN_HEIGHT;
self.curveX = SYS_DEVICE_WIDTH / 2.0 + point.x;
self.curveY = _mHeight > MIN_HEIGHT ? _mHeight : MIN_HEIGHT;
_curceView.frame = CGRectMake(_curveX, _curveY, _curceView.frame.size.width, _curceView.frame.size.height);
}else if (sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed){
// 手势结束时,_shapeLayer返回原状并产生弹簧动效
_isAnimating = YES;
_displayLink.paused = NO; //开启displaylink,会执行方法calculatePath.
//弹簧效果 UIViewAnimationOptionCurveEaseInOut动画由快到慢
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//曲线点(r5点)是一个view,所有在block中有弹簧效果,然后根据他的动效路径,在calcutepath中计算弹性图形的形状
_curceView.frame = CGRectMake(SYS_DEVICE_WIDTH/ 2.0, MIN_HEIGHT, 3, 3);
} completion:^(BOOL finished) {
if (finished) {
_displayLink.paused = YES;
_isAnimating = NO;
}
}];
}
}
}
-(void)updateShapeLayerPath{
//更新_shapeLayer形状
UIBezierPath *tpath = [UIBezierPath bezierPath];
[tpath moveToPoint:CGPointMake(0, 0)];
[tpath addLineToPoint:CGPointMake(SYS_DEVICE_WIDTH, 0)];
[tpath addLineToPoint:CGPointMake(SYS_DEVICE_WIDTH,MIN_HEIGHT)];
[tpath addQuadCurveToPoint:CGPointMake(0, MIN_HEIGHT) controlPoint:CGPointMake(_curveX, _curveY)];// r3,r4,r5确定的一个弧线
[tpath closePath];
_shapeLayer.path = tpath.CGPath;
}
-(void)calculatePath{
//由于手势结束时,r5执行一个uiview的弹簧动画,把这个过程记录下来,并画出相应的_shapLayer
CALayer *layer = _curceView.layer.presentationLayer;
self.curveX = layer.position.x;
self.curveY = layer.position.y;
}
-(void)dealloc{
[self removeObserver:self forKeyPath:kX];
[self removeObserver:self forKeyPath:kY];
}
iOS 下拉刷新之果冻效果
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 先上图🙃Demo在最下方 这次主讲下拉果冻动画效果,自定义Refresh动画请自行下载最下面的Demo,若有算法上...
- 前方高能,老司机~ 一.概述 相信用过path app的人都会被其精美的效果给吸引到,作为一名用户,我首先被其吸引...