您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 碎片化動畫實作詳解

編輯:iOS 開發綜合

效果預覽

遮罩視圖 maskView

UIView 提供了一個 maskView 屬性(iOS 8 之後可用),是實作本動畫的關鍵。它的作用類似於修改 alpha 來改變透明度,但差別在於:作為遮罩的視圖,其 backgroundColor、alpha、transform 等屬性都會影響被遮蓋視圖的顯示效果。

UIView *viewContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
viewContainer.backgroundColor = [UIColor blueColor];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 160, 160)];
contentView.backgroundColor = [UIColor redColor];
[viewContainer addSubview:contentView];
UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 35, 80)];
maskView.backgroundColor = [UIColor yellowColor];
contentView.maskView = maskView;

遮罩視圖決定了視圖的顯示內容

實驗可知:修改視圖自身或 maskView 的透明度,效果一致。也就是說,遮罩主要透過透明度與顯示尺寸來影響可見內容。

顏色的影響

當遮罩顏色不含透明度時,視圖不存在透明效果;若為遮罩顏色設定 alpha:

maskView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5]; // 任意顏色

其效果等同於直接設定 alpha = 0.5。

maskView 的子視圖

maskView.backgroundColor = [UIColor clearColor];
UIView *sub1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 34)];
sub1.backgroundColor = [UIColor blackColor];
UIView *sub2 = [[UIView alloc] initWithFrame:CGRectMake(15, 18, 33, 40)];
sub2.backgroundColor = [UIColor blackColor];
[maskView addSubview:sub1];
[maskView addSubview:sub2];

子視圖對於遮罩的影響

即使遮罩自身透明,其子視圖也能實現部分遮罩效果;改變子視圖的透明度,遮罩效果也會隨之改變。

動畫實作

目標動畫中,圖片被切成多個小方塊逐漸消失(垂直 2 份、水平約 15 份)。因此在 maskView 上均勻加入 2×15 個子視圖,並為每個加上 tag 以便依序控制:

UIView *maskView = [[UIView alloc] initWithFrame:contentView.bounds];
const NSInteger horizontalCount = 15;
const NSInteger verticalCount = 2;
const CGFloat fadeWidth = CGRectGetWidth(maskView.frame) / horizontalCount;
const CGFloat fadeHeight = CGRectGetHeight(maskView.frame) / verticalCount;
for (NSInteger line = 0; line < horizontalCount; line++) {
    for (NSInteger row = 0; row < verticalCount; row++) {
        CGRect frame = CGRectMake(line * fadeWidth, row * fadeHeight, fadeWidth, fadeHeight);
        UIView *fadeView = [[UIView alloc] initWithFrame:frame];
        fadeView.tag = [self viewTag:line * verticalCount + row];
        fadeView.backgroundColor = [UIColor whiteColor];
        [maskView addSubview:fadeView];
    }
}
contentView.maskView = maskView;

動畫開始時,遍歷所有子視圖並依序執行 alpha 由 1 到 0 的動畫:

for (NSInteger line = 0; line < horizontalCount; line++) {
    for (NSInteger row = 0; row < verticalCount; row++) {
        NSInteger idx = line * verticalCount + row;
        UIView *fadeView = [contentView.maskView viewWithTag:[self viewTag:idx]];
        [UIView animateWithDuration:fadeDuration delay:interval * idx options:UIViewAnimationOptionCurveLinear animations:^{
            fadeView.alpha = 0;
        } completion:nil];
    }
}

封裝為 Category

為了方便複用並降低耦合,可把碎片化動畫封裝成 UIView 的分類。由於在 category 中宣告的屬性編譯器不會自動綁定 getter/setter,需搭配 objc/runtime.h 的關聯物件機制來動態增加屬性:

- (BOOL)isFade {
    return [objc_getAssociatedObject(self, kIsFadeKey) boolValue];
}
- (void)setIsFade:(BOOL)isFade {
    objc_setAssociatedObject(self, kIsFadeKey, @(isFade), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

還原動畫則反向遍歷,讓子視圖的 alpha 由 0 回到 1:

NSInteger fadeCount = self.verticalCount * self.horizontalCount;
for (NSInteger idx = fadeCount - 1; idx >= 0; idx--) {
    UIView *subview = [self.maskView viewWithTag:[self subViewTag:idx]];
    [UIView animateWithDuration:self.fadeAnimationDuration delay:self.intervalDuration * (fadeCount - 1 - idx) options:UIViewAnimationOptionCurveLinear animations:^{
        subview.alpha = 1;
    } completion:nil];
}

由於每個方塊動畫的結束時間不一,可用臨時計數變數記錄完成數量,當完成數等於 fadeCount 時即代表整體動畫結束,並觸發完成回調 block:

__block NSInteger timeCount = 0;
// 於每個動畫的 completion 中:
if (++timeCount == fadeCount) {
    self.isFade = NO;
    self.isFading = NO;
    if (complete) { complete(); }
}

輪播碎片動畫

有了碎片動畫後,可延伸做出廣告輪播頁:在目前圖片下方插入一個 UIImageView 預先展示下一張圖,加上 UIPageControl 標示下標,並在完成回調中更新圖片、循環播放。

廣告輪播頁

特別提醒:完成回調的 block 需要用 objc_setAssociatedObject 動態保存,否則當 block 執行到第三次圖片碎片時會被釋放而造成崩潰。此外,每次切換完成後別忘了把所有子視圖遮罩還原(reverseWithoutAnimate)再更新圖片。

小結

以上就是 iOS 碎片化動畫的完整實作,核心在於 maskView 搭配子視圖的透明度動畫,並善用關聯物件封裝與計數判斷動畫結束,希望對你開發 iOS 動畫有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved