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

iOS学习笔记——手势(Gesture)

2013-12-30 09:26 274 查看
手势是指你用一个或多个手指接触屏幕开始,直到你的手指全部离开屏幕为止所发生的所有事件。手势识别器(UIGestureRecognizer)是一个对象,知道如何观察用户生成的事件流,并识别用户何时以与预定义的手势相匹配的方式进行拉触摸和拖动。UIGestureRecognizer类封装了查找手势的工作。在模拟器中,按“option”键,可模拟两个手指的手势。

在.h文件里创建一个视图对象,此对象是模拟拖动手势时的目标:

@interface LinViewController : UIViewController
//创建视图对象
@property (retain, nonatomic) UIView * mView;

@end
在.m文件里,创建各个手势的对象,编写各个手势的实现方法:

@implementation LinViewController

- (void)viewDidLoad
{
[super viewDidLoad];

//---点击手势识别器---//
//创建点击手势的对象
UITapGestureRecognizer * pSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];
//将手势添加到当前视图中
[self.view addGestureRecognizer:pSingleTap];

//创建点击手势的对象
UITapGestureRecognizer * pDoubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];
//设置点击次数为2次,默认为1次,属于单击操作
pDoubleTap.numberOfTapsRequired = 2;
//将手势添加到当前视图中
[self.view addGestureRecognizer:pDoubleTap];
//双击的时候让单击放弃响应
[pSingleTap requireGestureRecognizerToFail:pDoubleTap];
//释放创建的对象
[pDoubleTap release];
[pSingleTap release];

//---滑动手势识别器---//
//创建滑动手势的对象
UISwipeGestureRecognizer * pSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];
//设置滑动的方向,此处为向右,共有4个方向
pSwip.direction = UISwipeGestureRecognizerDirectionRight;
//将手势添加到当前视图中
[self.view addGestureRecognizer:pSwip];
//释放创建的对象
[pSwip release];

//---拖动手势识别器--//
//创建一个拖动手势动作的目标,假定为一个视图块
self.mView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
self.mView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.mView];

//创建拖动手势对象
UIPanGestureRecognizer * pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
//将手势添加到当前视图中
[self.view addGestureRecognizer:pPan];
//释放创建的对象
[pPan release];

//---长按手势识别器--//
//创建长按手势的对象
UILongPressGestureRecognizer * pLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
//设置长按的时间,2秒以上判定为长按
pLongPress.minimumPressDuration = 2;
//将手势添加到当前视图中
[self.view addGestureRecognizer:pLongPress];
//释放创建的对象
[pLongPress release];

//---旋转手势识别器---//
//创建旋转手势对象
UIRotationGestureRecognizer * pRotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
//将手势添加到当前视图中
[self.view addGestureRecognizer:pRotation];
//释放创建的对象
[pRotation release];
}
#pragma mark--------tapGesture>>>single  单击
- (void)singleGesture:(UITapGestureRecognizer *)tap
{
//单击手势对应的操作
NSLog(@"单击操作");
}
#pragma mark--------tapGesture>>>double  双击
- (void)doubleGesture:(UITapGestureRecognizer *)tap
{
//双击手势对应的操作
NSLog(@"双击操作");
}
#pragma mark--------tapGesture>>>Swip    滑动
- (void)swipGesture:(UISwipeGestureRecognizer *)swip
{
//滑动,划屏手势对应的操作
NSLog(@"滑动操作,分方向,此处向右滑动");
}
#pragma mark--------tapGesture>>>Pan     平移
- (void)panGesture:(UIPanGestureRecognizer *)pan
{
//获取平移手势在视图上的坐标
CGPoint point = [pan locationInView:self.view];
//把视图的中心点确定在平移手势的坐标上,即把视图拖动到平移点
self.mView.center = point;
NSLog(@"%@",NSStringFromCGPoint(point));
}
#pragma mark--------tapGesture>>>LongPress 长按
- (void)longPressGesture:(UILongPressGestureRecognizer *)longPress
{
//长按手势对应的操作
NSLog(@"长按操作");
}
#pragma mark--------tapGesture>>>Rotation  旋转
- (void)rotationGesture:(UIRotationGestureRecognizer *)rotation
{
//确定手势旋转的大小,角度,使视图做出相应的反应
float degree = rotation.rotation *(180/M_PI);
NSLog(@"旋转的角度为%.2f",degree);
}
//释放创建的对象
- (void)dealloc
{
[_mView release];
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: