画面の右側から左にスワイプするジェスチャで非表示のUIViewを表示し、同じジェスチャ(今回は右)で再び非表示にすることができます。トランジションを追加します。
.hファイルにBOOLdidNotSwipeを追加します。.mファイルにその値didNotSwipe=TRUEを追加します。viewDidLoadメソッド
self.viewに、異なるセレクターを使用して左右方向にスワイプジェスチャを追加します。
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer *recognizer1;
recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeleft:)];
[recognizer1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:recognizer1];
[recognizer1 release];
左にスワイプすると、このメソッドが呼び出されます。
-(void)swipeleft:(UISwipeGestureRecognizer *)swipeGesture
{
if (didNotSwipe) {
didNotSwipe = FALSE;
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setDuration:0.50];
[animation setTimingFunction:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.layer addAnimation:animation forKey:kCATransition];
[self.view addSubView:self.overlayView];
//[self.overlayView setFrame:CGRectMake(0,0,self.overlayView.frame.size.width,self.overlayView.frame.size.height)];
}
}
この方法を右にスワイプすると:
-(void)swipeRight:(UISwipeGestureRecognizer *)swipeGesture
{
if(!didNotSwipe){
didNotSwipe = TRUE;
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setDuration:0.40];
[animation setTimingFunction:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.layer addAnimation:animation forKey:kCATransition];
[self.overlayView removeFromSuperView];
}
}