一看副标题,是不是很熟悉。恩,居然还是手势和动画!不过这次是第二节了!内容跟之前的是不同的喔。
哪里不同?上次是点击之后播放动画,这次嘛,是拖动的时候播放动画。虽然没太大不同,但是在尝试的时候也是花了不少时间啊。
项目需要,要做一个类似iOS自带的控制中心的弹出式抽屉,因为为了贯彻尽量自己写自己研究,所以嘛,这次也是直接写好就放上来了,也没太多要点,就是在拖动的时候对手势的判断,手势所在点的处理。
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
至于里面那个DrawerViewController是为了以后拓展用的,恩。
然后就是效果图:
其实还是很简单的,而且效果也一般般~大牛请轻喷!
网友评论