IOS抽屜效果的實現
抽屜檢視實現的思路
UIViewController 控制著一個 左邊的抽屜檢視(LeftViewController)
同時控制著一個UITabBarController.然後tabBarController就按我們平時做專案那樣做一個三級控制器。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//建立視窗
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//設定背景顏色
_window.backgroundColor = [UIColor whiteColor];
//顯示window
[_window makeKeyAndVisible];
BaseTabBarController *baseVC = [[BaseTabBarController alloc]init];
LeftViewController *leftVC = [[LeftViewController alloc]init];
RootViewController *rootVC = [[RootViewController alloc] initWithLeftViewController:leftVC withCenterViewController:baseVC];
_window.rootViewController = rootVC;
return YES;
}
//這裡指的是上面那個控制所有控制器的根控制器(我建立的是叫做RootViewController的類)
RootViewController.h檔案
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
- (instancetype)initWithLeftViewController:(UIViewController *)leftVC
withCenterViewController:(UIViewController *)centerVC;
- (void)closeLeftViewController:(UIViewController *)parentViewController;
@end
RootViewController.m檔案
- (instancetype)initWithLeftViewController:(UIViewController *)leftVC
withCenterViewController:(UIViewController *)centerVC {
self = [super init];
if (self != nil) {
//新增子檢視和子控制器
[self addChildViewController:leftVC];
leftVC.view.frame = CGRectMake(-200, 0, 200, self.view.bounds.size.height);
[self.view addSubview:leftVC.view];
[self addChildViewController:centerVC];
[self.view addSubview:centerVC.view];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
}
return self;
}
#pragma mark - 手勢
- (void)panAction:(UIPanGestureRecognizer *)pan {
/*
* 1.讓抽屜檢視的平移量跟隨著手指的x軸偏移量相同
* 2.判斷條件:
* ①是否在滑動的過程中大於等於(>=)某個臨界值,這裡我的臨界值是200(左邊抽屜檢視的寬度)
* 1>條件成立這個臨界值就讓當前的偏移量 = 這個臨界值
* ②再次判斷手勢的狀態是否結束,並且手指的偏移量是否小於這個臨界值200
* 1>條件成立就讓當前手指的x軸偏移量為0
*
* 整個過程就是滑動量到達200時才顯示抽屜檢視沒達到200就不顯示
*/
CGPoint p = [pan translationInView:pan.view];
[UIView animateWithDuration:0.3 animations:^{
for (UIView *subView in self.view.subviews) {
CGPoint p1 = p;
if (p1.x >= 200) {
p1.x = 200;
}else if (pan.state == UIGestureRecognizerStateEnded && p1.x < 200){
p1.x = 0;
}
subView.transform = CGAffineTransformMakeTranslation(p1.x, 0);
}
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:0.3 animations:^{
for (UIView *subView in self.view.subviews) {
subView.transform = CGAffineTransformIdentity;
}
}];
}
#pragma mark - 關閉左邊的抽屜
- (void)closeLeftViewController:(UIViewController *)parentViewController {
[UIView animateWithDuration:0.3 animations:^{
for (UIView *subView in parentViewController.view.subviews) {
subView.transform = CGAffineTransformIdentity;
}
}];
LeftViewController.m檔案
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"點選" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, 100, 70, 70);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(btnAc:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnAc:(UIButton *)btn {
RootViewController *root = [[RootViewController alloc]init];
TestViewController *test = [[TestViewController alloc]init];
//先關閉左邊的抽屜檢視
[root closeLeftViewController:self.parentViewController];
//要先pop到當前的根檢視
[self.nv popToRootViewControllerAnimated:NO];
//push到你想要的控制器
[self.nv pushViewController:test animated:NO];
}
相關文章
- Flutter 類抽屜效果動畫實現。Flutter動畫
- flutter上拉抽屜效果,flutter拖動抽屜效果Flutter
- iOS 抽獎輪盤效果實現思路iOS
- 安卓導航抽屜 Navigation Drawer 實現沉浸通知欄安卓Navigation
- JavaScript實現隨機抽獎效果JavaScript隨機
- 【Unity】(UI)抽屜式摺疊皮膚UnityUI
- Flutter - Drawer 抽屜檢視與自定義headerFlutterHeader
- iOS 類知乎”分頁”效果的實現?iOS
- 仿百度地圖上拉下滑抽屜盒地圖
- iOS實現音訊進度條效果iOS音訊
- selenium 如何確定一個抽屜全部拉取出來了?
- 在 iOS 裡 100% 還原 Sketch 實現的陰影效果iOS
- 如何實現 iOS 16 帶來的 Depth Effect 圖片效果iOS
- flutter好用的輪子推薦九-flutter可定製的上下滑出抽屜Flutter
- flutter好用的輪子推薦十二-flutter一個全屏動效的抽屜元件Flutter元件
- JavaScript抽獎效果詳解JavaScript
- 測試開發【提測平臺】分享10-Element UI抽屜和表單校驗&增改介面合併實現應用管理UI
- 微信小程式翻牌抽獎效果微信小程式
- [Hive]Hive實現抽樣查詢Hive
- 實現聚焦效果
- 按照獎品概率分佈抽獎的實現概率分佈
- Java實現隨機抽獎的方法有哪些Java隨機
- canvas實現 漂亮的下雨效果Canvas
- Elasticsearch實現Mysql的Like效果ElasticsearchMySql
- iOS開發UI篇--使用UICollectionView實現一個傾斜列表效果iOSUIView
- 基於ARKit的iOS無限屏實現,還原錘子釋出會效果iOS
- iOS開發UI篇--使用UICollectionView實現一個列表頭部拉伸效果的案例iOSUIView
- css 實現打分效果CSS
- webgl實現火焰效果Web
- webgl實現故障效果Web
- js實現打字效果JS
- canvas實現波浪效果Canvas
- C#實現的簡單的隨機抽號器C#隨機
- 毛玻璃效果在Android的實現Android
- 實現元素的淡入淡出效果
- CSS實現漂亮的小水球效果CSS
- 自定義實現MIUI的拖動視差效果(阻尼效果)UI
- iOS - 對 block 實現的探究iOSBloC
- CSS實現鏤空效果CSS