使用UIContainerView實現(xiàn)控制器側(cè)滑功能

1、容器視圖實現(xiàn)的側(cè)滑示例圖效果如下所示:

容器視圖側(cè)滑效果.gif

2、代碼部分如下所示:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,weak) IBOutlet UIView *containerView;
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
  
   [self setUpUI];
   
}
// 當(dāng)用戶觸摸屏幕的時候開始給容器視圖添加手勢
-(void)setUpUI{

   UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

   [_containerView addGestureRecognizer:pan];

   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
   
   [_containerView addGestureRecognizer:tap];
}
// 添加點按手勢
-(void)tap:(UITapGestureRecognizer *)sender{
  
   [UIView animateWithDuration:0.25 animations:^{
       _containerView.transform = CGAffineTransformIdentity;
   }];

}
// 添加的拖拽手勢
-(void)pan:(UIPanGestureRecognizer *)sender{
   
   //1、獲取拖拽手勢獲取偏移量
   CGPoint translate = [sender translationInView:_containerView];
   
   // 2、重置 為什么要重置 因為每一次的偏移都是從屏幕最左邊為起始位置
   [sender setTranslation:CGPointZero inView:_containerView];
   
   switch (sender.state) {
    case UIGestureRecognizerStateBegan:
    case UIGestureRecognizerStateChanged:
       {
           // 這樣做的好處就是 translate.x 和 _containerView.frame.origin.x
           CGFloat destX = translate.x + _containerView.frame.origin.x;
          // 限制容器視圖向左移動
           if (destX < 0) {
               return;
           }
           // 設(shè)置容器視圖的水平偏移量 每一次的偏移量都是基于上一次的偏移
           _containerView.transform = CGAffineTransformTranslate(_containerView.transform,translate.x, 0);
        }
           break;
    case UIGestureRecognizerStateEnded:
       {
           // 手勢結(jié)束 判斷偏移量距離屏幕中心的位置
           CGFloat centerX = [UIScreen mainScreen].bounds.size.width * 0.5;
           CGFloat containerX = _containerView.frame.origin.x;
           CGFloat destination = centerX + 40;
           if (centerX < containerX ) {
               [UIView animateWithDuration:0.25 animations:^{
                   
                   _containerView.transform = CGAffineTransformMakeTranslation(destination, 0);
               }];
               break;
           }
#warning - 這里用到的是switch的穿透效果
       }
    case UIGestureRecognizerStateFailed:
    case  UIGestureRecognizerStateCancelled:
       {
           [UIView animateWithDuration:0.25 animations:^{
               // 回到原始位置
               _containerView.transform = CGAffineTransformIdentity;
           }];
       }
           break;
           default:
           break;
     }
}
@end

3、說一下實現(xiàn)的思路

3.1、使用側(cè)滑手勢來拿到容器視圖的偏移,然后實現(xiàn)容器視圖的偏移。
根據(jù)側(cè)滑手勢的狀態(tài)分別對容器視圖的側(cè)滑情況進行設(shè)置。側(cè)滑效果把屏幕寬度的1/2的位置作為分界線。側(cè)滑超過屏幕1/2的位置時候,那么就保持這種抽屜的打開效果,如果沒有超過屏幕跨度1/2的時候,就以動畫的效果關(guān)閉抽屜的效果。同時,不允許容器視圖向左側(cè)滑動。接下來就判斷側(cè)滑手勢的狀態(tài),讓容器視圖到達指定的位置。

3.2、在點擊容器視圖的時候,容器視圖回到原始位置,鋪滿整個屏幕。使用的是點擊手勢(UITApGestureRecognizer手勢)。創(chuàng)建手勢對象,添加到要作用的容器視圖上。在點擊手勢的方法選擇器中,執(zhí)行容器視圖的transform方法,來實現(xiàn)容器視圖的平移效果。同時為了保證平移時不顯得那么生硬,就把平移放在UIView動畫中來執(zhí)行。

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

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

  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標下拉刷新...
    皇小弟閱讀 47,183評論 22 665
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,692評論 4 61
  • 1、此次活動每個孩子必須服從命令聽從指揮,沒得商量。 2、6月28日早6:40準時在學(xué)校門口出發(fā),過時不候,自行解...
    44229閱讀 1,079評論 0 0
  • 從前有個小漁村,天上掉下一條船,打破了原有的自給自足的局面。 小漁村的一位A找到了這艘船,他在思考給自己還是給所有...
    一桿梅子酒_f9c6閱讀 1,248評論 0 0

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