????1??UIGestureRecognizer ????
????????????? iOS ?з???????????????????????豸??????????
????iOS ???? 3.2 ??????????Щ??????????UIGestureRecognizer ???????????????????????????????????????
????UIPanGestureRecognizer???????
????UIPinchGestureRecognizer??????
????UIRotationGestureRecognizer???????
????UITapGestureRecognizer??????
????UILongPressGestureRecognizer????????
????UISwipeGestureRecognizer???????
?????????????????? UIGestureRecognizer ????????????????????????????
????PS????????????????? #import <UIKit/UIGestureRecognizerSubclass.h>

?????????????·?????

1 - (void)reset;
2 
3 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
4 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
5 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
6 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
7 //???????????? UIGestureRecognizer (UIGestureRecognizerProtected) ???????????????????????в?

????UIGestureRecognizer ???й?????£?
 

 

????2????????
??????????????????У??????????????????????????? UITapGestureRecognizer??
????????????????????????????????????????????????????????????????????????????????????
???????仰????????????????????????????????????????????????ε??????????????????????????????????????????????????????????????ò??????????????????

????????????????£?

1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
2     UIGestureRecognizerStatePossible,   // ??δ???????????????????????????????????????????????
3     UIGestureRecognizerStateBegan,      // ???????????????????????????????????п???????仯???????????δ???
4     UIGestureRecognizerStateChanged,    // ?????????????
5     UIGestureRecognizerStateEnded,      // ????????????????????????????
6     UIGestureRecognizerStateCancelled,  // ????????????????????
7     UIGestureRecognizerStateFailed,     // ?????????????????????
8     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // ????????????UIGestureRecognizerStateEnded
9 };

????????????????? UITapGestureRecgnizer ???????????????????????????????1?????????????????????????β???????????????????????????????????ò???????????????3??????????
????????????????????????Щ????????????????????????????????????κβ????????????????????????????????????????????????????????????????????????0?????????????????????????????????????????1??????????????????1?????????????????2???????????????????????????????????ò?????????????????д????????????2????????????????????3????????1?β????????
????3?????????????
????????????????????????
???????????????????????????????????????????????????????????????????????л????????
??????????????????????????????????????????
??????????????? View ?С??????????????? View????????????? View ????????????????????????????????л????????
????PS????????????????? View????????? View ?????ж???????????????????????Щ????????????????????????????????????Ч??????????????????????????????????? option ??????????????????????
????4?????????
??????????????
?????????????????? UIImageView ???С?????????????????????????????????
????????????????????????? KMGestureRecognizer???????????? UIView ?С?
????????????е???????λ?????
???????????е???????????
????????????е?????????????
????????????????????????????????????????????
??????????????????????????????0.7
?????????????????????????????????У??????????е???????????λ
????????????????????????????????3?λ???????????????????????У??????????е???????????λ
????Ч?????£?

????KMGestureRecognizer.h

 1 #import <UIKit/UIKit.h>
 2 
 3 typedef NS_ENUM(NSUInteger, Direction) {
 4     DirectionUnknown,
 5     DirectionLeft,
 6     DirectionRight
 7 };
 8 
 9 @interface KMGestureRecognizer : UIGestureRecognizer
10 @property (assign, nonatomic) NSUInteger tickleCount; //????????
11 @property (assign, nonatomic) CGPoint currentTickleStart; //??????????????λ??
12 @property (assign, nonatomic) Direction lastDirection; //?????????????
13 
14 @end

????KMGestureRecognizer.m

 1 #import "KMGestureRecognizer.h"
 2 #import <UIKit/UIGestureRecognizerSubclass.h>
 3 
 4 @implementation KMGestureRecognizer
 5 #define kMinTickleSpacing 20.0
 6 #define kMaxTickleCount 3
 7 
 8 - (void)reset {
 9     _tickleCount = 0;
10     _currentTickleStart = CGPointZero;
11     _lastDirection = DirectionUnknown;
12     
13     if (self.state == UIGestureRecognizerStatePossible) {
14         self.state = UIGestureRecognizerStateFailed;
15     }
16 }
17 
18 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
19     UITouch *touch = [touches anyObject];
20     _currentTickleStart = [touch locationInView:self.view]; //?????????????????λ??
21 }
22 
23 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
24     //????????????????λ?á?????????????λ?á????? X ?????????????????????????
25     UITouch *touch = [touches anyObject];
26     CGPoint tickleEnd = [touch locationInView:self.view];
27     CGFloat tickleSpacing = tickleEnd.x - _currentTickleStart.x;
28     Direction currentDirection = tickleSpacing < 0 ? DirectionLeft : DirectionRight;
29     
30     //????? X ??????????????????
31     if (ABS(tickleSpacing) >= kMinTickleSpacing) {
32         //?ж?????????β????????????????????????????????л??????
33         if (_lastDirection == DirectionUnknown ||
34             (_lastDirection == DirectionLeft && currentDirection == DirectionRight) ||
35             (_lastDirection == DirectionRight && currentDirection == DirectionLeft)) {
36             _tickleCount++;
37             _currentTickleStart = tickleEnd;
38             _lastDirection = currentDirection;
39             
40             if (_tickleCount >= kMaxTickleCount && self.state == UIGestureRecognizerStatePossible) {
41                 self.state = UIGestureRecognizerStateEnded;
42                 //NSLog(@"?????????????????л??????");
43             }
44         }
45     }
46 }
47 
48 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
49     [self reset];
50 }
51 
52 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
53     [self reset];
54 }
55 
56 @end

 

????ViewController.h

 

1 #import <UIKit/UIKit.h>
2 #import "KMGestureRecognizer.h"
3 
4 @interface ViewController : UIViewController
5 @property (strong, nonatomic) UIImageView *imgV;
6 @property (strong, nonatomic) UIImageView *imgV2;
7 @property (strong, nonatomic) KMGestureRecognizer *customGestureRecognizer;
8 
9 @end

????ViewController.m
 

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)handlePan:(UIPanGestureRecognizer *)recognizer;
  5 - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;
  6 - (void)handleRotation:(UIRotationGestureRecognizer *)recognizer;
  7 - (void)handleTap:(UITapGestureRecognizer *)recognizer;
  8 - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer;
  9 - (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer;
 10 - (void)handleCustomGestureRecognizer:(KMGestureRecognizer *)recognizer;
 11 
 12 - (void)bindPan:(UIImageView *)imgVCustom;
 13 - (void)bindPinch:(UIImageView *)imgVCustom;
 14 - (void)bindRotation:(UIImageView *)imgVCustom;
 15 - (void)bindTap:(UIImageView *)imgVCustom;
 16 - (void)bindLongPress:(UIImageView *)imgVCustom;
 17 - (void)bindSwipe;
 18 - (void)bingCustomGestureRecognizer;
 19 - (void)layoutUI;
 20 @end
 21 
 22 @implementation ViewController
 23 
 24 - (void)viewDidLoad {
 25     [super viewDidLoad];
 26     
 27     [self layoutUI];
 28 }
 29 
 30 - (void)didReceiveMemoryWarning {
 31     [super didReceiveMemoryWarning];
 32     // Dispose of any resources that can be recreated.
 33 }
 34 
 35 #pragma mark - ???????????
 36 /**
 37  *  ???????????
 38  *
 39  *  @param recognizer ???????????????????
 40  */
 41 - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
 42     //?????ò???
 43     [recognizer.view.superview bringSubviewToFront:recognizer.view];
 44     
 45     CGPoint center = recognizer.view.center;
 46     CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
 47     CGPoint translation = [recognizer translationInView:self.view];
 48     //NSLog(@"%@", NSStringFromCGPoint(translation));
 49     recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
 50     [recognizer setTranslation:CGPointZero inView:self.view];
 51     
 52     if (recognizer.state == UIGestureRecognizerStateEnded) {
 53         //?????????????????????С??200??????л???
 54         CGPoint velocity = [recognizer velocityInView:self.view];
 55         CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
 56         CGFloat slideMult = magnitude / 200;
 57         //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
 58         
 59         //????????????????????????
 60         float slideFactor = 0.1 * slideMult;
 61         CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
 62                                          center.y + (velocity.y * slideFactor));
 63         //????С??cornerRadius?????????self.view.bounds.size.width - cornerRadius???????????????????
 64         finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
 65                            self.view.bounds.size.width - cornerRadius);
 66         finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
 67                            self.view.bounds.size.height - cornerRadius);
 68