-
涉及到的技術(shù)點(diǎn)
- CoreAnimation的基本單位
- 圖片彈跳
- 翻轉(zhuǎn)動(dòng)畫
- 我們看到的現(xiàn)象是:如果以一個(gè)初速度往上拋一個(gè)物體浙炼,你的感覺是物體上拋過程花費(fèi)的時(shí)間短,下落花費(fèi)的時(shí)間長唯袄。所以我們在做動(dòng)畫中需要做這些違背物理規(guī)律的事情:即把下落的時(shí)間設(shè)置得比上彈的長弯屈。
-
JumpStar動(dòng)畫實(shí)現(xiàn)的思路
- 動(dòng)畫分為兩個(gè)階段:一個(gè)彈上去的階段,一個(gè)落下來的階段恋拷。彈上去的過程讓視圖繞 y 軸旋轉(zhuǎn) 90 °资厉,此時(shí)第一階段的動(dòng)畫結(jié)束。在代理方法 animationDidStop 中開始第二個(gè)動(dòng)畫 —— 下落蔬顾。在這個(gè)階段一開始立刻替換圖片酌住, 隨后在落下的同時(shí)讓視圖繼續(xù)旋轉(zhuǎn) 90°。我們要在下落動(dòng)畫結(jié)束之后 removeAllAnimations阎抒。
-
JumpStar動(dòng)畫的具體實(shí)現(xiàn)步驟
-
界面布局如圖所示
ZQJumpStarView
的具體代碼如下
// ZQJumpStarView.h #import <UIKit/UIKit.h> typedef enum : NSUInteger { NONMark, Mark, }STATE; @interface ZQJumpStarView : UIView /** 圖片的狀態(tài) */ @property(nonatomic, assign, setter=setState:)STATE state; /** 被標(biāo)記的圖片 */ @property(nonatomic,strong)UIImage *markedImage; /** 未被標(biāo)記的圖片 */ @property(nonatomic, strong)UIImage *non_markedImage; /** 動(dòng)畫 */ -(void)animation; @end // ZQJumpStarView.m #import "ZQJumpStarView.h" /** 上升時(shí)間的宏 */ #define jumpDuration 0.125 /** 下降時(shí)間的宏 */ #define downDuration 0.215 @interface ZQJumpStarView() /** 圖片View */ @property(nonatomic, strong)UIImageView *starView; /** 陰影View */ @property(nonatomic, strong)UIImageView *shadowView; @end @implementation ZQJumpStarView { /** 動(dòng)畫執(zhí)行 */ BOOL animating; } -(instancetype)init { self = [super init]; if (self) { } return self; } /** 布局子視圖 */ -(void)layoutSubviews { [super layoutSubviews]; self.backgroundColor = [UIColor clearColor]; if (self.starView == nil) { self.starView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 - (self.bounds.size.width - 6) / 2, 0, self.bounds.size.width - 6, self.bounds.size.height - 6)]; self.starView.contentMode = UIViewContentModeScaleToFill; [self addSubview:self.starView]; } if (self.shadowView == nil) { self.shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 - 10 / 2, self.bounds.size.height - 3, 10, 3)]; self.shadowView.alpha = 0.4; self.shadowView.image = [UIImage imageNamed:@"shadow_new"]; [self addSubview:self.shadowView]; } } /** 圖片狀態(tài)的set方法 */ -(void)setState:(STATE)state { _state = state; self.starView.image = _state == Mark ? _markedImage : _non_markedImage; } /** 動(dòng)畫 */ -(void)animation { if (animating == YES) { return; } animating = YES; CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]; transformAnimation.fromValue = @(0); transformAnimation.toValue = @(M_PI_2); transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; positionAnimation.fromValue = @(self.starView.center.y); positionAnimation.toValue = @(self.starView.center.y - 14); positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; animationGroup.duration = jumpDuration; animationGroup.fillMode = kCAFillModeForwards; animationGroup.removedOnCompletion = NO; animationGroup.delegate = self; animationGroup.animations = @[transformAnimation, positionAnimation]; [self.starView.layer addAnimation:animationGroup forKey:@"jumpUp"]; } /** 上升動(dòng)畫開始 */ -(void)animationDidStart:(CAAnimation *)anim { if ([anim isEqual:[self.starView.layer animationForKey:@"jumpUp"]]) { [UIView animateWithDuration:jumpDuration delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{ _shadowView.alpha = 0.2; _shadowView.bounds = CGRectMake(0, 0, _shadowView.bounds.size.width * 1.6, _shadowView.bounds.size.height); } completion:NULL]; } else if ([anim isEqual:[self.starView.layer animationForKey:@"jumpDown"]]) { [UIView animateWithDuration:jumpDuration delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{ _shadowView.alpha = 0.4; _shadowView.bounds = CGRectMake(0, 0, _shadowView.bounds.size.width / 1.6, _shadowView.bounds.size.height); } completion:NULL]; } } /** 下降動(dòng)畫開始 */ -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { if ([anim isEqual:[self.starView.layer animationForKey:@"jumpUp"]]) { self.state = self.state == Mark ? NONMark : Mark; NSLog(@"state:%ld", _state); CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]; transformAnimation.fromValue = @(M_PI_2); transformAnimation.toValue = @(M_PI); transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; positionAnimation.fromValue = @(self.starView.center.y - 14); positionAnimation.toValue = @(self.starView.center.y); positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; animationGroup.duration = downDuration; animationGroup.fillMode = kCAFillModeForwards; animationGroup.removedOnCompletion = NO; animationGroup.delegate = self; animationGroup.animations = @[transformAnimation, positionAnimation]; [self.starView.layer addAnimation:animationGroup forKey:@"jumpDown"]; } else if ([anim isEqual:[self.starView.layer animationForKey:@"jumpDown"]]) { [self.starView.layer removeAllAnimations]; animating = NO; } } @end
-
ViewController
的具體代碼如下
// ViewController.m #import "ViewController.h" #import "ZQJumpStarView.h" @interface ViewController () /** 動(dòng)畫View */ @property (weak, nonatomic) IBOutlet ZQJumpStarView *jumpStarView; @end @implementation ViewController -(void)viewDidLoad { [super viewDidLoad]; [_jumpStarView layoutIfNeeded]; _jumpStarView.markedImage = [UIImage imageNamed:@"icon_star_incell"]; _jumpStarView.non_markedImage = [UIImage imageNamed:@"blue_dot"]; _jumpStarView.state = NONMark; } -(IBAction)tapped:(id)sender { [_jumpStarView animation]; } @end
-
-
運(yùn)行結(jié)果如圖所示
JumpStar
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門穆趴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人遇汞,你說我怎么就攤上這事未妹。” “怎么了空入?”我有些...
- 文/不壞的土叔 我叫張陵络它,是天一觀的道長。 經(jīng)常有香客問我歪赢,道長化戳,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任埋凯,我火速辦了婚禮点楼,結(jié)果婚禮上扫尖,老公的妹妹穿的比我還像新娘。我一直安慰自己掠廓,他們只是感情好藏斩,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著却盘,像睡著了一般狰域。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上黄橘,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼椰于!你這毒婦竟也來了怠益?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對情侶失蹤瘾婿,失蹤者是張志新(化名)和其女友劉穎蜻牢,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體偏陪,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡抢呆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了笛谦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抱虐。...
- 正文 年R本政府宣布轩娶,位于F島的核電站儿奶,受9級(jí)特大地震影響框往,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜闯捎,卻給世界環(huán)境...
- 文/蒙蒙 一椰弊、第九天 我趴在偏房一處隱蔽的房頂上張望许溅。 院中可真熱鬧,春花似錦秉版、人聲如沸贤重。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽并蝗。三九已至,卻和暖如春秸妥,著一層夾襖步出監(jiān)牢的瞬間滚停,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓突雪,卻偏偏與公主長得像起惕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子咏删,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 在iOS中隨處都可以看到絢麗的動(dòng)畫效果惹想,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌督函。在這里你可以看...
- 在iOS中隨處都可以看到絢麗的動(dòng)畫效果勺馆,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌侨核。在這里你可以看...
- 在iOS實(shí)際開發(fā)中常用的動(dòng)畫無非是以下四種:UIView動(dòng)畫草穆,核心動(dòng)畫,幀動(dòng)畫搓译,自定義轉(zhuǎn)場動(dòng)畫悲柱。 1.UIView...
- Core Animation Core Animation,中文翻譯為核心動(dòng)畫些己,它是一組非常強(qiáng)大的動(dòng)畫處理API豌鸡,...
- 本文轉(zhuǎn)載自:http://www.cocoachina.com/ios/20150105/10812.html 為...