事件處理以及響應者鏈條(一)

iOS有3大處理事件

  1. 觸摸事件
  2. 加速計事件
  3. 遠程操控事件

響應者對象

只有繼承了UIResponder的對象,才是響應者對象

  • 例如UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應者對象,都能夠接收并處理事件
UIResponder內部提供了以下方法來處理事件
觸摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

加速計事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

遠程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

GestureReconizer

長按

UILongPressGestureRecognizer
有4個自有的屬性

@property (nonatomic) NSUInteger numberOfTapsRequired;      // Default is 0. The number of full taps required before the press for gesture to be recognized
@property (nonatomic) NSUInteger numberOfTouchesRequired;   // Default is 1. Number of fingers that must be held down for the gesture to be recognized

@property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.5. Time in seconds the fingers must be held down for the gesture to be recognized
@property (nonatomic) CGFloat allowableMovement;

輕掃

UISwipeGestureRecognizer
有2個自有的屬性,主要是輕掃方向。而且,一個輕掃手勢只可以有一個方向,想監聽多個方向請添加多個手勢

@property(nonatomic) NSUInteger  numberOfTouchesRequired; // default is 1. the number of fingers that must swipe
@property(nonatomic) UISwipeGestureRecognizerDirection direction;

點擊

UITapGestureRecognizer
因為這個,即使一些本身不是繼承uicontrol的控件也可以監聽點擊事件
有2個自有的屬性,關鍵是第一個屬性,手機開發中最好不要使用多次點擊,就是說點擊次數最好為1

@property (nonatomic) NSUInteger  numberOfTapsRequired;       // Default is 1. The number of taps required to match
@property (nonatomic) NSUInteger  numberOfTouchesRequired; ```

###拖拽
UIPanGestureReconizer
有3個屬性
```objc
- (CGPoint)translationInView:(UIView *)view;                        // translation in the coordinate system of the specified view
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view;
- (CGPoint)velocityInView:(UIView *)view;

具體使用:

//這個是獲取手指從初始位置移動的偏移量
CGPoint offset = [tap translationInView:self.mainV];
//取出要改變的控件,重新設置frame
CGRect temp = self.mainV.frame;
temp.origin.x += offset.x;
temp.origin.y += offset.y;

self.mainV.frame = temp;
//再把形變設置為0,不然會因為不斷調用這個方法,值會不斷疊加,,
//例如從0 - 3的距離應該只移動3,但是卻變1+2+3 = 6,移動了6的距離
[tap setTranslation:CGPointZero inView:self.mainV];

pan手勢的開始需要手指滑動一定的量后,才會把移動后的點當做起始點,所以會有一個值的丟失,需要配合touchbegan進行使用

  • 如何同時支持旋轉和縮放?默認不支持多個手指,
    Simultaneously:同時
    當使用一個手勢的時候會調用代理的Simultaneously方法,詢問是否支持多個手勢
    • pan
      獲取平移的位置:translationInView
      復位:setTranslation:inView: 需要傳一個view,因為點的位置跟坐標系有關系,看他是基于哪個坐標系被清空的。

translationInView locationInView velocityInView

locationView是UIGestureRecognizer的屬性
translationInView是UIPanGestureRecognizer的屬性

這兩個的區別就是
locationView的話,最好是在控制器里面使用,然后傳值設置為控制器的view。得出一個新的點,這個點就是移動后的點
移動控件的中心點的話,直接給他賦值這個新的point就好了

translationInView的話,設置傳值為控件本身,得到的點,是指手指在控件上移動的offset。所以給控件的中心點加上這個offset,就可以使他準確的移動

- (void)translationInViewTest:(UIPanGestureRecognizer *)panGesture{
    CGPoint currentP = [panGesture translationInView:self];
    self.center = CGPointMake(self.center.x + currentP.x, self.center.y + currentP.y);
        [panGesture setTranslation:CGPointZero inView:self];
}
- (void)locationInView:(UIPanGestureRecognizer *)panGesture{
    CGPoint currentLIV = [panGesture locationInView:self.superview];
    NSLog(@"currentTPPp%@",NSStringFromCGPoint(currentLIV));
    self.center = currentLIV;
    [self translationInViewTest:panGesture];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容