Main.storyboard

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="vXZ-lx-hvc">
  3. <dependencies>
  4. <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
  5. </dependencies>
  6. <scenes>
  7. <!--View Controller-->
  8. <scene sceneID="ufC-wZ-h7g">
  9. <objects>
  10. <viewController id="vXZ-lx-hvc" customClass="LWTViewController" sceneMemberID="viewController">
  11. <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
  12. <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
  13. <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
  14. <subviews>
  15. <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="scene" id="i13-3w-YRs">
  16. <rect key="frame" x="48" y="149" width="224" height="182"/>
  17. <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
  18. </imageView>
  19. </subviews>
  20. <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
  21. </view>
  22. <connections>
  23. <outlet property="imageView" destination="i13-3w-YRs" id="omC-0A-gHm"/>
  24. </connections>
  25. </viewController>
  26. <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
  27. </objects>
  28. </scene>
  29. </scenes>
  30. <resources>
  31. <image name="scene" width="223" height="181"/>
  32. </resources>
  33. <simulatedMetricsContainer key="defaultSimulatedMetrics">
  34. <simulatedStatusBarMetrics key="statusBar"/>
  35. <simulatedOrientationMetrics key="orientation"/>
  36. <simulatedScreenMetrics key="destination"/>
  37. </simulatedMetricsContainer>
  38. </document>

View Code

LWTViewController.m

  1. //
  2. // LWTViewController.m
  3. // 手势识别器 -- UIGestureRecognizer
  4. //
  5. // Created by apple on 14-6-14.
  6. // Copyright (c) 2014年 lwt. All rights reserved.
  7. //
  8.  
  9. #import "LWTViewController.h"
  10.  
  11. @interface LWTViewController () <UIGestureRecognizerDelegate>
  12. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  13. @end
  14.  
  15. @implementation LWTViewController
  16. - (void)viewDidLoad
  17. {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view, typically from a nib.
  20. self.imageView.userInteractionEnabled = YES;
  21. self.imageView.multipleTouchEnabled = YES;
  22. // 拖拽
  23. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
  24. [self.imageView addGestureRecognizer:pan];
  25. // [self pinch];
  26. //
  27. // [self rotation];
  28. }
  29. - (void)panView: (UIPanGestureRecognizer *)pan
  30. {
  31. // NSLog(@"%@",pan);
  32. // 返回的值是以手指按下的点为原点
  33. CGPoint point = [pan translationInView:pan.view];
  34. // NSLog(@"拖拽 %@", NSStringFromCGPoint(point));
  35. self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
  36. // CGPoint temp = self.imageView.center;
  37. // temp.x += point.x;
  38. // temp.y += point.y;
  39. // self.imageView.center = temp;
  40. [pan setTranslation:CGPointZero inView:pan.view];
  41. }
  42. // 捏合
  43. - (void)pinch
  44. {
  45. UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
  46. pinch.delegate = self;
  47. [self.imageView addGestureRecognizer:pinch];
  48. }
  49. - (void)pinchView : (UIPinchGestureRecognizer *)pinch
  50. {
  51. // NSLog(@"%@",pinch);
  52. // self.imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
  53. self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
  54. pinch.scale = 1.0;
  55. }
  56. // 旋转
  57. - (void)rotation
  58. {
  59. UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
  60. rotation.delegate = self;
  61. [self.imageView addGestureRecognizer:rotation];
  62. }
  63. - (void)rotationView: (UIRotationGestureRecognizer *)rotation
  64. {
  65. // NSLog(@"%@",rotation);
  66. // 每次从最初的位置开始
  67. // self.iconView.transform = CGAffineTransformMakeRotation(gesture.rotation);
  68. // 在传入的transform基础上递增一个弧度
  69. self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
  70. // 将旋转的弧度清零(注意不是将图片旋转的弧度清零, 而是将当前手指旋转的弧度清零)
  71. rotation.rotation = 0;
  72. }
  73. // 长按
  74. - (void)longPress
  75. {
  76. UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressView:)];
  77. longPress.minimumPressDuration = 2.0;
  78. // 手指按下后事件响应之前允许手指移动的偏移位
  79. longPress.allowableMovement = 50;
  80. [self.imageView addGestureRecognizer:longPress];
  81. }
  82. - (void)longPressView : (UILongPressGestureRecognizer *)longPress
  83. {
  84. NSLog(@"UILongPressGestureRecognizer");
  85. }
  86. // 轻扫
  87. - (void)swipe
  88. {
  89. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
  90. // 不是所有的都能实现
  91. // swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionRight;
  92. // 设置轻扫的方向
  93. swipe.direction = UISwipeGestureRecognizerDirectionUp;
  94. [self.imageView addGestureRecognizer:swipe];
  95. UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
  96. swipe1.direction = UISwipeGestureRecognizerDirectionLeft;
  97. [self.imageView addGestureRecognizer:swipe1];
  98. UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
  99. swipe2.direction = UISwipeGestureRecognizerDirectionDown;
  100. [self.imageView addGestureRecognizer:swipe2];
  101. UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
  102. swipe3.direction = UISwipeGestureRecognizerDirectionRight;
  103. [self.imageView addGestureRecognizer:swipe3];
  104. }
  105. - (void)swipeView : (UISwipeGestureRecognizer *)swipe
  106. {
  107. NSLog(@"UITapGestureRecognizer");
  108. }
  109. // 敲击
  110. - (void)tap
  111. {
  112. // 创建手势识别器
  113. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
  114. tap.delegate = self;
  115. // tap.numberOfTapsRequired = 2;
  116. // tap.numberOfTouchesRequired = 2;
  117. // 添加手势识别器到View
  118. [self.imageView addGestureRecognizer:tap];
  119. // 监听手势识别器
  120. [tap addTarget:self action:@selector(tapView:)];
  121. }
  122. - (void)tapView : (UITapGestureRecognizer *)tap
  123. {
  124. NSLog(@"UITapGestureRecognizer");
  125. }
  126. #pragma mark - UIGestureRecognizerDelegate
  127. /*
  128. // 该方法返回的BOOL值决定了view是否响应点击事件
  129. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  130. {
  131. CGPoint point = [touch locationInView:touch.view];
  132. if (point.x < self.imageView.bounds.size.width * 0.5) {
  133. return YES;
  134. }else
  135. {
  136. return NO;
  137. }
  138. }
  139. */
  140.  
  141. // 该方法返回的BOOL值决定了view是否能够同时响应多个手势
  142. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  143. {
  144. return YES;
  145. }
  146. @end

View Code

 

版权声明:本文为wentianblog原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wentianblog/p/3788537.html