iOS中的動畫有兩種實現(xiàn)方式,一種是UIView來實現(xiàn)動畫,另一種動畫是通過CALayer來實現(xiàn),下面介紹兩種動畫的簡單實現(xiàn):
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比平和網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式平和網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋平和地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
一、UIView動畫的實現(xiàn)
UIView使用Context來實現(xiàn)動畫
關(guān)鍵代碼:
//參數(shù)1 動畫名稱 參數(shù)2 要實現(xiàn)動畫的對象上下文 [UIView beginAnimations:@"attribute" context:_showImageView]; //設(shè)置動畫的時間 [UIView setAnimationDuration:1.0f]; //設(shè)置動畫延遲時間 // [UIView setAnimationDelay:2]; //設(shè)置視圖center 實現(xiàn)試圖移動動畫 _showImageView.center = CGPointMake(100, 100); //設(shè)置alpha值:視圖透明度 _showImageView.alpha = 0.2f; //設(shè)置背景顏色 _showImageView.backgroundColor = [UIColor greenColor]; //UIView動畫 設(shè)置代理 [UIView setAnimationDelegate:self]; //動畫將要開始代理方法 [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)]; //動畫已經(jīng)結(jié)束代理方法 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; //提交動畫設(shè)置,執(zhí)行動畫 [UIView commitAnimations];
使用Block實現(xiàn)的動畫:
//UIView動畫, 使用Block實現(xiàn) [UIView animateWithDuration:1.0f animations:^{ //通過設(shè)置translation 實現(xiàn)視圖的偏移 if ([self.mySwitch isOn]) { //基于上一次的translation _showImageView.transform = CGAffineTransformTranslate(_showImageView.transform, 50, 0); } else { //基于原始的translation _showImageView.transform = CGAffineTransformMakeTranslation(-50, 0); } }];
二、CALayer動畫的實現(xiàn)
CABasic動畫的實現(xiàn):根據(jù)初始位置和結(jié)束位置確定動畫
//CABasic 有兩個屬性 fromValue 動畫開始值,toValue動畫結(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)建一組動畫:
//創(chuàng)建組動畫對象 CAAnimationGroup *group = [CAAnimationGroup animation]; //CABasic動畫 CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"]; animation1.fromValue = @1.5; animation1.toValue = @0.5; //關(guān)鍵幀動畫 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添加動畫數(shù)組,group中動畫對象并發(fā)執(zhí)行 [group setAnimations:@[animation1, animation2]]; [group setDuration:4.0f]; [_p_w_picpathView.layer addAnimation:group forKey:@"group"];
完整的工程和代碼見:https://github.com/winann/iOS-Animation
工程中的動畫實現(xiàn)方法比較全,而且都有注釋,大家可以直接把工程下載下來,邊看邊練習一下。
本文名稱:iOS動畫:UIView動畫和CALayer動畫(CABasicAnimation、CAKeyframeAnimation的使用)
本文網(wǎng)址:http://sd-ha.com/article40/jocpeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、做網(wǎng)站、自適應網(wǎng)站、響應式網(wǎng)站、網(wǎng)站策劃、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)