UIScreenEdgePanGestureRecognizer名字很長,而且關於其文檔也是少的的可憐,蘋果官方給的唯一的一個屬性是edges,文檔中的解釋是這樣的:
A UIScreenEdgePanGestureRecognizer looks for panning (dragging) gestures that start near an edge of the screen. The system uses screen edge gestures in some cases to initiate view controller transitions. You can use this class to replicate the same gesture behavior for your own actions.
大概的意思就是UIScreenEdgePanGestureRecognizer跟pan(平移)手勢差不多,需要從邊緣進行拖動,在控制器轉換的時候是有用的,看文檔的話我們會發現UIScreenEdgePanGestureRecognizer是UIPanGestureRecognizer的子類,理解會更方便一點。
UIPanGestureRecognizer鋪墊
先簡單的看下需要實現的視圖控制器的效果:
稍微回顧一下UIPanGestureRecognizer,第一個紅色的視圖我們通過Pan手勢進行操作:
self.panView=[[UIView alloc]initWithFrame:CGRectMake(0, 200, CGRectGetWidth(self.view.bounds), 100)]; [self.panView setBackgroundColor:[UIColor redColor]]; self.panLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, 150, 40)]; [self.panLabel setText:@"博客園-FlyElephant"]; [self.panLabel setFont:[UIFont systemFontOfSize:14]]; [self.panView addSubview:self.panLabel]; [self.view addSubview:self.panView]; UIPanGestureRecognizer *pangestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)]; [self.panView addGestureRecognizer:pangestureRecognizer];
手勢事件:
-(void)panGesture:(UIPanGestureRecognizer *)gesture{ CGPoint translation = [gesture translationInView:gesture.view]; NSLog(@"%@",[NSString stringWithFormat:@"(%0.0f, %0.0f)", translation.x, translation.y]); }
手勢向左滑動的panView的變化:
UIScreenEdgePanGestureRecognizer實戰
第二個視圖我們可以通過UIScreenEdgePanGestureRecognizer進行設置,跟上面的代碼稍微有點重復,如果你有代碼潔癖的話可以考慮將以上代碼進行惰性初始化,可能感官會更好一點,不過為了方便暫時都寫在了一起:
self.centerX=CGRectGetWidth(self.view.bounds)/2; self.edgeView=[[UIView alloc]initWithFrame:CGRectMake(0, 320, CGRectGetWidth(self.view.bounds), 100)]; [self.edgeView setBackgroundColor:[UIColor greenColor]]; self.label=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 320, 40)]; [self.label setText:@"原文地址:http://www.cnblogs.com/xiaofeixiang/"]; [self.label setFont:[UIFont systemFontOfSize:14]]; [self.edgeView addSubview:self.label]; [self.view addSubview:self.edgeView];
注意這個時候手勢是加載view不是單獨的edgeView上的,手勢代碼,edges是一個枚舉,我們可以設置的是響應邊緣右滑事件;
UIScreenEdgePanGestureRecognizer *rightEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightEdgeGesture:)]; rightEdgeGesture.edges = UIRectEdgeRight; // 右滑顯示 [self.view addGestureRecognizer:rightEdgeGesture];
響應邊緣事件的代碼:
//當前被觸摸的view UIView *view = [self.view hitTest:[gesture locationInView:gesture.view] withEvent:nil]; if(UIGestureRecognizerStateBegan == gesture.state || UIGestureRecognizerStateChanged == gesture.state) { CGPoint translation = [gesture translationInView:gesture.view]; [UIView animateWithDuration:0.5 animations:^{ view.center = CGPointMake(self.centerX + translation.x, view.center.y); NSLog(@"%@",NSStringFromCGPoint(view.center)); }]; } else//取消,失敗,結束的時候返回原處 { [UIView animateWithDuration:0.5 animations:^{ view.center = CGPointMake(self.centerX, view.center.y); }]; }
具體效果如下:
如果你細心點會發現那個籃球在滑動介結束的時候轉動了一下,在處理動畫結束的時候加了一個判斷,代碼如下:
if (gesture.state==UIGestureRecognizerStateEnded) { //旋轉360度之后歸0 if(self.currentRadius==360.f){ self.currentRadius=0.0f; } [UIView animateWithDuration:1.0 animations:^{ self.currentRadius += 90.0; self.circleView.transform = CGAffineTransformMakeRotation((self.currentRadius * M_PI) / 180.0); }]; }
如果你想那個籃球一直轉動的話通過NSTimer即可實現:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(transformRotate) userInfo: nil repeats: YES];
轉動的代碼和上面的差不多,不過每次改變的弧度較小:
-(void)transformRotate{ if(self.currentRadius==360.f){ self.currentRadius=0.0f; }else{ self.currentRadius += 10.0; self.circleView.transform = CGAffineTransformMakeRotation((self.currentRadius * M_PI) / 180.0); } }