額,沒錯。這篇還是手勢和動畫。第一篇是Tap的手勢,第二篇是Pan的手勢,這次是LongPress。
上篇的控制中心的效果,機油說不滿意。太多bug,太多沒處理好。這次必須要我做一個完成仿照控制中心的,我也是醉了。
首先
因為是要仿照鎖屏界面下地打開控制中心的流程,首先我們需要一個黑漆漆的背景。恩,然后一顆白白的按鈕!
Paste_Image.png
和鎖屏界面對比下:
Paste_Image.png
反正仿的,沒必要太較真嘛~
首先就是模仿點在上面的時候彈出一條。
MainCenterDisplay1.gif
其實就是判斷手勢,長按的時候就動作,else就恢復到原位。
因為是一次完成再來寫的,代碼在最后放出。反正做得也很渣的說。
之后就是拖動的時候,下面的View跟著移動。
MainCenterDisplay2.gif
嘛,就是這樣。最后就是給一個判斷,向上向下添加個方向,拖動多少就自動往上覆蓋之類。
看看代碼就懂了,就是三個手勢狀態的判斷并且事件處理。
仿
因為畢竟是仿的,差不多就可以了!
最后放上代碼。
#import "BaseCenterViewController.h"
#import "CommonHandler.h"
#define GAP_DEFAULT 15
#define WIDTH_BUTTON 40
#define HEIGHT_BUTTON 10
#define HEIGHT_DISPLAY_VIEW 35
@interface BaseCenterViewController ()
@property (nonatomic, strong) UILabel *lb_base;
@property (nonatomic, strong) UIView *view_temp;
@property (nonatomic, assign) BOOL isUpDirection;
@property (nonatomic, assign) CGPoint point_lastLocation;
@end
@implementation BaseCenterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setupBaseButton];
}
- (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)setupBaseButton{
self.isUpDirection = NO;
self.point_lastLocation = CGPointMake(self.view.frame.origin.x, self.view.frame.size.height);
self.lb_base = [[UILabel alloc]init];
self.lb_base.layer.backgroundColor = [CommonHandler getColorWithRed:215 andGreen:215 andBlue:215 andAlpha:1].CGColor;
self.lb_base.layer.cornerRadius = 5.0f;
[self.lb_base setFrame:CGRectMake(self.view.frame.size.width - GAP_DEFAULT - WIDTH_BUTTON, self.view.frame.size.height - GAP_DEFAULT - HEIGHT_BUTTON, WIDTH_BUTTON, HEIGHT_BUTTON)];
[self.view addSubview:self.lb_base];
UILongPressGestureRecognizer *gesture_long = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(doLongPressed:)];
gesture_long.minimumPressDuration = 0.05f;
gesture_long.numberOfTouchesRequired = 1;
self.lb_base.userInteractionEnabled = YES;
[self.lb_base addGestureRecognizer:gesture_long];
}
- (void)doLongPressed:(UILongPressGestureRecognizer *)gesture{
if(!self.view_temp){
self.view_temp = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - HEIGHT_NAVIGATIONBAR_STATUSBAR - HEIGHT_BOTTOMBAR * 2)];
[self.view_temp setAlpha:0.75f];
}
if(gesture.state == UIGestureRecognizerStateBegan){
// NSLog(@"%s || long pressed's been actived.", __FUNCTION__ );
self.view_temp.backgroundColor = [UIColor grayColor];
// [self.view addSubview:self.view_temp];
[self.view insertSubview:self.view_temp belowSubview:self.lb_base];
[UIView animateWithDuration:0.1f animations:^{
self.view_temp.transform = CGAffineTransformMakeTranslation(self.view.frame.origin.x, -HEIGHT_DISPLAY_VIEW);
}];
}else if(gesture.state == UIGestureRecognizerStateChanged){
CGPoint point_location = [gesture locationInView:self.view];
if(point_location.y < self.point_lastLocation.y){
self.isUpDirection = YES;
}else{
self.isUpDirection = NO;
}
// NSLog(@"%s || long pressed's moving. and location in view:%@ and Direction:%i.", __FUNCTION__, NSStringFromCGPoint(point_location), self.isUpDirection);
if(point_location.y > self.view.frame.size.height - self.view_temp.frame.size.height + 10){
CGRect rect_view_temp = self.view_temp.frame;
rect_view_temp.origin = CGPointMake(self.view.frame.origin.x, point_location.y - 10);
[UIView animateWithDuration:0.1f animations:^{
[self.view_temp setFrame:rect_view_temp];
[self.lb_base setFrame:CGRectMake(self.lb_base.frame.origin.x, point_location.y, WIDTH_BUTTON, HEIGHT_BUTTON)];
}];
}
self.point_lastLocation = point_location;
}else{
// NSLog(@"%s || long pressed's been done.", __FUNCTION__ );
if(self.view_temp){
if(self.view_temp.frame.origin.y < self.view.frame.size.height - HEIGHT_DISPLAY_VIEW * 2 && self.isUpDirection){
[UIView animateWithDuration:0.25f animations:^{
[self.view_temp setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.size.height - self.view_temp.frame.size.height, self.view.frame.size.width, self.view_temp.frame.size.height)];
[self.lb_base setFrame:CGRectMake(self.view.frame.size.width - GAP_DEFAULT - WIDTH_BUTTON, self.view_temp.frame.origin.y + 10, WIDTH_BUTTON, HEIGHT_BUTTON)];
}];
}else{
[UIView animateWithDuration:0.25f animations:^{
[self.view_temp setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.size.height, self.view_temp.frame.size.width, self.view_temp.frame.size.height)];
[self.lb_base setFrame:CGRectMake(self.view.frame.size.width - GAP_DEFAULT - WIDTH_BUTTON, self.view.frame.size.height - GAP_DEFAULT - HEIGHT_BUTTON, WIDTH_BUTTON, HEIGHT_BUTTON)];
}completion:^(BOOL finished){
[self.view_temp removeFromSuperview];
self.view_temp = nil;
}];
}
}
}
}
@end