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

iOS 抽屜效果實作:單側與雙側版本程式碼分享

編輯:iOS 相關教學

不依賴第三方框架也能做出抽屜效果,核心就是 UIPanGestureRecognizer 加上 frame 的即時位移。本文提供兩個版本:單側抽屜與雙側抽屜。

一、單側(左側)抽屜

@interface ViewController () {
    UIView *_leftView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _leftView = [[UIView alloc] init];
    // 一開始藏在畫面左側之外
    _leftView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height);
    _leftView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_leftView];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.view addGestureRecognizer:pan];
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    // 加上位移後的 x 座標
    CGFloat Xresult = translation.x + _leftView.frame.origin.x;

    if (translation.x >= 0) {
        // 向右拖:已全部拉出則不再移動
        if (_leftView.frame.origin.x >= 0 || Xresult >= 0) {
            _leftView.frame = CGRectMake(0, 0, 200, self.view.frame.size.height);
            return;
        }
    } else if (translation.x < 0) {
        // 向左拖:已全部收回則不再移動
        if (_leftView.frame.origin.x <= -200 || Xresult <= -200) {
            _leftView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height);
            return;
        }
    }

    CGRect frame = _leftView.frame;
    frame.origin.x += translation.x;
    _leftView.frame = frame;

    // 關鍵:清空已累積的位移量
    [recognizer setTranslation:CGPointZero inView:recognizer.view];

    // 放手後以中線為界做吸附
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        if (_leftView.frame.origin.x > -100) {
            [self closeView:NO];
        } else {
            [self closeView:YES];
        }
    }
}

- (void)closeView:(BOOL)close {
    if (close) {
        [self moveView:CGRectMake(-200, 0, 200, self.view.frame.size.height)];
    } else {
        [self moveView:CGRectMake(0, 0, 200, self.view.frame.size.height)];
    }
}

- (void)moveView:(CGRect)frame {
    [UIView animateWithDuration:0.3 animations:^{
        _leftView.frame = frame;
    } completion:^(BOOL finished) {}];
}

二、雙側抽屜

改成放在自己的 view 裡實作,讓 view 自己改變自己的 frame,並用兩個屬性分別指定左右抽出的寬度;不需要某一側時把它設為 0 即可。

#import "PathView.h"
#import "UIView+Additions.h"

@implementation PathView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupGestureRecognizer];
    }
    return self;
}

- (void)setupGestureRecognizer {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self addGestureRecognizer:pan];
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self];
    CGFloat Xresult = translation.x + self.left;

    if (translation.x >= 0) {
        // 向右
        if (self.left >= _leftWidth || Xresult >= _leftWidth) {
            self.frame = CGRectMake(_leftWidth, 0, self.width, self.height);
            return;
        }
    } else if (translation.x < 0) {
        // 向左
        if (self.left <= -_rightWidth || Xresult <= -_rightWidth) {
            self.frame = CGRectMake(-_rightWidth, 0, self.width, self.height);
            return;
        }
    }

    self.left += translation.x;

    if (recognizer.state == UIGestureRecognizerStateEnded) {
        if (self.left > _leftWidth / 2) {
            [self openLeft:YES openRight:NO];
        } else if (self.left < -(_rightWidth / 2)) {
            [self openLeft:NO openRight:YES];
        } else {
            [self openLeft:NO openRight:NO];
        }
    }

    // 清空位移量
    [recognizer setTranslation:CGPointZero inView:recognizer.view];
}

- (void)openLeft:(BOOL)left openRight:(BOOL)right {
    if (!left && !right) {
        [self moveView:CGRectMake(0, 0, self.width, self.height)];
    } else if (!left && right) {
        [self moveView:CGRectMake(-_rightWidth, 0, self.width, self.height)];
    } else if (left && !right) {
        [self moveView:CGRectMake(_leftWidth, 0, self.width, self.height)];
    }
}

- (void)moveView:(CGRect)frame {
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = frame;
    } completion:^(BOOL finished) {}];
}

兩個容易忽略的關鍵

  • setTranslation:CGPointZero 不可省:手勢的 translation 是「自上次重置以來的累積位移」,若不清零,每一幀都會重複加上整段距離,畫面會飛掉。
  • 記得加上邊界判斷:拖到極限就停止更新,並直接吸附到定位,避免抽屜被拉出畫面。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved