iOS添加購物車動(dòng)畫效果示例
一、計(jì)算動(dòng)畫開始結(jié)束點(diǎn)位置
方法:
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
1) 動(dòng)畫開始位置fromCenter
CGPoint fromCenter = [animationView convertPoint:CGPointMake(animationView.frame.size.width * 0.5f, animationView.frame.size.height * 0.5f) toView:keyWindow];
2)動(dòng)畫結(jié)束位置endCenter
CGPoint endCenter = [endView convertPoint:CGPointMake(endView.frame.size.width * 0.5f, endView.frame.size.height * 0.5f) toView:keyWindow];
二、計(jì)算貝塞爾曲線(拋物線)的兩個(gè)控制點(diǎn)

- controlPoint1是控制點(diǎn)1
- controlPoint2是控制點(diǎn)2
- A是controlPoint1和controlPoint2的中點(diǎn)
- controlPointC是fromCenter和B的中點(diǎn)
1)先設(shè)置控制點(diǎn)距最高點(diǎn)(fromCenter或endCenter)的水平距離controlPointEY,本篇默認(rèn)controlPointEY = 100,即圖1中點(diǎn)controlPointC到點(diǎn)A的距離。
2)計(jì)算控制點(diǎn)相對于點(diǎn)A的距離controlPointEX,即controlPoint1到A距離或controlPoint2到A距離,本篇設(shè)置為fromCenter.x到endCenter.x的1/4,即controlPointEX = (endCenter.x - fromCenter.x) * 0.25f;
3)計(jì)算兩個(gè)控制點(diǎn)
CGPoint controlPoint1 = CGPointMake(controlPointCX - controlPointEX, controlPointCY - controlPointEY); CGPoint controlPoint2 = CGPointMake(controlPointCX + controlPointEX, controlPointCY - controlPointEY);
三、復(fù)制動(dòng)畫的layer
NSString *str = ((UIButton *)animationView).titleLabel.text; _animationLayer = [CATextLayer layer]; _animationLayer.bounds = animationView.bounds; _animationLayer.position = fromCenter; _animationLayer.alignmentMode = kCAAlignmentCenter;//文字對齊方式 _animationLayer.wrapped = YES; _animationLayer.contentsScale = [UIScreen mainScreen].scale; _animationLayer.string = str; _animationLayer.backgroundColor = [UIColor redColor].CGColor; [keyWindow.layer addSublayer:_animationLayer];
四、動(dòng)畫組合
1)運(yùn)動(dòng)軌跡(拋物線)
UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:fromCenter]; [path addCurveToPoint:endCenter controlPoint1:controlPoint1 controlPoint2:controlPoint2]; CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; pathAnimation.path = path.CGPath;
2)旋轉(zhuǎn)起來
CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; rotateAnimation.removedOnCompletion = YES; rotateAnimation.fromValue = [NSNumber numberWithFloat:0]; rotateAnimation.toValue = [NSNumber numberWithFloat:10 * M_PI]; rotateAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]
3)縮放動(dòng)畫
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; scaleAnimation.removedOnCompletion = NO; scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0]; scaleAnimation.toValue = [NSNumber numberWithFloat:0.2];
4)透明度動(dòng)畫
CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; alphaAnimation.removedOnCompletion = NO; alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0]; alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
5)動(dòng)畫組合
CAAnimationGroup *groups = [CAAnimationGroup animation]; groups.animations = @[pathAnimation,rotateAnimation, scaleAnimation, alphaAnimation]; groups.duration = kShoppingCartDuration; groups.removedOnCompletion=NO; groups.fillMode=kCAFillModeForwards; groups.delegate = self; [_animationLayer addAnimation:groups forKey:@"group"];
動(dòng)畫效果:

下載地址:ShoppingCartAnimation_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用IOS自動(dòng)化測試工具UIAutomation
這篇文章主要介紹了UIAutomation使用實(shí)例、應(yīng)用技巧、基本知識點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值2021-04-04
iOS開發(fā)使用GDataXML框架解析網(wǎng)絡(luò)數(shù)據(jù)
GDataXML是Google開發(fā)的一個(gè)XML解析庫,輕便,特點(diǎn)使用非常簡單,支持XPath。今天把前兩天弄的IOS XML解析記錄下來,也供大家參考。2016-02-02
iOS實(shí)現(xiàn)淘寶上拉進(jìn)入詳情頁交互效果
最近遇到一個(gè)項(xiàng)目,項(xiàng)目中某個(gè)新需求的交互要求仿照淘寶上拉從下往上彈出寶貝詳情。所以死打開淘寶APP仔細(xì)看了看,然后自己寫了寫,現(xiàn)在感覺效果差不多了,記錄一下分享給大家,方法自己和大家需要的時(shí)候查看借鑒,感興趣的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
iOS省市二級聯(lián)動(dòng)的數(shù)據(jù)組織PHP版
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)之"省市"二級聯(lián)動(dòng)的數(shù)據(jù)組織PHP版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
iOS runtime forwardInvocation詳解及整理
這篇文章主要介紹了 iOS runtime forwardInvocation詳解及整理的相關(guān)資料,需要的朋友可以參考下2017-02-02
iOS 實(shí)現(xiàn)多代理的方法及實(shí)例代碼
這篇文章主要介紹了iOS 實(shí)現(xiàn)多代理的方法及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
iOS動(dòng)畫-定時(shí)對UIView進(jìn)行翻轉(zhuǎn)和抖動(dòng)的方法
下面小編就為大家?guī)硪黄猧OS動(dòng)畫-定時(shí)對UIView進(jìn)行翻轉(zhuǎn)和抖動(dòng)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

