最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實(shí)現(xiàn)簡(jiǎn)單抽屜效果

 更新時(shí)間:2020年02月22日 08:14:25   作者:vbirdbest  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)單抽屜效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

抽屜效果

所謂抽屜效果就是三個(gè)視圖,向右拖拽顯示左邊的視圖,向左拖拽顯示右邊的視圖,當(dāng)拖拽大于屏幕的一半時(shí)最上面的視圖會(huì)自動(dòng)定位到一邊,當(dāng)點(diǎn)擊左邊或右邊視圖時(shí)會(huì)最上面視圖會(huì)自動(dòng)復(fù)位。

效果如圖:三個(gè)視圖(左邊:淺灰色視圖、右邊:品紅視圖、主視圖:黑色視圖)

封裝代碼

DrawerViewController

#import <UIKit/UIKit.h>
@interface DrawerViewController : UIViewController

@property (weak, nonatomic, readonly) UIView *leftView;
@property (weak, nonatomic, readonly) UIView *rightView;
@property (weak, nonatomic, readonly) UIView *mainView;

@end

// -------------------------------------------------------
#import "DrawerViewController.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define MaxOffsetY 100
#define MaxOffsetX ([UIScreen mainScreen].bounds.size.width - 100)

@implementation DrawerViewController
- (void)viewDidLoad {
 [super viewDidLoad];

 // 1. 初始化視圖
 [self setup];

 // 2. 給mainView添加拖動(dòng)手勢(shì)
 UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
 [self.mainView addGestureRecognizer:panGestureRecognizer];

 // 3. 給self.view添加一個(gè)單擊手勢(shì)
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
 [self.view addGestureRecognizer:tap];
}

- (void)tapGesture:(UITapGestureRecognizer *)tap {
 // mainView復(fù)位
 [UIView animateWithDuration:0.2 animations:^{
  self.mainView.frame = self.view.bounds;
 }];
}

- (void)panGesture:(UIPanGestureRecognizer *)pan {
 CGPoint offsetPoint = [pan translationInView:self.view];
 self.mainView.frame = [self frameWithOffset:offsetPoint.x];

 if (self.mainView.frame.origin.x > 0) {
  // → 右移(顯示leftView)
  self.rightView.hidden = YES;
 } else if (self.mainView.frame.origin.x < 0) {
  // ← 左移(顯示rightView)
  self.rightView.hidden = NO;
 }

 // 如果拖拽結(jié)束,自動(dòng)定位
 CGFloat targetOffsetX = 0;
 if (pan.state == UIGestureRecognizerStateEnded) {
  if (self.mainView.frame.origin.x >= ScreenWidth * 0.5) { // 右側(cè)
   targetOffsetX = MaxOffsetX;
  } else if (CGRectGetMaxX(self.mainView.frame) < ScreenWidth * 0.5){ // 左側(cè)
   targetOffsetX = -MaxOffsetX;
  }

  // 計(jì)算出當(dāng)前位置距離目標(biāo)位置所需要的偏移距離
  CGFloat offsetX = targetOffsetX - self.mainView.frame.origin.x;

  // 滑動(dòng)不到屏幕一般時(shí)仍然顯示mainView(self.view.bounds) 否則自動(dòng)定位到左側(cè)或右側(cè)
  CGRect mainFrame = targetOffsetX == 0 ? self.view.bounds : [self frameWithOffset:offsetX];
  [UIView animateWithDuration:0.2 animations:^{
   self.mainView.frame = mainFrame;
  }];
 }

 [pan setTranslation:CGPointZero inView:self.view];
}

- (CGRect)frameWithOffset:(CGFloat)offsetX {
 CGRect newFrame = self.mainView.frame;
 newFrame.origin.x += offsetX;  // x


 CGFloat offsetY = self.mainView.frame.origin.x * MaxOffsetY / ScreenWidth;
 newFrame.origin.y = fabs(offsetY); // y

 CGFloat offsetHeight = ScreenHeight - (newFrame.origin.y * 2);
 newFrame.size.height = offsetHeight; // height

 return newFrame;
}

- (void)setup {
 UIView *leftView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 //leftView.backgroundColor = [UIColor lightGrayColor];
 _leftView = leftView;
 [self.view addSubview:leftView];

 UIView *rightView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 //rightView.backgroundColor = [UIColor magentaColor];
 _rightView = rightView;
 [self.view addSubview:rightView];

 UIView *mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 //mainView.backgroundColor = [UIColor blackColor];
 _mainView = mainView;
 [self.view addSubview:mainView];
}

@end

使用封裝

1.將DrawerViewController類(lèi)拖入到工程中,并繼承該類(lèi)
2.分別創(chuàng)建LeftViewController、RightViewController、MainViewController
3.將每個(gè)視圖對(duì)應(yīng)的view添加到對(duì)應(yīng)的視圖上,并成為當(dāng)前控制器的子控制器

第一步:繼承DrawerViewController

#import <UIKit/UIKit.h>
#import "DrawerViewController.h"
@interface ViewController : DrawerViewController

@end

第二步:分別創(chuàng)建LeftViewController、RightViewController、MainViewController
第三步:為leftView、rightView、mainView 添加子視圖,并將每天控制器作為當(dāng)前控制器的子控制器

#import "ViewController.h"
#import "LeftViewController.h"
#import "RightViewController.h"
#import "MainViewController.h"

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 // Main
 MainViewController *mainViewController = [[MainViewController alloc] init];
 mainViewController.view.frame = self.view.bounds;
 mainViewController.view.backgroundColor = [UIColor brownColor];
 [self.mainView addSubview:mainViewController.view];
 [self addChildViewController:mainViewController];

 // Left
 LeftViewController *leftViewController = [[LeftViewController alloc] init];
 leftViewController.view.frame = self.view.bounds;
 leftViewController.view.backgroundColor = [UIColor purpleColor];
 [self.leftView addSubview:leftViewController.view];
 [self addChildViewController:leftViewController];

 // Right
 RightViewController *rightViewController = [[RightViewController alloc] init];
 rightViewController.view.frame = self.view.bounds;
 rightViewController.view.backgroundColor = [UIColor cyanColor];
 [self.rightView addSubview:rightViewController.view];
 [self addChildViewController:rightViewController];
}
@end

實(shí)現(xiàn)效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)

    iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • iOS中幾種定時(shí)器的實(shí)現(xiàn)小結(jié)

    iOS中幾種定時(shí)器的實(shí)現(xiàn)小結(jié)

    這篇文章主要介紹了iOS中幾種定時(shí)器的實(shí)現(xiàn)小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • iOS實(shí)現(xiàn)日歷翻頁(yè)動(dòng)畫(huà)

    iOS實(shí)現(xiàn)日歷翻頁(yè)動(dòng)畫(huà)

    本文的內(nèi)容主要是在IOS中實(shí)現(xiàn)日歷翻頁(yè)的動(dòng)畫(huà),界面簡(jiǎn)單但效果很好,以后可以運(yùn)用到app中,下面一起來(lái)看看。
    2016-08-08
  • ios實(shí)現(xiàn)底部PopupWindow的示例代碼(底部彈出菜單)

    ios實(shí)現(xiàn)底部PopupWindow的示例代碼(底部彈出菜單)

    這篇文章主要介紹了ios實(shí)現(xiàn)底部PopupWindow的示例代碼(底部彈出菜單),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • iOS常用組件之高效切圓角的方法匯總

    iOS常用組件之高效切圓角的方法匯總

    最近在研究切圓角的方法,也找了下網(wǎng)上的資料,所以下面這篇文章主要給大家總結(jié)介紹了關(guān)于iOS常用組件之高效切圓角的一些方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • IOS 指紋識(shí)別詳解及實(shí)例代碼

    IOS 指紋識(shí)別詳解及實(shí)例代碼

    這篇文章主要介紹了IOS 指紋識(shí)別詳解的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下
    2016-11-11
  • 分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    近日不忙,給大家分享一個(gè)關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2015-12-12
  • IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果

    IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果

    在我們?nèi)粘i_(kāi)發(fā)IOS中,經(jīng)常見(jiàn)到兩個(gè)tableview的聯(lián)動(dòng),滑動(dòng)一側(cè)tableview,另一側(cè)tableview跟著滑動(dòng),其實(shí)實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,只是需要搞清楚他們之間的區(qū)別和聯(lián)系,下面一起來(lái)看看如何實(shí)現(xiàn)。
    2016-08-08
  • iOS App開(kāi)發(fā)中的UIPageControl分頁(yè)控件使用小結(jié)

    iOS App開(kāi)發(fā)中的UIPageControl分頁(yè)控件使用小結(jié)

    UIPageControl分頁(yè)控件的例子簡(jiǎn)單來(lái)說(shuō)即是我們平時(shí)翻動(dòng)多個(gè)桌面頁(yè)時(shí)及底部帶有的圓點(diǎn)頁(yè)碼標(biāo)注,這里我們來(lái)看一下iOS App開(kāi)發(fā)中的UIPageControl分頁(yè)控件使用小結(jié),需要的朋友可以參考下
    2016-06-06
  • 詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件

    詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件

    這篇文章先是給大家介紹iOS中Button按鈕的狀態(tài),而后又詳細(xì)介紹了iOS中按鈕點(diǎn)擊事件處理方式,本文介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。
    2016-09-09

最新評(píng)論

武隆县| 乌鲁木齐县| 桃园市| 白银市| 融水| 山东省| 东丽区| 南雄市| 兰溪市| 合山市| 洛南县| 四平市| 平阳县| 济源市| 临湘市| 巴中市| 宜君县| 庆阳市| 宝鸡市| 永州市| 临沂市| 连江县| 林口县| 邢台县| 谢通门县| 北川| 汝阳县| 舞阳县| 文成县| 武安市| 兴业县| 论坛| 双江| 舞钢市| 布尔津县| 新龙县| 乌兰察布市| 武夷山市| 皋兰县| 桦南县| 郴州市|