Demo实例
UIGestureRecognizer类,用于检测发生在设备中的手势。
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:
1、拍击 UITapGestureRecognizer (任意次数的拍击)
通过设置属性" numberOfTapsRequired "实现单击,或双击
2、向里或向外捏 UIPinchGestureRecognizer (用于缩放)
通过设置属性" scale "实现对UI控件大小进行缩放的改变,如:
float lastScale = 0.0;
UILabel *view = (UILabel *)recognizer.view;
recognizer.scale = recognizer.scale - lastScale + 1.0;
view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale);
lastScale = recognizer.scale;
3、摇动或者拖拽 UIPanGestureRecognizer
4、滑动 UISwipeGestureRecognizer (以任意方向)
通过设置属性" direction "实现滑动方向
5、旋转 UIRotationGestureRecognizer (手指朝相反方向移动)
通过设置属性" rotation "实现对UI控件旋转的改变,如:
UILabel *view = (UILabel *)recognizer.view;
float rotationV = recognizer.rotation;
view.transform = CGAffineTransformMakeRotation(rotationV);
6、长按 UILongPressGestureRecognizer
通过设置属性" longpressRecognizer "实现长按响应时间
对于不同类型的手势识别器,具有不同的配置属性。
比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一 个消息,用于处理响应手势动作后的任务。
不同的手势识别器,发送的消息方法也会有所不同。
使用注意事项:
1、实例化时,同时设置响应target对象,以及响应方法
2、响应方法中,可能会出现方法被多次响应的情况,可通过设置手势状态进行控制
-
- UIImageView *recognizerImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 144, 60, 60)];
- [recognizerImage setImage:[UIImage imageNamed:@"dingdang.png"]];
- [recognizerImage setBackgroundColor:[UIColor clearColor]];
- [recognizerImage setUserInteractionEnabled:YES];
- [self.view addSubview:recognizerImage];
-
-
- UITapGestureRecognizer *tapRecognizerSingle = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizerSingleAction:)];
- tapRecognizerSingle.numberOfTapsRequired = 1;
-
- [recognizerImage addGestureRecognizer:tapRecognizerSingle];
-
-
- UITapGestureRecognizer *tapRecognizerDouble = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizerDoubleAction:)];
- tapRecognizerDouble.numberOfTapsRequired = 2;
-
- [tapRecognizerSingle requireGestureRecognizerToFail:tapRecognizerDouble];
-
- [recognizerImage addGestureRecognizer:tapRecognizerDouble];
-
-
- bool isChangeBgColor;
- - (void)tapRecognizerSingleAction:(UITapGestureRecognizer *)recognizer
- {
-
- if (1 == recognizer.numberOfTapsRequired)
- {
- UIImageView *view = (UIImageView *)recognizer.view;
-
- if (!isChangeBgColor)
- {
- [view setBackgroundColor:[UIColor orangeColor]];
- }
- else
- {
- [view setBackgroundColor:[UIColor yellowColor]];
- }
- isChangeBgColor = !isChangeBgColor;
- }
- }
-
-
-
- bool isChangeSize;
- - (void)tapRecognizerDoubleAction:(UITapGestureRecognizer *)recognizer
- {
-
- if (2 == recognizer.numberOfTapsRequired)
- {
- UIImageView *view = (UIImageView *)recognizer.view;
-
-
- if (!isChangeSize)
- {
-
- CGRect currentBounds = view.bounds;
- currentBounds.size.width *= 2;
- currentBounds.size.height *= 2;
-
-
- [UIView animateWithDuration:0.3 animations:^{
- view.bounds = currentBounds;
- }];
- }
- else
- {
-
- CGRect currentBounds = view.bounds;
- currentBounds.size.width /= 2;
- currentBounds.size.height /= 2;
-
-
- [UIView animateWithDuration:0.3 animations:^{
- view.bounds = currentBounds;
- }];
- }
- isChangeSize = !isChangeSize;
- }
- }
-
- UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRecognizerAction:)];
-
- [recognizerImage addGestureRecognizer:longPressRecognizer];
-
-
- - (void)longPressRecognizerAction:(UILongPressGestureRecognizer *)recognizer
- {
-
- UIImageView *view = (UIImageView *)recognizer.view;
-
- [view setFrame:CGRectMake(0, 0, 60, 60)];
- [view setCenter:CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2)];
- [view setBackgroundColor:[UIColor clearColor]];
- }
-
- UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateRecognizerAction:)];
-
- [recognizerImage addGestureRecognizer:rotateRecognizer];
-
-
- - (void)rotateRecognizerAction:(UIRotationGestureRecognizer *)recognizer
- {
-
- UIImageView *view = (UIImageView *)recognizer.view;
-
- float rotationV = recognizer.rotation;
- view.transform = CGAffineTransformMakeRotation(rotationV);
- }
-
- UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognizerAction:)];
-
- [recognizerImage addGestureRecognizer:panRecognizer];
-
-
- - (void)panRecognizerAction:(UIPanGestureRecognizer *)recognizer
- {
-
- UIImageView *view = (UIImageView *)recognizer.view;
-
- CGPoint translation = [recognizer translationInView:view];
- CGFloat centerX = view.center.x + translation.x;
- CGFloat centerY = view.center.y + translation.y;
- view.center = CGPointMake(centerX, centerY);
- [recognizer setTranslation:CGPointZero inView:view];
- }
-
-
- UISwipeGestureRecognizer *swipeRecognizerUP = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
- swipeRecognizerUP.direction = UISwipeGestureRecognizerDirectionUp;
-
- UISwipeGestureRecognizer *swipeRecognizerDOWN = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
- swipeRecognizerDOWN.direction = UISwipeGestureRecognizerDirectionDown;
-
- UISwipeGestureRecognizer *swipeRecognizerLEFT = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
- swipeRecognizerLEFT.direction = UISwipeGestureRecognizerDirectionLeft;
-
- UISwipeGestureRecognizer *swipeRecognizerRIGHT = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizerAction:)];
- swipeRecognizerRIGHT.direction = UISwipeGestureRecognizerDirectionRight;
-
- [recognizerImage addGestureRecognizer:swipeRecognizerUP];
- [recognizerImage addGestureRecognizer:swipeRecognizerUP];
- [recognizerImage addGestureRecognizer:swipeRecognizerUP];
- [recognizerImage addGestureRecognizer:swipeRecognizerRIGHT];
-
-
- - (void)swipeRecognizerAction:(UISwipeGestureRecognizer *)recognizer
- {
- UIImageView *view = (UIImageView *)recognizer.view;
- NSLog(@"view(%@) %@", NSStringFromClass([view class]), view);
-
- if (recognizer.direction == UISwipeGestureRecognizerDirectionUp)
- {
- NSLog(@"direction == UISwipeGestureRecognizerDirectionUp");
- }
- else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown)
- {
- NSLog(@"direction == UISwipeGestureRecognizerDirectionDown");
- }
- else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
- {
- NSLog(@"direction == UISwipeGestureRecognizerDirectionLeft");
- }
- else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
- {
- NSLog(@"direction == UISwipeGestureRecognizerDirectionRight");
- }
- }
-
- UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRecognizerAction:)];
-
- [recognizerImage addGestureRecognizer:pinchRecognizer];
-
-
- float lastScale = 0.0;
- - (void)pinchRecognizerAction:(UIPinchGestureRecognizer *)recognizer
- {
-
- UIImageView *view = (UIImageView *)recognizer.view;
-
- recognizer.scale = recognizer.scale - lastScale + 1.0;
- view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale);
-
- lastScale = recognizer.scale;
- }