您的位置:首页 > 移动开发 > IOS开发

学习iOS开发的第19天

2014-03-31 20:16 537 查看
学了一下手势识别器的运用。下面简单地演示一下。

手势包括轻击、捏合、平移、轻扫、旋转、长按等。它们分别由相应类来实现。依次为UITapGestureRecognizer、UIPinchGestureRecognizer、UIPanGestureRecognizer、UISwipeGestureRecognizer、UIRotationGestureRecognizer、UILongPressGestureRecognizer。它们都继承于UIGestureRecognizer类。创建这些类的实例来获取响应的手势事件并会绑定制定的方法,手势会触发相应的方法。创建好手势识别器后,可以将手势识别器添加到view上面。

新建一个项目,然后创建一个继承于UIViewController的视图控制器,在里面分别创建那几个手势识别器。手势识别器也有很多属性可以设置,有兴趣者可以深入研究下。

//轻击识别器
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
[self.view addGestureRecognizer:tap];

//捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch)];
[self.view addGestureRecognizer:pinch];

//平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan)];
[self.view addGestureRecognizer:pan];

//轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
//设置方向
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipe];

//旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation)];
[self.view addGestureRecognizer:rotation];

//长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
//设置长按最小时间
longPress.minimumPressDuration = 1;
[self.view addGestureRecognizer:longPress];
下面是各个手势所触发的方法。
-(void)tap{
NSLog(@"轻击");
}
-(void)pinch{
NSLog(@"捏合");
}
-(void)pan{
NSLog(@"平移");
}
-(void)swipe{
NSLog(@"轻扫");
}
-(void)rotation{
NSLog(@"旋转");
}
-(void)longPress{
NSLog(@"长按");
}

最后在应用程序的代理类里创建视图控制器。
TouchViewController *controller = [[TouchViewController alloc] init];
self.window.rootViewController = controller;


运行程序,在屏幕上做出各种手势,会输出相应手势的名称。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: