iOS 抽屜效果
抽屜效果思路:
三個View疊加,一個作為左View,一個作為右View,一個主View,在主View上新增拖動手勢,修改主View的frame以顯示左View和右View,設定分界值和左/右邊界值;根據響應者鏈,將主View回收的tap手勢,新增到self.view 上最合適
//
// ViewController.m
// 抽屜效果
//
// Created by Captain on 2017/7/29.
// Copyright © 2017年 CaptainSir. All rights reserved.
//
#import "ViewController.h"
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (nonatomic, weak) UIView * leftView;
@property (nonatomic, weak) UIView * rightView;
@property (nonatomic, weak) UIView * mainView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addViews];
[self setupViews];
}
- (void)addViews{
// 左側View
UIView * leftV = [[UIView alloc]init];
leftV.backgroundColor = [UIColor greenColor];
[self.view addSubview:leftV];
// 右側view
UIView * rightV = [[UIView alloc]init];
rightV.backgroundColor = [UIColor yellowColor];
[self.view addSubview:rightV];
// 主View
UIView * mainV = [[UIView alloc]init];
mainV.backgroundColor = [UIColor redColor];
[self.view addSubview:mainV];
leftV.frame = rightV.frame = mainV.frame = self.view.frame;
self.leftView = leftV;
self.rightView = rightV;
self.mainView = mainV;
}
- (void)setupViews{
// 主View拖動手勢
UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(mainViewPanAction:)];
[self.mainView addGestureRecognizer:panGes];
// 點選self.view tap手勢回收mainView
UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mainViewRecoveryTapAction)];
[self.view addGestureRecognizer:tapGes];
}
#define BoundaryValue [UIScreen mainScreen].bounds.size.width * 0.5 // 位置分界值
#define TargetRight [UIScreen mainScreen].bounds.size.width - 50 // 右側最遠距離
#define TargetLeft 50 // 左側最遠距離
- (void)mainViewPanAction:(UIPanGestureRecognizer *) panGes{
// 獲取手勢在self.mainView上的偏移量
CGPoint point = [panGes translationInView:self.mainView];
// 修改self.mainView的frame
[self changeMainViewFrameWithOffsetX:point.x];
// 復原手勢在self.mainView上的偏移量,防止疊加偏移
[panGes setTranslation:CGPointZero inView:self.mainView];
if (panGes.state == UIGestureRecognizerStateEnded) {
// 設定邊界距離
CGFloat margin = 0;
if (self.mainView.frame.origin.x > BoundaryValue) {
margin = TargetRight - self.mainView.frame.origin.x;
}else if (CGRectGetMaxX(self.mainView.frame) < BoundaryValue) {
margin = TargetLeft - CGRectGetMaxX(self.mainView.frame);
}else {
margin = -self.mainView.frame.origin.x;
}
[self changeMainViewFrameWithOffsetX: margin];
}
}
#define MarginY 80
// 根據偏移量計算self.mainView的frame
- (void)changeMainViewFrameWithOffsetX:(CGFloat)offsetX{
CGRect frame = self.mainView.frame;
// X 值
frame.origin = CGPointMake(frame.origin.x + offsetX, 0);
// Y 值
frame.origin.y = fabs((frame.origin.x / screenW) * MarginY);
// H 值
frame.size.height = screenH - (2 * frame.origin.y);
self.mainView.frame = frame;
if (self.mainView.frame.origin.x > 0) {
self.rightView.hidden = YES;
}else {
self.rightView.hidden = NO;
}
}
// 回收mainView的frame
- (void)mainViewRecoveryTapAction{
self.mainView.frame = self.view.bounds;
}
@end
相關文章
- Swift iOS : 抽屜效果SwiftiOS
- IOS抽屜效果的實現iOS
- flutter上拉抽屜效果,flutter拖動抽屜效果Flutter
- Flutter 類抽屜效果動畫實現。Flutter動畫
- 玩轉iOS開發 - 簡易的實現2種抽屜效果iOS
- Android開發之DrawerLayout實現抽屜效果Android
- 需要側滑抽屜效果?一行程式碼足以!行程
- Android UI開發第九篇——SlidingDrawer 抽屜效果AndroidUI
- [UI]抽屜選單DrawerLayout分析(二)UI
- 【Unity】(UI)抽屜式摺疊皮膚UnityUI
- iOS實現簡單的抽屜式側欄——MMDrawerController的使用iOSController
- iOS 抽獎輪盤效果實現思路iOS
- Flutter - Drawer 抽屜檢視與自定義headerFlutterHeader
- 仿百度地圖上拉下滑抽屜盒地圖
- 安卓導航抽屜 Navigation Drawer 實現沉浸通知欄安卓Navigation
- 51nod 1103 N的倍數 (抽屜原理)
- JavaScript抽獎效果詳解JavaScript
- 微信小程式翻牌抽獎效果微信小程式
- flutter好用的輪子推薦十二-flutter一個全屏動效的抽屜元件Flutter元件
- flutter好用的輪子推薦九-flutter可定製的上下滑出抽屜Flutter
- JavaScript實現隨機抽獎效果JavaScript隨機
- js不重複抽獎效果程式碼例項JS
- jQuery九宮格抽獎效果程式碼例項jQuery
- 能夠設定中獎概率的抽獎效果
- js實現刮刮樂抽獎效果程式碼例項JS
- iOS 粒子發射效果iOS
- ios 卡片切換效果iOS
- iOS文字漸變效果iOS
- iOS全景效果實現iOS
- iOS複雜動畫之抽絲剝繭(Objective C & Swift)iOS動畫ObjectSwift
- iOS自定義UIView動畫效果iOSUIView動畫
- iOS UITableViewCell的動畫效果(一)iOSUIView動畫
- iOS動畫-擴散波紋效果iOS動畫
- iOS 視訊新增濾鏡效果iOS
- 鄭州iOS·點-毛玻璃效果iOS
- iOS加入購物車動畫效果iOS動畫
- iOS中實現模糊效果教程iOS
- 測試開發【提測平臺】分享10-Element UI抽屜和表單校驗&增改介面合併實現應用管理UI