最近空閒時間在學習iOS相關知識,幾周沒有更新文章了,今天總結下這些天的學習內容,也整理下iOS的學習筆記,以便以後查閱翻看~
iOS中的事件可以分爲3大類型:
觸摸事件
加速計事件
遠程控制事件
響應者對象
在iOS中不是任何對象都能處理事件,只有繼承了UIResponder的對象才能接收並處理事件。我們稱之爲“響應者對象”。
UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應者對象,都能夠接收並處理事件。
UIResponder內部提供了以下方法來處理事件
觸摸事件(對應android的action_down、action_move、action_up、action_cancel)
- (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;
加速計事件(有點像android的傳感器)
- (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;
UIView的觸摸事件處理
UIView是UIResponder的子類,可以覆蓋下列4個方法處理不同的觸摸事件
一根或者多根手指開始觸摸view,系統會自動調用view的下面方法
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
一根或者多根手指在view上移動,系統會自動調用view的下面方法(隨着手指的移動,會持續調用該方法)
- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
一根或者多根手指離開view,系統會自動調用view的下面方法
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
觸摸結束前,某個系統事件(例如電話呼入)會打斷觸摸過程,系統會自動調用view的下面方法
- (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
touches中存放的都是UITouch對象。
當用戶用一根手指觸摸屏幕時,會創建一個與手指相關聯的UITouch對象,一根手指對應一個UITouch對象。
UITouch的作用
保存着跟手指相關的信息,比如觸摸的位置、時間、階段。當手指移動時,系統會更新同一個UITouch對象,使之能夠一直保存該手指在的觸摸位置,當手指離開屏幕時,系統會銷燬相應的UITouch對象。
UITouch的屬性
觸摸產生時所處的窗口
@property(nonatomic,readonly,retain) UIWindow *window;
觸摸產生時所處的視圖
@property(nonatomic,readonly,retain) UIView *view;
短時間內點按屏幕的次數,可以根據tapCount判斷單擊、雙擊或更多的點擊
@property(nonatomic,readonly) NSUInteger tapCount;
記錄了觸摸事件產生或變化時的時間,單位是秒
@property(nonatomic,readonly) NSTimeInterval timestamp;
當前觸摸事件所處的狀態
@property(nonatomic,readonly) UITouchPhase phase;
- (CGPoint)locationInView:(UIView *)view;
返回值表示觸摸在view上的位置
這裏返回的位置是針對view的座標系的(以view的左上角爲原點(0, 0))
調用時傳入的view參數爲nil的話,返回的是觸摸點在UIWindow的位置
- (CGPoint)previousLocationInView:(UIView *)view;
該方法記錄了前一個觸摸點的位置
自定義控件拖拽效果
ios:
1.自定義view繼承自UIView
2.覆蓋touchesMoved實現具體邏輯
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//取得UITouch對象
UITouch *touch = [touches anyObject];
//獲取上一個點
CGPoint p = [touch previousLocationInView:self];
//獲取當前點
CGPoint currp = [touch locationInView:self];
//產生平移拖拽效果
self.transform = CGAffineTransformTranslate(self.transform, currp.x - p.x, currp.y - p.y);
}
android:
1.自定義view繼承自View
2.覆蓋onTouchEvent實現具體邏輯
private int x, y;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
x = (int) event.getRawX();
y = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int mx = (int)event.getRawX();
int my = (int)event.getRawY();
// setTranslationX(getTranslationX()+mx-x);
// setTranslationY(getTranslationY()+my-y);
layout(getLeft()+mx-x,getTop()+my-y,getLeft()+getMeasuredWidth()+mx-x,getTop()+getMeasuredHeight()+my-y);
x = mx;
y = my;
break;
}
return true;
}
UIGestureRecognizer
iOS 3.2之後,蘋果推出了手勢識別功能(Gesture Recognizer),在觸摸事件處理方面,利用UIGestureRecognizer,能輕鬆識別用戶在某個view上面做的一些常見手勢。
UIGestureRecognizer
UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行爲,使用它的子類才能處理具體的手勢
UITapGestureRecognizer(敲擊)
UIPinchGestureRecognizer(捏合,用於縮放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(輕掃)
UIRotationGestureRecognizer(旋轉)
UILongPressGestureRecognizer(長按)
手勢識別器的用法相似,比如UITapGestureRecognizer的使用步驟如下:
//創建手勢識別器對象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//設置手勢識別器對象的具體屬性
// 連續敲擊2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲擊
tap.numberOfTouchesRequired = 2;
//添加手勢識別器到對應的view上
[self.iconView addGestureRecognizer:tap];
監聽手勢的觸發
[tap addTarget:self action:@selector(tapIconView:)];
上面的拖拽效果也可以用這種方式實現:
//頁面加載完成時調用
- (void)viewDidLoad {
[super viewDidLoad];
//創建手勢識別器對象
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
//添加手勢識別器到對應的view上
[self.imageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 獲取手勢的觸摸點
// CGPoint curP = [pan locationInView:self.imageView];
// 獲取手勢的移動,也是相對於最開始的位置
CGPoint transP = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
// 復位
[pan setTranslation:CGPointZero inView:self.imageView];
//判斷當前手指狀態
//if (pan.state == UIGestureRecognizerStateBegan) {//手指按下時類似於Android中的ACTION_DOWN
//}
}
其中用pan.state
對應UIGestureRecognizerState
有如下幾種狀態:
// 沒有觸摸事件發生,所有手勢識別的默認狀態
UIGestureRecognizerStatePossible,
// 一個手勢已經開始但尚未改變或者完成時
UIGestureRecognizerStateBegan,
// 手勢狀態改變
UIGestureRecognizerStateChanged,
// 手勢完成
UIGestureRecognizerStateEnded,
// 手勢取消,恢復至Possible狀態
UIGestureRecognizerStateCancelled,
// 手勢失敗,恢復至Possible狀態
UIGestureRecognizerStateFailed,
// 識別到手勢識別
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
注意:默認不支持多個手勢,也就是默認不能同時縮放和旋轉的,如果要支持多個手勢需要實現UIGestureRecognizerDelegate
代理方法:shouldRecognizeSimultaneouslyWithGestureRecognizer
方法:
#pragma mark - 手勢代理方法
// 是否允許開始觸發手勢
//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
// return NO;
//}
// 是否允許同時支持多個手勢,默認是不支持多個手勢
// 返回yes表示支持多個手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
// 是否允許接收手指的觸摸點
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
// // 獲取當前的觸摸點
// CGPoint curP = [touch locationInView:self.imageView];
// return YES;
//}
事件傳遞
一個view怎麼不能處理事件:
userInteractionEnabled = NO,hidden = YES,alpha <= 0.01UIImageView默認不允許用戶交互,因此默認它上面的子控件不能接收事件。
通過遞歸找到最合適的view
第一個接收事件的控件是窗口,當事件傳遞給窗口的時候,就會讓窗口去找最合適的view,
1. 判斷自己能不能接收事件
2. 點在不在窗口上
3. 去找比自己更合適的view,從後往前遍歷子控件,拿到子控件後,把事件傳遞給這個子控件
4. 子控件拿到事件之後,又會做同樣的判斷,一直遞歸去找,直到找到最合適的view.
事件傳遞的目的在於要找到最合適的view,把事件交給他。
hitText方法和pointInside方法
// 事件傳遞的時候調用
// 當事件傳遞給控件的時候,就會調用控件的這個方法,尋找最合適的view
// point:當前的觸摸點,point這個點的座標系就是方法調用者
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// 調用系統的做法去尋找最合適的view,返回最合適的view
UIView *fitView = [super hitTest:point withEvent:event];
// NSLog(@"fitView--%@",fitView);
return fitView;
}
// 作用:判斷當前這個點在不在方法調用者(控件)上
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return YES;
}
hitTest的底層實現
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// 1.判斷當前控件能否接收事件
if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;
// 2. 判斷點在不在當前控件
if ([self pointInside:point withEvent:event] == NO) return nil;
// 3.從後往前遍歷自己的子控件
NSInteger count = self.subviews.count;
for (NSInteger i = count - 1; i >= 0; i--) {
UIView *childView = self.subviews[i];
// 把當前控件上的座標系轉換成子控件上的座標系
CGPoint childP = [self convertPoint:point toView:childView];
UIView *fitView = [childView hitTest:childP withEvent:event];
if (fitView) { // 尋找到最合適的view
return fitView;
}
}
// 循環結束,表示沒有比自己更合適的view
return self;
}
- 判斷窗口能不能處理事件? 如果不能,意味着窗口不是最合適的view,而且也不會去尋找比自己更合適的view,直接返回nil,通知UIApplication,沒有最合適的view。
- 判斷點在不在窗口
- 遍歷自己的子控件,尋找有沒有比自己更合適的view
- 如果子控件不接收事件,意味着子控件沒有找到最合適的view,然後返回nil,告訴窗口沒有找到更合適的view,窗口就知道沒有比自己更合適的view,就自己處理事件。
響應者鏈的事件傳遞過程
touch的默認做法:自己不處理,交給上一個響應者。
上一個響應者默認是父控件
- 如果view的控制器存在,就傳遞給控制器;如果控制器不存在,則將其傳遞給它的父視圖
- 在視圖層次結構的最頂級視圖,如果也不能處理收到的事件或消息,則其將事件或消息傳遞給window對象進行處理
- 如果window對象也不處理,則其將事件或消息傳遞給UIApplication對象
- 如果UIApplication也不能處理該事件或消息,則將其丟棄