iOS實(shí)現(xiàn)抽屜效果
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)抽屜效果的具體代碼,供大家參考,具體內(nèi)容如下
抽屜效果:

#import "DragerViewController.h"
#define screenW [UIScreen mainScreen].bounds.size.width
@interface DragerViewController ()
@property (nonatomic, weak) UIView *leftV;
@property (nonatomic, weak) UIView *rightV;
@property (nonatomic, weak) UIView *mainV;
@end
@implementation DragerViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加子控件
[self setUp];
//添加手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.mainV addGestureRecognizer:pan];
//給控制器的View添加點(diǎn)按手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
[self.view addGestureRecognizer:tap];
}
- (void)tap{
//讓MainV復(fù)位
[UIView animateWithDuration:0.5 animations:^{
self.mainV.frame = self.view.bounds;
}];
}
#define targetR 275
#define targetL -275
- (void)pan:(UIPanGestureRecognizer *)pan{
//獲取偏移量
CGPoint transP = [pan translationInView:self.mainV];
//為什么不使用transform,是因?yàn)槲覀冞€要去修改高度,使用transform,只能修改,x,y
//self.mainV.transform = CGAffineTransformTranslate(self.mainV.transform, transP.x, 0);
self.mainV.frame = [self frameWithOffsetX:transP.x];
//判斷拖動(dòng)的方向
if(self.mainV.frame.origin.x > 0){
//向右
self.rightV.hidden = YES;
}else if(self.mainV.frame.origin.x < 0){
//向左
self.rightV.hidden = NO;
}
//當(dāng)手指松開時(shí),做自動(dòng)定位.
CGFloat target = 0;
if (pan.state == UIGestureRecognizerStateEnded) {
if (self.mainV.frame.origin.x > screenW * 0.5 ) {
//1判斷在右側(cè)
//當(dāng)前View的x有沒有大于屏幕寬度的一半,大于就是在右側(cè)
target = targetR;
}else if(CGRectGetMaxX(self.mainV.frame) < screenW * 0.5){
//2.判斷在左側(cè)
//當(dāng)前View的最大的x有沒有小于屏幕寬度的一半,小于就是在左側(cè)
target = targetL;
}
//計(jì)算當(dāng)前mainV的frame.
CGFloat offset = target - self.mainV.frame.origin.x;
[UIView animateWithDuration:0.5 animations:^{
self.mainV.frame = [self frameWithOffsetX:offset];
}];
}
//復(fù)位
[pan setTranslation:CGPointZero inView:self.mainV];
}
#define maxY 100
//根據(jù)偏移量計(jì)算MainV的frame
- (CGRect)frameWithOffsetX:(CGFloat)offsetX {
NSLog(@"offsetX===%f",offsetX);
CGRect frame = self.mainV.frame;
NSLog(@"x====%f",frame.origin.x);
frame.origin.x += offsetX;
//當(dāng)拖動(dòng)的View的x值等于屏幕寬度時(shí),maxY為最大,最大為100
// 375 * 100 / 375 = 100
//對計(jì)算的結(jié)果取絕對值
CGFloat y = fabs( frame.origin.x * maxY / screenW);
frame.origin.y = y;
//屏幕的高度減去兩倍的Y值
frame.size.height = [UIScreen mainScreen].bounds.size.height - (2 * frame.origin.y);
return frame;
}
- (void)setUp{
//leftV
UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];
leftV.backgroundColor = [UIColor blueColor];
self.leftV = leftV;
[self.view addSubview:leftV];
//rightV
UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];
rightV.backgroundColor = [UIColor greenColor];
self.rightV = rightV;
[self.view addSubview:rightV];
//mianV
UIView *mainV = [[UIView alloc] initWithFrame:self.view.bounds];
mainV.backgroundColor = [UIColor redColor];
self.mainV = mainV;
[self.view addSubview:mainV];
}
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS中長條藍(lán)色按鈕(button)實(shí)現(xiàn)代碼
本文通過實(shí)例代碼給大家介紹了iOS中長條藍(lán)色按鈕(button)實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-08-08
iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時(shí)傳輸
這篇文章主要為大家詳細(xì)介紹了iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時(shí)傳輸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
iOS中setValue和setObject的區(qū)別詳解
setObject:ForKey: 是NSMutableDictionary特有的;setValue:ForKey:是KVC的主要方法。接下來通過本文給大家分享iOS中setValue和setObject的區(qū)別,需要的朋友參考下2017-02-02
通過UIKit坐標(biāo)系來全面掌握iOS中的UIScrollView組件
iOS開發(fā)套件中的UIScrollView組件十分強(qiáng)大,不僅是滾動(dòng),縮放操作也能夠控制自如,其核心當(dāng)然是坐標(biāo)軸上的控制,下面就通過UIKit坐標(biāo)系來全面掌握iOS中的UIScrollView組件2016-05-05
詳談iOS 位置權(quán)限彈出框閃現(xiàn)的問題
下面小編就為大家?guī)硪黄斦刬OS 位置權(quán)限彈出框閃現(xiàn)的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
解決Alamofire庫在iOS7下設(shè)置Head無效的問題
本文主要介紹Alamofire庫在iOS下設(shè)置Head,這里通過代碼實(shí)例解決不同版本的IOS系統(tǒng)出現(xiàn)的問題,有需要的小伙伴可以參考下2016-07-07
iOS利用UIBezierPath + CAAnimation實(shí)現(xiàn)路徑動(dòng)畫效果
在iOS開發(fā)中,制作動(dòng)畫效果是最讓開發(fā)者享受的環(huán)節(jié)之一,這篇文章主要給大家介紹了關(guān)于iOS利用UIBezierPath + CAAnimation實(shí)現(xiàn)路徑動(dòng)畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10

