#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坐標(biāo)
@property (nonatomic,assign) CGFloat curveY;
@property (nonatomic,strong) UIView *curceView;
// NSTimer 我們通常會用在背景計算袄膏,更新一些數(shù)值資料壳贪,而如果牽涉到畫面的更新远剩,動畫過程的演變煌茴,我們通常會用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默認(rèn)每秒運行60次calculatePath是算出在運行期間_curveView的坐標(biāo)埋合,從而確定_shapeLayer的形狀
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(calculatePath)];
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
_displayLink.paused = YES;//控制CADisplayLink的運行
_displayLink.frameInterval = 2;//cadisplayLink每隔一幀運行一次,每秒運行30次 ,默認(rèn)為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跟著手勢向下擴展區(qū)域
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){
// 手勢結(jié)束時,_shapeLayer返回原狀并產(chǎn)生彈簧動效
_isAnimating = YES;
_displayLink.paused = NO; //開啟displaylink,會執(zhí)行方法calculatePath.
//彈簧效果 UIViewAnimationOptionCurveEaseInOut動畫由快到慢
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//曲線點(r5點)是一個view,所有在block中有彈簧效果,然后根據(jù)他的動效路徑,在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{
//由于手勢結(jié)束時,r5執(zhí)行一個uiview的彈簧動畫,把這個過程記錄下來,并畫出相應(yīng)的_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 下拉刷新之果凍效果
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瘟仿,“玉大人箱锐,你說我怎么就攤上這事±徒希” “怎么了驹止?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長观蜗。 經(jīng)常有香客問我臊恋,道長,這世上最難降的妖魔是什么墓捻? 我笑而不...
- 正文 為了忘掉前任抖仅,我火速辦了婚禮,結(jié)果婚禮上砖第,老公的妹妹穿的比我還像新娘撤卢。我一直安慰自己,他們只是感情好梧兼,可當(dāng)我...
- 文/花漫 我一把揭開白布放吩。 她就那樣靜靜地躺著,像睡著了一般袱院。 火紅的嫁衣襯著肌膚如雪屎慢。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼欣喧,長吁一口氣:“原來是場噩夢啊……” “哼腌零!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起唆阿,我...
- 正文 年R本政府宣布,位于F島的核電站盛正,受9級特大地震影響删咱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜豪筝,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望摘能。 院中可真熱鬧续崖,春花似錦、人聲如沸团搞。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽逻恐。三九已至像吻,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間复隆,已是汗流浹背拨匆。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 先上圖??Demo在最下方 這次主講下拉果凍動畫效果,自定義Refresh動畫請自行下載最下面的Demo黎侈,若有算法上...
- 前方高能峻汉,老司機~ 一.概述 相信用過path app的人都會被其精美的效果給吸引到贴汪,作為一名用戶,我首先被其吸引...
- 人要是不興奮俱济,抗利尿激素都不給力嘶是。Talk is cheap,show code蛛碌,留著給自己以后看聂喇。 然后創(chuàng)建一個...