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)相對(duì)于點(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;//文字對(duì)齊方式 _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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter之TabBarView組件項(xiàng)目實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Flutter之TabBarView組件項(xiàng)目實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10iOS中Label實(shí)現(xiàn)顯示不同顏色與字體的方法
這篇文章主要給大家介紹了關(guān)于在iOS中Label實(shí)現(xiàn)顯示不同顏色與字體的相關(guān)資料,文中分別介紹了利用range或者文字兩種實(shí)現(xiàn)的方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例
下面小編就為大家分享一篇iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01iOS開發(fā)使用UITableView制作N級(jí)下拉菜單的示例
這篇文章主要介紹了iOS開發(fā)使用UITableView制作N級(jí)下拉菜單的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01iOS小技能之字典轉(zhuǎn)模及對(duì)象相等性示例詳解
這篇文章主要為大家介紹了iOS小技能之字典轉(zhuǎn)模及對(duì)象相等性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08iOS仿網(wǎng)易新聞滾動(dòng)導(dǎo)航條效果
這篇文章主要為大家詳細(xì)介紹了iOS仿網(wǎng)易新聞滾動(dòng)導(dǎo)航條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05深入解析iOS應(yīng)用開發(fā)中九宮格視圖布局的相關(guān)計(jì)算方法
這篇文章主要介紹了iOS應(yīng)用開發(fā)中九宮格視圖布局的計(jì)算方法,包括item尺寸和坐標(biāo)等一系列影像布局的數(shù)值相關(guān)計(jì)算的講解,需要的朋友可以參考下2016-03-03