iOS中肯定用到手势。下面是简单的介绍一下侧滑手势 就是UISwipeGestureRecognizer。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageViewalloc]init];
imageView.backgroundColor = [UIColoryellowColor];
imageView.image = [UIImageimageNamed:@"01"];
CGFloat imageViewW = 300;
CGFloat imageViewH =imageViewW *400/640;
CGFloat imageViewX =(self.view.frame.size.width - imageViewW)/2;
CGFloat imageViewY =200;
imageView.userInteractionEnabled =YES;
imageView.frame =CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
[self.viewaddSubview:imageView];
/*
最好是一个方向写一个手势
*/
//右
UISwipeGestureRecognizer *swipe1= [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
//这是两个手指才能侧滑默认是1个手指
// swipe1.numberOfTouchesRequired = 2;
swipe1.direction=UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipe1];
//左
UISwipeGestureRecognizer *swipe2= [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
swipe2.direction=UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipe2];
//上
UISwipeGestureRecognizer *swipe3= [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
swipe3.direction=UISwipeGestureRecognizerDirectionUp;
[imageView addGestureRecognizer:swipe3];
//下
UISwipeGestureRecognizer *swipe4= [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
swipe4.direction=UISwipeGestureRecognizerDirectionDown;
[imageView addGestureRecognizer:swipe4];
}
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
switch (swipe.direction) {
caseUISwipeGestureRecognizerDirectionRight:
[self makeMove:swipeandX:100andY:0];
NSLog(@"right");
break;
caseUISwipeGestureRecognizerDirectionLeft:
[self makeMove:swipeandX:-100andY:0];
NSLog(@"left");
break;
caseUISwipeGestureRecognizerDirectionUp:
[self makeMove:swipeandX:0andY:-100];
NSLog(@"up");
break;
caseUISwipeGestureRecognizerDirectionDown:
[self makeMove:swipeandX:0andY:100];
NSLog(@"down");
break;
default:
break;
}
}
//添加一个动画让他更明显的显示出侧滑的方向
-(void)makeMove:(UISwipeGestureRecognizer *)swipe andX:(CGFloat)x andY:(CGFloat)y{
CGFloat time = 0.1;
[UIViewanimateWithDuration:timeanimations:^{
swipe.view.transform =CGAffineTransformMakeTranslation(x, y);
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:timeanimations:^{
swipe.view.transform =CGAffineTransformIdentity;
}];
}];
}
@end