一看副標(biāo)題怖喻,是不是很熟悉底哗。恩,居然還是手勢(shì)和動(dòng)畫(huà)罢防!不過(guò)這次是第二節(jié)了艘虎!內(nèi)容跟之前的是不同的喔。
哪里不同咒吐?上次是點(diǎn)擊之后播放動(dòng)畫(huà)野建,這次嘛,是拖動(dòng)的時(shí)候播放動(dòng)畫(huà)恬叹。雖然沒(méi)太大不同候生,但是在嘗試的時(shí)候也是花了不少時(shí)間啊。
項(xiàng)目需要绽昼,要做一個(gè)類(lèi)似iOS自帶的控制中心的彈出式抽屜唯鸭,因?yàn)闉榱素瀼乇M量自己寫(xiě)自己研究,所以嘛硅确,這次也是直接寫(xiě)好就放上來(lái)了目溉,也沒(méi)太多要點(diǎn),就是在拖動(dòng)的時(shí)候?qū)κ謩?shì)的判斷菱农,手勢(shì)所在點(diǎn)的處理缭付。
DrawerCenterViewController.h
#import <UIKit/UIKit.h>
@interface DrawCenterViewController : UIViewController
@end
頭文件一如既往空空如也……
DrawerCenterViewController.m
#import "DrawCenterViewController.h"
#import "DrawerViewController.h"
@interface DrawCenterViewController ()
@property (nonatomic, strong) DrawerViewController *instance_drawerView;
@property (nonatomic, strong) UIView *view_cover;
@end
@implementation DrawCenterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initDrawerView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)initGesture{
UIPanGestureRecognizer *gesture_pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(showDrawer:)];
[self.view addGestureRecognizer:gesture_pan];
UITapGestureRecognizer *gesture_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissDrawer:)];
[self.view_cover addGestureRecognizer:gesture_tap];
}
- (void)initDrawerView{
self.view_cover = [[UIView alloc]init];
self.view_cover.backgroundColor = [UIColor blackColor];
[self.view_cover setAlpha:0];
[self.view_cover setFrame:self.view.bounds];
[self.view addSubview:self.view_cover];
self.instance_drawerView = [[DrawerViewController alloc]init];
[self.instance_drawerView.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.size.height, self.view.frame.size.width, 250)];
self.instance_drawerView.view.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.instance_drawerView.view];
[self initGesture];
}
- (void)showDrawer:(UIPanGestureRecognizer *)gesture{
CGPoint translation;
if (gesture.state == UIGestureRecognizerStateChanged) {
translation = [gesture translationInView:self.view];
if (translation.y < -15 && translation.y > -self.instance_drawerView.view.frame.size.height) {
[UIView animateWithDuration:0.15f animations:^{
self.instance_drawerView.view.transform = CGAffineTransformMakeTranslation(self.instance_drawerView.view.frame.origin.x, translation.y);
[self.view_cover setAlpha:0.2];
}];
}
}else if(gesture.state==UIGestureRecognizerStateEnded){
if(self.instance_drawerView.view.frame.origin.y <= (self.view.frame.size.height - (self.instance_drawerView.view.frame.size.height / 2))){
[UIView animateWithDuration:0.5f animations:^{
self.instance_drawerView.view.transform = CGAffineTransformMakeTranslation(self.instance_drawerView.view.frame.origin.x, -self.instance_drawerView.view.frame.size.height);
}];
} else {
[UIView animateWithDuration:0.5f animations:^{
self.instance_drawerView.view.transform = CGAffineTransformMakeTranslation(self.instance_drawerView.view.frame.origin.x, self.view.frame.size.height - self.instance_drawerView.view.frame.origin.y);
[self.view_cover setAlpha:0];
}];
}
}
}
- (void)dismissDrawer:(UITapGestureRecognizer *)gesture{
[UIView animateWithDuration:0.35f animations:^{
self.instance_drawerView.view.transform = CGAffineTransformMakeTranslation(self.instance_drawerView.view.frame.origin.x, self.view.frame.size.height - self.instance_drawerView.view.frame.origin.y);
[self.view_cover setAlpha:0];
}];
}
@end
至于里面那個(gè)DrawerViewController是為了以后拓展用的,恩循未。
然后就是效果圖:
其實(shí)還是很簡(jiǎn)單的陷猫,而且效果也一般般~大牛請(qǐng)輕噴!