/**
* 手势识别器
1 能够为系统提供的视图对象添加触摸事件的响应方法比如(uiview uilabel uiimageview等)
2 内部封装了手势识别的过程 只需要把重心放到手势识别之后对应的操作
*/
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
view.backgroundColor = [UIColor brownColor];
[self.view addSubview:view];
[view release];
UIButton *goBack = [UIButton buttonWithType:UIButtonTypeSystem];
goBack.frame = CGRectMake(20, 20, 60, 40);
[goBack setTitle:@"GO" forState:UIControlStateNormal];
[view addSubview:goBack];
[goBack addTarget:self action:@selector(gun:) forControlEvents:UIControlEventTouchUpInside];
// UIGestureRecognizer 是手势识别器的一个抽象的基类 只提供手势识别器的基本功能 使用具体子类(7大子类)
// //轻拍手势
// UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
设置轻拍的次数
// tap.numberOfTapsRequired = 1;
设置轻拍时需要手指的个数
// tap.numberOfTouchesRequired = 2;
给视图添加手势
// [view addGestureRecognizer:tap];
// [tap release];
// 长按手势
// UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture:)];
// 设置长按手势 触发的最短时间
// longpress.minimumPressDuration = 2;
// [view addGestureRecognizer:longpress];
// [longpress release];
// 清扫手势
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
// 设置左右方向都存在只要枚举值里有左移或者右移运算符 表示 可以按位或 同时存在
// 上下和左右在一个手势识别器中不能同时存在 (若想同时存在 则创建两个清扫手势对象)
// swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
// [view addGestureRecognizer:swipe];
// [swipe release];
//
// 平移手势
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
// [view addGestureRecognizer:pan];
// [pan release];
//
// 捏合手势
// UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
// [view addGestureRecognizer:pinch];
// [pinch release];
// 旋转手势
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture:)];
// [view addGestureRecognizer:rotation];
// [rotation release];
//屏幕边缘手势
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenEdgePanGesture:)];
screenEdgePan.edges = UIRectEdgeLeft;
[view addGestureRecognizer:screenEdgePan];
[screenEdgePan release];
}
-(void)gun:(UIButton *)butten{
butten.superview.frame = [[UIScreen mainScreen] bounds];
}
-(void)handleScreenEdgePanGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgePan{
CGPoint point = [screenEdgePan translationInView:screenEdgePan.view];
screenEdgePan.view.transform = CGAffineTransformTranslate(screenEdgePan.view.transform, point.x, 0);
[screenEdgePan setTranslation:point inView:screenEdgePan.view];
}
-(void)handleRotationGesture:(UIRotationGestureRecognizer *)rotation{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
//旋转角度清零
rotation.rotation = 0.0;
}
-(void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//把比例置 1
pinch.scale = 1.0;
}
-(void)handlePanGesture:(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)handleSwipeGesture:(UISwipeGestureRecognizer *)swipe{
swipe.view.backgroundColor = [UIColor randomColor];
}
-(void)handleTapGesture:(UITapGestureRecognizer *)tap{
tap.view.backgroundColor = [UIColor randomColor];
}
-(void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPress{
//当识别到长按手势触发(也就是长按时间到达后)
if (UIGestureRecognizerStateBegan == longPress.state) {
longPress.view.backgroundColor = [UIColor randomColor];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
iOS UIGestureRecognizer小结
最新推荐文章于 2018-10-11 15:10:10 发布