UIGestureRecognize 手势的基类
1.轻拍
2.轻扫
3.长按
4.平移
5.捏合
6.旋转
7.屏幕边缘手势
//第一种手势 轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
//1.设置轻拍次数
tap.numberOfTapsRequired = 1;//默认值是1 轻拍一次
//2.设置手指的个数
tap.numberOfTouchesRequired = 2;//默认值是1
[aView addGestureRecognizer:tap];
[tap release];
*/
/*
/////// //第二种手势 轻扫 最多支持两种手势方向
//需求:向右轻扫改变自身颜色,向左轻扫改变父视图颜色
//1.向右轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
//设置轻扫方向
//默认轻扫方向 向右
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[aView addGestureRecognizer:swipe];
[swipe release];
//2.向左轻扫的手势
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeLeft:) ];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;//方向向左
[aView addGestureRecognizer:leftSwipe];
[leftSwipe release];
*/
/*
/////// 第三种手势 长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
//设置识别长按手势的时间
longPress.minimumPressDuration = 0.5;
[aView addGestureRecognizer:longPress];
[longPress release];
*/
//////// 第四种手势 平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlepan:)];
[aView addGestureRecognizer:pan];
[pan release];
///////// 第五种 捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[aView addGestureRecognizer:pinch];
[pinch release];
//////// 第六种 旋转手势
UIRotationGestureRecognizer * rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handlerorate:)];
[aView addGestureRecognizer:rotate];
[rotate release];
///////// 第七种 屏幕边缘手势 ios7 之后新增的手势
UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenEdge:)];
//设置屏幕边缘
screenEdge.edges = UIRectEdgeLeft;
//移除某个手势
[aView removeGestureRecognizer:screenEdge];
//获取该视图所添加的所有手势
//aView.gestureRecognizers;
[aView addGestureRecognizer:screenEdge];
[screenEdge release];
}
#pragma mark - handle gesture action -
///////// 第一种 处理轻拍手势的事件
- (void)handleTap:(UITapGestureRecognizer *)tap{
//修改轻拍的视图的颜色
tap.view.backgroundColor = RandomColor;
}
//////// 第二种 处理轻扫手势
//改变父视图的颜色
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe{
swipe.view.superview.backgroundColor = RandomColor;
}
//改变该视图的颜色
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)leftSwipe{
leftSwipe.view.backgroundColor = RandomColor;
}
///////// 第三种 处理长按手势的事件
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress{ //改变视图的颜色 (开始状态) if (longPress.state == UIGestureRecognizerStateBegan) { longPress.view.backgroundColor = RandomColor; }}////////// 第四种 处理平移手势的事件- (void)handlepan:(UIPanGestureRecognizer *)pan{ //换取偏移量CGPoint point = [pan translationInView:pan.view]; //改变视图的位置 让视图发生移动,以前一次改变的值为基准 pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y); //将之前的增量清零 [pan setTranslation:CGPointZero inView:pan.view];}////// 第五种 处理捏合手势事件 - (void)handlePinch:(UIPinchGestureRecognizer *)pinch { //获取俩个手指之间的距离 pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); pinch.scale = 1;//比例}////// 第六种 处理旋转事件- (void)handlerorate:(UIRotationGestureRecognizer *)rorate{ rorate.view.transform = CGAffineTransformRotate(rorate.view.transform, rorate.rotation);//将之前的增量置为0; rorate.rotation = 0;}////// 第七种 处理屏幕边缘手势的事件- (void)handleScreenEdge:(UIScreenEdgePanGestureRecognizer *)screenEdge{ screenEdge.view.backgroundColor = RandomColor; }
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
//1.设置轻拍次数
tap.numberOfTapsRequired = 1;//默认值是1 轻拍一次
//2.设置手指的个数
tap.numberOfTouchesRequired = 2;//默认值是1
[aView addGestureRecognizer:tap];
[tap release];
*/
/*
/////// //第二种手势 轻扫 最多支持两种手势方向
//需求:向右轻扫改变自身颜色,向左轻扫改变父视图颜色
//1.向右轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
//设置轻扫方向
//默认轻扫方向 向右
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[aView addGestureRecognizer:swipe];
[swipe release];
//2.向左轻扫的手势
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeLeft:) ];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;//方向向左
[aView addGestureRecognizer:leftSwipe];
[leftSwipe release];
*/
/*
/////// 第三种手势 长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
//设置识别长按手势的时间
longPress.minimumPressDuration = 0.5;
[aView addGestureRecognizer:longPress];
[longPress release];
*/
//////// 第四种手势 平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlepan:)];
[aView addGestureRecognizer:pan];
[pan release];
///////// 第五种 捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[aView addGestureRecognizer:pinch];
[pinch release];
//////// 第六种 旋转手势
UIRotationGestureRecognizer * rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handlerorate:)];
[aView addGestureRecognizer:rotate];
[rotate release];
///////// 第七种 屏幕边缘手势 ios7 之后新增的手势
UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenEdge:)];
//设置屏幕边缘
screenEdge.edges = UIRectEdgeLeft;
//移除某个手势
[aView removeGestureRecognizer:screenEdge];
//获取该视图所添加的所有手势
//aView.gestureRecognizers;
[aView addGestureRecognizer:screenEdge];
[screenEdge release];
}
#pragma mark - handle gesture action -
///////// 第一种 处理轻拍手势的事件
- (void)handleTap:(UITapGestureRecognizer *)tap{
//修改轻拍的视图的颜色
tap.view.backgroundColor = RandomColor;
}
//////// 第二种 处理轻扫手势
//改变父视图的颜色
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe{
swipe.view.superview.backgroundColor = RandomColor;
}
//改变该视图的颜色
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)leftSwipe{
leftSwipe.view.backgroundColor = RandomColor;
}
///////// 第三种 处理长按手势的事件
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress{ //改变视图的颜色 (开始状态) if (longPress.state == UIGestureRecognizerStateBegan) { longPress.view.backgroundColor = RandomColor; }}////////// 第四种 处理平移手势的事件- (void)handlepan:(UIPanGestureRecognizer *)pan{ //换取偏移量CGPoint point = [pan translationInView:pan.view]; //改变视图的位置 让视图发生移动,以前一次改变的值为基准 pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y); //将之前的增量清零 [pan setTranslation:CGPointZero inView:pan.view];}////// 第五种 处理捏合手势事件 - (void)handlePinch:(UIPinchGestureRecognizer *)pinch { //获取俩个手指之间的距离 pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); pinch.scale = 1;//比例}////// 第六种 处理旋转事件- (void)handlerorate:(UIRotationGestureRecognizer *)rorate{ rorate.view.transform = CGAffineTransformRotate(rorate.view.transform, rorate.rotation);//将之前的增量置为0; rorate.rotation = 0;}////// 第七种 处理屏幕边缘手势的事件- (void)handleScreenEdge:(UIScreenEdgePanGestureRecognizer *)screenEdge{ screenEdge.view.backgroundColor = RandomColor; }
浙公网安备 33010602011771号