您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 抽屜效果開發案例:手勢拖動與縮放動畫

編輯:iOS 相關教學

本文分享一個左右抽屜效果的實作。做法是在顯示於窗口的控制器上疊三個 view:左邊頁、右邊頁與主頁(若只需要單側滑出,兩個 view 就夠)。拖動時主頁跟手移動,並依水平位移量同步做一點縮放,手勢結束後再吸附到最近的定位點。

一、聲明並加入三個 view

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, weak) UIView *mainV;
@property (nonatomic, weak) UIView *leftV;
@property (nonatomic, weak) UIView *rightV;
@end
#pragma mark - 添加子控件

- (void)setUpChildViews {
    UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];
    leftV.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:leftV];
    _leftV = leftV;

    UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];
    rightV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:rightV];
    _rightV = rightV;

    // 主視圖放在最上層
    UIView *mainV = [[UIView alloc] initWithFrame:self.view.bounds];
    mainV.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:mainV];
    _mainV = mainV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpChildViews];

    // 拖動手勢
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan;

    // 點按手勢:在主視圖任意位置點擊即可回到起始位置
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.view addGestureRecognizer:tap];
}

二、手勢處理與吸附定位

#pragma mark - 手勢識別方法

#define targetL -230
#define targetR 200
#define screenW [UIScreen mainScreen].bounds.size.width

- (void)pan:(UIPanGestureRecognizer *)pan {
    // 取得手勢的位移
    CGPoint tranP = [pan translationInView:self.view];
    CGFloat offsetX = tranP.x;

    // 依位移更新主視圖的 frame(含縮放)
    _mainV.frame = [self frameWithOffsetX:offsetX];

    // 依位置切換左右頁的顯示狀態
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];

    [pan setTranslation:CGPointZero inView:self.view];   // 復位

    // 手勢結束時吸附到定位點
    if (pan.state == UIGestureRecognizerStateEnded) {
        CGFloat target = 0;
        if (_mainV.frame.origin.x > screenW * 0.5) {
            target = targetR;                                  // 滑得夠多,定位到右邊頁
        } else if (CGRectGetMaxX(_mainV.frame) < screenW * 0.5) {
            target = targetL;                                  // 定位到左邊頁
        }
        CGFloat offsetX = target - _mainV.frame.origin.x;
        [UIView animateWithDuration:0.25 animations:^{
            _mainV.frame = (target == 0) ? self.view.bounds : [self frameWithOffsetX:offsetX];
        }];
    }
}

- (void)tap {
    [UIView animateWithDuration:0.25 animations:^{
        _mainV.frame = self.view.bounds;
    }];
}

三、依水平位移計算縮放後的 frame

這是讓抽屜「有層次感」的關鍵:主頁往側邊移動的同時逐漸變小,形成被推入景深的效果。

#define kMaxY 80

#pragma mark - 根據 offsetX 計算 mainV 的 frame

- (CGRect)frameWithOffsetX:(CGFloat)offsetX {
    CGRect frame = _mainV.frame;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;

    // X 軸每移動一點,Y 軸(高度變化)需要對應移動的距離
    CGFloat offsetY = offsetX * kMaxY / screenW;

    CGFloat preH = frame.size.height;
    CGFloat preW = frame.size.width;

    // 往右拖時高度變小,往左拖時高度變大
    CGFloat curH = preH - 2 * offsetY;
    if (frame.origin.x < 0) {
        curH = preH + 2 * offsetY;
    }

    // 依高度比例縮放寬度,保持不變形
    CGFloat scale = curH / preH;
    CGFloat curW = preW * scale;

    frame.origin.x += offsetX;
    frame.origin.y = (screenH - curH) / 2;    // 垂直居中
    frame.size.width  = curW;
    frame.size.height = curH;
    return frame;
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSString *, id> *)change
                       context:(void *)context {
    if (_mainV.frame.origin.x > 0) {
        _rightV.hidden = YES;      // 往右滑,隱藏右頁
    } else if (_mainV.frame.origin.x < 0) {
        _rightV.hidden = NO;       // 往左滑,顯示右頁
    }
}

四、在主視圖中放入 tableView

建立一個 TableViewController 負責列表資料:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", (long)indexPath.row];
    return cell;
}

再建立一個在 storyboard 中顯示的 XXMainViewController,繼承自已完成抽屜效果的 ViewController,並在 storyboard 中把它的 Custom Class 改為這個類別,同時把列表控制器設為它的子控制器:

- (void)viewDidLoad {
    [super viewDidLoad];

    XXViewController *vc = [[XXViewController alloc] init];
    vc.view.frame = self.view.bounds;
    // 讓 vc 成為主視圖控制器的子控制器
    [self addChildViewController:vc];
    // 把列表掛到主視圖上
    [self.mainV addSubview:vc.view];
}

五、用 readonly 保護子視圖

為了讓 XXMainViewController 只能讀取 mainV 而不能改寫,把子視圖宣告在 .h 中並標記為唯讀:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, weak, readonly) UIView *mainV;
@property (nonatomic, weak, readonly) UIView *leftV;
@property (nonatomic, weak, readonly) UIView *rightV;

@end

執行效果:

這個案例同時示範了幾個實務上很常用的小技巧:手勢結束後的「吸附」判斷、用 setTranslation: 復位手勢位移、以及在 .h 中以 readonly 暴露子視圖給子類使用。抽屜效果的核心其實不複雜,真正需要仔細調的是縮放比例與吸附閾值這兩個手感參數。

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