ios 移除動(dòng)畫(huà)

在移除動(dòng)畫(huà)之前, 首先得在 layer 上添加兩個(gè)動(dòng)畫(huà)
示例代碼:

CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.rotation";
animation.duration = 2.0;
animation.byValue = @(M_PI * 2);
animation.delegate = self;

animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[self.layer addAnimation:animation forKey:@"rotateAnimation"];

CABasicAnimation *animation2 = [CABasicAnimation animation];
animation2.keyPath = @"transform.scale";
animation2.duration = 2.0;
animation2.toValue = @2.0;
animation2.delegate = self;

animation2.removedOnCompletion = NO;
animation2.fillMode = kCAFillModeForwards;
[self.layer addAnimation:animation2 forKey:@"scale"];

添加兩個(gè)按鈕來(lái)控制動(dòng)畫(huà)的 開(kāi)始 和 移除 操作, 代碼(略)

點(diǎn)擊開(kāi)始, 執(zhí)行上面的代碼

點(diǎn)擊結(jié)束:
1.移除制定動(dòng)畫(huà)

[self.layer removeAnimationForKey:@"rotateAnimation"];

2.移除所有動(dòng)畫(huà)

[self.layer removeAllAnimations];

查看動(dòng)畫(huà)結(jié)束時(shí)的打印:

#pragma mark Animation delegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSString *strAnimation = nil;
    if ([self.layer animationForKey:@"rotateAnimation"] == anim) {
        strAnimation = @"rotateAnimation";
    }
    if ([self.layer animationForKey:@"scale"] == anim){
        strAnimation = @"scaleAnimation";
    }
    NSLog(@"%@ stop finish: %@",strAnimation, flag? @"yes": @"no");
}

特別注意:
1.以上方法取值時(shí) 使用的是 animationForKey 而不是 valueForKey
2.一定要把 animation 的 removedOnCompletion 設(shè)置成 NO, 否則取不到值

執(zhí)行以上代碼后, 發(fā)現(xiàn)手動(dòng)結(jié)束動(dòng)畫(huà)時(shí), 圖像會(huì)還原到原來(lái)的位置.
解決方法:
在移除動(dòng)畫(huà)操作后添加以下代碼

// 動(dòng)畫(huà)移除后 保持在當(dāng)前位置
self.layer.transform = self.layer.presentationLayer.transform;

執(zhí)行之后效果如下:

效果圖.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS實(shí)際開(kāi)發(fā)中常用的動(dòng)畫(huà)無(wú)非是以下四種:UIView動(dòng)畫(huà),核心動(dòng)畫(huà),幀動(dòng)畫(huà),自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,315評(píng)論 1 23
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫(huà)效果,實(shí)現(xiàn)這些動(dòng)畫(huà)的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫(huà)全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,688評(píng)論 6 30
  • 一、簡(jiǎn)介 Core Animation,中文翻譯為核心動(dòng)畫(huà),它是一組非常強(qiáng)大的動(dòng)畫(huà)處理API,使用它能做出非常炫麗...
    莦婼姑娘閱讀 1,053評(píng)論 0 4
  • Core Animation Core Animation,中文翻譯為核心動(dòng)畫(huà),它是一組非常強(qiáng)大的動(dòng)畫(huà)處理API,...
    45b645c5912e閱讀 3,154評(píng)論 0 21
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫(huà)效果,實(shí)現(xiàn)這些動(dòng)畫(huà)的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫(huà)全貌。在這里你可以看...
    F麥子閱讀 5,267評(píng)論 5 13

友情鏈接更多精彩內(nèi)容