iOS實現(xiàn)貝塞爾曲線動畫
本文實例為大家分享了iOS實現(xiàn)貝塞爾曲線動畫的具體代碼,供大家參考,具體內(nèi)容如下
效果如圖:
仿美人相機(jī),手勢滑動隱藏頂部view。為了方便講解,將屏幕分為幾個區(qū)域,如圖:
在拖動過程中:
1、拖動距離小于minMoveDistance,貝賽爾曲線發(fā)生形變
2、拖動大于minMoveDistance,整個view開始下移
在松開手時:
1、拖動距離小于minMoveDistance,未發(fā)生位移,貝塞爾曲線恢復(fù)形變
2、拖動大于minMoveDistance,小于minDisappearDistance,貝塞爾曲線恢復(fù)形變、位移回到初始位置。
3、拖動大于minDisappearDistance,向下移動隱藏view
一、根據(jù)y軸位移量確定貝塞爾路徑
在拖動時曲線形變、松手時曲線恢復(fù)形變的時候都需要改變貝塞爾曲線,實際上是改變二階貝塞爾曲線的控制點,因此寫一個方法,根據(jù)傳入的y軸位移量得到新的貝塞爾路徑:
- (CGPathRef)getPathWithMoveDistance:(CGFloat)distance{ UIBezierPath *path = [UIBezierPath bezierPath]; CGPoint startPoint = CGPointMake(0, 0); CGPoint controlPoint = CGPointMake(self.bounds.size.width*0.5, 60+distance); CGPoint endPoint = CGPointMake(self.bounds.size.width, 0); [path moveToPoint:startPoint]; [path addQuadCurveToPoint:endPoint controlPoint:controlPoint]; [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)]; [path addLineToPoint:CGPointMake(0, self.bounds.size.height)]; return path.CGPath; }
二、初始化圖形
初始化的時候,可以根據(jù)上一步的方法,傳入位移量0,獲取貝塞爾路徑,并將獲得的路徑賦給CAShapeLayer:
- (id)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.originY = frame.origin.y; [self setUpLayer]; [self addGesure]; } return self; } - (void)setUpLayer{ self.topLineLayer = [CAShapeLayer layer]; self.topLineLayer.fillColor = ColorForTheme.CGColor; self.topLineLayer.strokeColor = ColorForTheme.CGColor; self.topLineLayer.path = [self getPathWithMoveDistance:0]; [self.layer addSublayer:self.topLineLayer]; }
三、添加手勢
添加UIPanGestureRecognizer拖動手勢,并處理手勢拖動和結(jié)束拖動的事件。
拖動時:
1、拖動距離小于minMoveDistance,只有貝賽爾曲線發(fā)生形變,調(diào)用步奏一中的方法。
2、拖動距離大于minMoveDistance,整個view開始下移
結(jié)束拖動時:
調(diào)用revertFormY:方法,傳入當(dāng)前在y軸上已經(jīng)發(fā)生的位移量
- (void)addGesure{ if (self.gesture == nil) { self.gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; } [self addGestureRecognizer:self.gesture]; } - (void)handlePan:(UIPanGestureRecognizer *)gesture{ CGFloat distanceX = [gesture translationInView:self].x; CGFloat distanceY = [gesture translationInView:self].y; if (ABS(distanceX) > ABS(distanceY)) { return; } //拖動過程 if (gesture.state == UIGestureRecognizerStateChanged) { NSLog(@"%f",distanceY); //移動少于minMoveDistance,貝賽爾曲線形變 if (distanceY > 0 && distanceY <= minMoveDistance) { self.topLineLayer.path = [self getPathWithMoveDistance:distanceY]; } //移動大于minMoveDistance,整個view下移 else if (distanceY > minMoveDistance) { self.frame = CGRectMake(0, self.originY+distanceY-minMoveDistance, self.bounds.size.width, self.bounds.size.height); } } //手勢結(jié)束 if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled || gesture.state == UIGestureRecognizerStateFailed) { [self removeGestureRecognizer:self.gesture]; [self revertFormY:distanceY]; } }
四、revertFormY:恢復(fù)形變方法
根據(jù)傳入的y軸上的位移量,實現(xiàn)不同的效果:
1、y小于minMoveDistance,未發(fā)生位移,貝塞爾曲線恢復(fù)形變
2、y大于minMoveDistance,小于minDisappearDistance,貝塞爾曲線恢復(fù)形變、位移回到初始位置。
3、y大于minDisappearDistance,向下移動隱藏view
//手勢結(jié)束后恢復(fù)或隱藏 -(void)revertFormY:(CGFloat)y{ //y < 最小的隱藏位移距離,未發(fā)生位移,貝塞爾曲線恢復(fù)動畫 if (y < minDisappearDistance) { CAKeyframeAnimation *vibrate = [CAKeyframeAnimation animationWithKeyPath:@"path"]; vibrate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; vibrate.values = @[ (id) [self getPathWithMoveDistance:y], (id) [self getPathWithMoveDistance:-(y * 0.3)], (id) [self getPathWithMoveDistance:(y * 0.2)], (id) [self getPathWithMoveDistance:-(y * 0.15)], (id) [self getPathWithMoveDistance:(y * 0.1)], (id) [self getPathWithMoveDistance:-(y * 0.07)], (id) [self getPathWithMoveDistance:(y * 0.05)], (id) [self getPathWithMoveDistance:0.0] ]; vibrate.duration = 0.5; vibrate.removedOnCompletion = NO; vibrate.fillMode = kCAFillModeForwards; vibrate.delegate = self; [self.topLineLayer addAnimation:vibrate forKey:nil]; } //y > 最小位移距離,發(fā)生了位移 if(y > minMoveDistance){ [UIView animateWithDuration:0.3 animations:^{ CGFloat endY; //向上恢復(fù)view if (y < minDisappearDistance) { endY = self.originY; } //向下隱藏view else{ endY = SCREEN_HEIGHT; } self.frame = CGRectMake(0, endY, SCREEN_WIDTH, self.frame.size.height); }]; } }
五、雙擊屏幕空白,消失的view從底部上移,恢復(fù)到初始位置
這個只是為了回到初始狀態(tài),根據(jù)實際需求實現(xiàn)。
//恢復(fù)到初始位置 - (void)comeBack{ if (self.frame.origin.y <= self.originY) { return; } [UIView animateWithDuration:0.3 animations:^{ self.frame = CGRectMake(0, self.originY, SCREEN_WIDTH, self.frame.size.height); } completion:^(BOOL finished) { [self revertFormY:10]; }]; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動卡頓問題解決的方法
這篇文章主要給大家介紹了關(guān)于iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動卡頓問題解決的方法,文中通過示例代碼介紹的非常詳細(xì),對同樣遇到的朋友們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起看看吧。2018-03-03iOS實現(xiàn)對不同分辨率設(shè)備的字號大小適配方法
下面小編就為大家分享一篇iOS實現(xiàn)對不同分辨率設(shè)備的字號大小適配方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01iOS?GCD之dispatch_group_enter和dispatch_group_leave使用
這篇文章主要為大家介紹了iOS?GCD之dispatch_group_enter和dispatch_group_leave使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03IOS 開發(fā)之 UITextField限制字?jǐn)?shù)的方法
這篇文章主要介紹了IOS 開發(fā)之 UITextField限制字?jǐn)?shù)的方法的相關(guān)資料,這里提供實現(xiàn)限制最大字?jǐn)?shù)的方法,需要的朋友可以參考下2017-08-08IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器
這篇文章主要介紹了IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器的步驟,幫助大家更好的理解和學(xué)習(xí)使用ios開發(fā),感興趣的朋友可以了解下2021-04-04iOS UICollectionView實現(xiàn)橫向滑動
這篇文章主要為大家詳細(xì)介紹了iOS UICollectionView實現(xiàn)橫向滑動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用
這篇文章主要介紹了Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用,重點講解了EGOCache對緩存過期時間的檢測及處理,需要的朋友可以參考下2016-05-05