您好,登錄后才能下訂單哦!
iOS中的動(dòng)畫(huà)有兩種實(shí)現(xiàn)方式,一種是UIView來(lái)實(shí)現(xiàn)動(dòng)畫(huà),另一種動(dòng)畫(huà)是通過(guò)CALayer來(lái)實(shí)現(xiàn),下面介紹兩種動(dòng)畫(huà)的簡(jiǎn)單實(shí)現(xiàn):
一、UIView動(dòng)畫(huà)的實(shí)現(xiàn)
UIView使用Context來(lái)實(shí)現(xiàn)動(dòng)畫(huà)
關(guān)鍵代碼:
//參數(shù)1 動(dòng)畫(huà)名稱 參數(shù)2 要實(shí)現(xiàn)動(dòng)畫(huà)的對(duì)象上下文 [UIView beginAnimations:@"attribute" context:_showImageView]; //設(shè)置動(dòng)畫(huà)的時(shí)間 [UIView setAnimationDuration:1.0f]; //設(shè)置動(dòng)畫(huà)延遲時(shí)間 // [UIView setAnimationDelay:2]; //設(shè)置視圖center 實(shí)現(xiàn)試圖移動(dòng)動(dòng)畫(huà) _showImageView.center = CGPointMake(100, 100); //設(shè)置alpha值:視圖透明度 _showImageView.alpha = 0.2f; //設(shè)置背景顏色 _showImageView.backgroundColor = [UIColor greenColor]; //UIView動(dòng)畫(huà) 設(shè)置代理 [UIView setAnimationDelegate:self]; //動(dòng)畫(huà)將要開(kāi)始代理方法 [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)]; //動(dòng)畫(huà)已經(jīng)結(jié)束代理方法 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; //提交動(dòng)畫(huà)設(shè)置,執(zhí)行動(dòng)畫(huà) [UIView commitAnimations];
使用Block實(shí)現(xiàn)的動(dòng)畫(huà):
//UIView動(dòng)畫(huà), 使用Block實(shí)現(xiàn) [UIView animateWithDuration:1.0f animations:^{ //通過(guò)設(shè)置translation 實(shí)現(xiàn)視圖的偏移 if ([self.mySwitch isOn]) { //基于上一次的translation _showImageView.transform = CGAffineTransformTranslate(_showImageView.transform, 50, 0); } else { //基于原始的translation _showImageView.transform = CGAffineTransformMakeTranslation(-50, 0); } }];
二、CALayer動(dòng)畫(huà)的實(shí)現(xiàn)
CABasic動(dòng)畫(huà)的實(shí)現(xiàn):根據(jù)初始位置和結(jié)束位置確定動(dòng)畫(huà)
//CABasic 有兩個(gè)屬性 fromValue 動(dòng)畫(huà)開(kāi)始值,toValue動(dòng)畫(huà)結(jié)束值 CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"position"]; [animation1 setDuration:2]; animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 150)]; animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)]; [_p_w_picpathView.layer addAnimation:animation1 forKey:@"position"];
創(chuàng)建一組動(dòng)畫(huà):
//創(chuàng)建組動(dòng)畫(huà)對(duì)象 CAAnimationGroup *group = [CAAnimationGroup animation]; //CABasic動(dòng)畫(huà) CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"]; animation1.fromValue = @1.5; animation1.toValue = @0.5; //關(guān)鍵幀動(dòng)畫(huà) CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"position"]; animation2.values = @[[NSValue valueWithCGPoint:CGPointMake(100, 100)], [NSValue valueWithCGPoint:CGPointMake(200, 150)], [NSValue valueWithCGPoint:CGPointMake(100, 200)], [NSValue valueWithCGPoint:CGPointMake(200, 250)]]; //group添加動(dòng)畫(huà)數(shù)組,group中動(dòng)畫(huà)對(duì)象并發(fā)執(zhí)行 [group setAnimations:@[animation1, animation2]]; [group setDuration:4.0f]; [_p_w_picpathView.layer addAnimation:group forKey:@"group"];
完整的工程和代碼見(jiàn):https://github.com/winann/iOS-Animation
工程中的動(dòng)畫(huà)實(shí)現(xiàn)方法比較全,而且都有注釋,大家可以直接把工程下載下來(lái),邊看邊練習(xí)一下。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。