iOS:触摸控件UITouch、事件类UIEvent
UITouch:觸摸控件類 ??UIEvent:事件類
?
??????UITouch的介紹??????
一、觸摸狀態類型枚舉typedef?NS_ENUM(NSInteger, UITouchPhase) {
? ? UITouchPhaseBegan, ? ? ? ? ? ??// 開始觸摸
? ? UITouchPhaseMoved, ? ? ? ??? ??// 觸摸移動
? ? UITouchPhaseStationary,? ? ???// 觸摸沒有移動
? ? UITouchPhaseEnded, ? ? ? ? ? ??//觸摸結束
? ? UITouchPhaseCancelled, ? ? ? ??//取消觸摸
};
?
@interface?UITouch :?NSObject
二、屬性:
//時間戳記錄了觸摸事件產生或變化時的時間,單位是秒
@property(nonatomic,readonly)?NSTimeInterval ? timestamp;
//當前觸摸事件在一個周期中所處的狀態? ? ? ??? ? ? ?
@property(nonatomic,readonly)?UITouchPhase ?phase;
?//表示短時間內點按屏幕的次數? ? ??? ? ??
@property(nonatomic,readonly)?NSUInteger ?tapCount;?
//觸摸的主半徑 ? ? ??? ? ? ? ?
@property(nonatomic,readonly)?CGFloat?majorRadius;
//觸摸的主半徑的公差
@property(nonatomic,readonly)?CGFloat?majorRadiusTolerance;
//觸摸產生時所處的窗口。由于窗口可能發生變化,當前所在的窗口不一定是最開始的窗口
@property(nonatomic,readonly,retain)?UIWindow? ? *window;
//觸摸產生時所處的視圖。由于視圖可能發生變化,當前視圖也不一定時最初的視圖
@property(nonatomic,readonly,retain)?UIView? ? ? *view;
//觸摸手勢數組
@property(nonatomic,readonly,copy) ??NSArray?? *gestureRecognizers;
?
三、方法:
//返回當前觸摸點的位置
- (CGPoint)locationInView:(UIView?*)view;
//返回上一個觸摸點的位置
- (CGPoint)previousLocationInView:(UIView?*)view;
@end
?
四、由于觸摸點被放在了NSSet,所以通過NSSet集合的一個屬性和方法可以獲取觸摸點:
//獲取全部的觸摸點
@property?(readonly,?copy)?NSArray?*allObjects;
//獲取當前觸摸點
- (id)anyObject;
?
?
===============================================================
?
?
?
??????UIEvent的介紹??????
?//輸入事件類型枚舉
typedef NS_ENUM(NSInteger, UIEventType) {
? ?UIEventTypeTouches, ? ? ? ? ? ? //觸摸事件
? ? UIEventTypeMotion, ? ? ? ? ? ? ?//運動事件
? ? UIEventTypeRemoteControl, ? ?//遠程控制事件
};
//輸入事件不同類型的一些具體事件枚舉
typedef NS_ENUM(NSInteger, UIEventSubtype) {
? ? UIEventSubtypeNone? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0,
? ? UIEventSubtypeMotionShake ? ? ? ? ? ? ? ? ? ? ? = 1?
? ? UIEventSubtypeRemoteControlPlay ? ? ? ? ? ? ? ? = 100,?
? ? UIEventSubtypeRemoteControlPause? ? ? ? ? ? ? ? = 101,?
? ? UIEventSubtypeRemoteControlStop ? ? ? ? ? ? ? ? = 102,
? ? UIEventSubtypeRemoteControlTogglePlayPause? ? ? = 103,
? ? UIEventSubtypeRemoteControlNextTrack? ? ? ? ? ? = 104,
? ? UIEventSubtypeRemoteControlPreviousTrack? ? ? ? = 105,
? ? UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
? ? UIEventSubtypeRemoteControlEndSeekingBackward ? = 107,
? ? UIEventSubtypeRemoteControlBeginSeekingForward? = 108,
? ? UIEventSubtypeRemoteControlEndSeekingForward? ? = 109;
};
?
//類
@interface?UIEvent :?NSObject
//屬性
@property(nonatomic,readonly)?UIEventType?? ?type?; ? ? ? ? ???//事件類型
@property(nonatomic,readonly)?UIEventSubtype??subtype?; ??? //同一事件類型的具體事件
@property(nonatomic,readonly)?NSTimeInterval??timestamp; ? ?//事件觸發時間間隔
//方法
※所有觸摸點
- (NSSet?*)allTouches;?
※窗體上的觸摸點
- (NSSet?*)touchesForWindow:(UIWindow?*)window;
※視圖上的觸摸點
- (NSSet?*)touchesForView:(UIView?*)view;
※手勢觸摸點
?- (NSSet?*)touchesForGestureRecognizer:(UIGestureRecognizer?*)gesture;
?@end
?
五、舉例如下:演示觸摸事件(都是用鼠標代替手指在模擬器上進行測試)
實例一:打印觸摸輸出
1.設置用戶交互和觸摸點
//支持用戶交互,能夠處理觸摸事件 self.view.userInteractionEnabled = YES;//支持多點觸摸 self.view.multipleTouchEnabled = YES;?
2.實現UIResponser中觸摸的4個事件
觸摸開始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {NSLog(@"觸摸開始");//取出一個touch對象UITouch *touch = [touches anyObject];//取出當前點CGPoint location = [touch locationInView:self.view];//取出上一點CGPoint previousLocation = [touch previousLocationInView:self.view];NSLog(@"%@,%@",NSStringFromCGPoint(location),NSStringFromCGPoint(previousLocation));//遍歷每一個touch對象[touches enumerateObjectsUsingBlock:^(UITouch *touch, BOOL *stop) {}]; }觸摸移動
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {NSLog(@"觸摸移動"); }觸摸結束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {NSLog(@"觸摸結束"); }觸摸取消(該事件在模擬器不好演示)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {NSLog(@"觸摸取消"); }?
演示結果如下:
1.在屏幕上開始觸摸和結束觸摸時:
2015-10-08 21:21:24.875 02-touch[5988:346647] 觸摸開始 2015-10-08 21:21:24.875 02-touch[5988:346647] {219.5, 181},{219.5, 181} 2015-10-08 21:21:24.983 02-touch[5988:346647] 觸摸結束2.在屏幕上觸摸移動時:
2015-10-08 21:23:00.388 02-touch[5988:346647] 觸摸移動 2015-10-08 21:23:00.413 02-touch[5988:346647] 觸摸移動 2015-10-08 21:23:00.430 02-touch[5988:346647] 觸摸移動?
實例二:觸摸時,視圖變色(每觸摸一次,顏色就交替改變)
<1>布局故事板,在視圖中在拖一個試圖UIView控件,設置合適大小和背景顏色
<2>自定義一個試圖類,將控件與該類關聯
???? ????
<3>在自定義類myView.m中實現觸摸開始事件
//開始觸摸 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {if ([self.backgroundColor isEqual:[UIColor redColor]]){self.backgroundColor = [UIColor purpleColor];}else{self.backgroundColor = [UIColor redColor];} }<4>演示結果如下
? ? 開始顏色:紅色 ? ? ? ? ? ? ? ? ? ? ? ?觸摸一次:變為紫色 ? ? ? ? ? ? ? ? 再觸摸一次:又變為紅色
???
?
實例三:觸摸移動時,移動紅色試圖位置
<1>布局故事板,在視圖中在拖一個試圖UIView控件,設置合適大小和背景顏色
<2>自定義一個試圖類,將控件與該類關聯,同時將視圖IBOutLet到視圖控制器類中
???? ??
<3> 在控制器類ViewController.m中實現觸摸移動事件
//添加移動事件 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {//獲取觸摸對象touchUITouch *touch = [touches anyObject];//當前觸摸點CGPoint location = [touch locationInView:self.view];//設置觸摸點只有在myView中,才可以移動myView視圖CGPoint point = [touch locationInView:self.myView];if ([self.myView pointInside:point withEvent:event]){//上一個觸摸點CGPoint previousLocation = [touch previousLocationInView:self.view];//計算位移CGFloat xOffset = location.x - previousLocation.x;CGFloat yOffset = location.y - previousLocation.y;//設置視圖新的位置CGPoint center = self.myView.center;self.myView.center = CGPointMake(center.x+xOffset, center.y+yOffset);}}<4>演示結果如下:觸摸紅色視圖位置拖拽移動即可
? ??
? ? ?
?
?
總結
以上是生活随笔為你收集整理的iOS:触摸控件UITouch、事件类UIEvent的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: servlet+jdbc+javabea
- 下一篇: 什么是Nib文件?(Nib文件是一种特殊