Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

POP 動畫實作 01:彈出式提示浮層

編輯:關於 Android 程式設計

前言

從標題大概能猜到——我打算做一系列 POP 相關的小組件,這是第一個。演示圖我挑了好一陣子,趁著中午空檔發出來,午沒睡好,有點困,廢話有點多。

這個小零件的用途是「提示浮層」,在許多直播類 App 裡很常見:一個半透明遮罩鋪滿全屏,中間彈出一塊提示視圖,點擊任意位置或下滑即可關閉。文末會附上完整示例工程。

思路:自定義 XTPopingView

新建一個繼承自 UIView 的 XTPopingView,先實現最基礎的兩種進場方向:從下往上(下-下)、從上往下(上-下)。動畫引擎選用 Facebook 的 Pop,聲明一下依賴。

1. 初始化

初始化時做三件事:鋪一層半透明黑底、把真正要展示的 flyView 加進來、綁定一個點擊手勢用於關閉。

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 半透明遮罩
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self addSubview:self.flyView];

        // 點擊任意位置關閉
        UITapGestureRecognizer *tapGes =
            [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClickAction)];
        [self addGestureRecognizer:tapGes];
    }
    return self;
}

- (UIView *)flyView {
    if (!_flyView) {
        _flyView = [[UIView alloc] init];
    }
    return _flyView;
}
2. 進場動畫

關鍵在於:先把 flyView 擺到「屏幕外」的起始位置,再用一個 POPSpringAnimation 把它的 center 動畫到目標點。springSpeed 控制速度,springBounciness 控制回彈幅度——這兩個參數的搭配決定了動畫是「沉穩」還是「Q 彈」。

- (void)startFly:(FlyType)type {
    switch (type) {
        case FlyTypeUToD: {   // 從上方飛入
            _flyView.frame = CGRectMake(SCREEN_WIDTH_XT / 2 - self.fly_w / 2,
                                        -self.fly_h,
                                        self.fly_w, self.fly_h);
            break;
        }
        case FlyTypeDToD: {   // 從下方飛入
            _flyView.frame = CGRectMake(SCREEN_WIDTH_XT / 2 - self.fly_w / 2,
                                        SCREEN_HEIGHT_XT + self.fly_h,
                                        self.fly_w, self.fly_h);
            break;
        }
        default:
            break;
    }
    _flyView.backgroundColor = [UIColor purpleColor];

    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    anim.toValue = [NSValue valueWithCGPoint:self.center];
    anim.springSpeed = 5;          // 速度
    anim.springBounciness = 10.0f; // 彈力,即晃動幅度
    [_flyView pop_addAnimation:anim forKey:@"animationShow"];
}
3. 退場動畫

點擊時把目標 center 設到屏幕下方之外,並在動畫即將結束時把自己從父視圖移除。這裡用 dispatch_after 做延遲移除,時間要略小於動畫時長,才能保證視覺上不突兀。

- (void)tapClickAction {
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x,
                                                        SCREEN_HEIGHT_XT + self.fly_h)];
    anim.springSpeed = 5;
    [_flyView pop_addAnimation:anim forKey:@"animationRemove"];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)),
                   dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
}
小結

整套實現的核心只有一句話:把元素放到屏幕外,再用彈簧動畫把 center 拉回來。後續可以在這個骨架上擴充:切換多種 FlyType、支持連續彈出多條提示、加入模糊背景等。完整示例工程隨博文放出。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved