1、事件(uievent),是由硬體裝置捕捉到使用者對裝置的操作,把這個操作抽象成一個事件對象
ios中三大事件:觸touches摸晃動事件motion,遠端控制事件remotecontrol;其中應有最廣泛的是觸摸事件
uiview是支援觸摸的,由于uiview 内部沒有實作跟觸摸相關的方法,是以我們再點選uiview建立其子類進行方法實作
2、一般準備做題思路:先在 appdelegate.m中建立一個 touchvc對象
touchviewcontroller *touchvc = [[touchviewcontroller alloc]init];
//将touchvc指定self.window的為根視圖控制器
self.window.rootviewcontroller = touchvc;
[touchvc release];
然後在 touchviewcontroller.m中設定顔色
self.view.backgroundcolor = [uicolor yellowcolor];
————————————————————————————
以上方法為通常準備方法:
第一類型:點選移動視圖
moveview *moveview = [[moveview alloc]initwithframe:cgrectmake(110, 234, 100, 100)];
moveview.backgroundcolor = [uicolor blackcolor];
[self.view addsubview:moveview];
[moveview release];
第二類型:捏合視圖對象
pinchview *pinchview = [[pinchview alloc]initwithframe:cgrectmake(60, 184, 200, 200)];
pinchview.backgroundcolor = [uicolor orangecolor];
[self.view addsubview:pinchview];
[pinchview release];
首先總結點選移動視圖:
touchview.m
//如果想讓一個視圖對象對觸摸事件作出反應,需要在這個類中的.m檔案中實作跟觸摸相關一些方法
//當手指在視圖範圍内移動的時候觸發
- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event{
// self.backgroundcolor = [uicolor randomcolor];
self.center = cgpointmake(arc4random_uniform(220 - 100 + 1) + 100, arc4random_uniform(468 - 100 +1) +100);
//取出手指對像
uitouch *f = [touches anyobject];
//目前手指的 位置
cgpoint location = [f locationinview:self];
//之前手指位置
cgpoint previouslocation = [f previouslocationinview:self];
cgpoint center = self.center;
cgfloat dx = location.x - previouslocation.x;
cgfloat dy = location.y - previouslocation.y;
self.center = cgpointmake(center.x + dx, center.y + dy);
nslog(@"%s",__function__);
- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event
{
//1.擷取手指對象
uitouch *finger = [touches anyobject];
//2.擷取手指目前在自身視圖的位置
cgpoint currentlocation = [finger locationinview:self];
//3.擷取手指之前在視圖上的位置
cgpoint previouslocation = [finger previouslocationinview:self];
//4.計算手指在x軸和y軸上的增量
//手指在x軸上的增量
cgfloat dx = currentlocation.x - previouslocation.x;
//手指在y軸上的增量
cgfloat dy = currentlocation.y - previouslocation.y;
//擷取中心點,之前center的位置
}
@end
——————————————————————————————————————————
第二種類型捏合視圖對象:
首先要把預設的單觸點關掉
pinchview.m 繼承自uiview
-(instancetype)initwithframe:(cgrect)frame{
if (self = [super initwithframe:frame]) {
//ios支援多點觸摸,隻是預設是單點觸摸
self.multipletouchenabled = yes;
}
return self;
//如果集合中touch集合中手指對象個數為1,就直接傳回touchsmoved方法
if (1 == touches.count) {
return;//結束方法
}else{
//得到集合中所有手指對象,并使用數組存儲(數組有序)
nsarray *alltouchs = [touches allobjects];
//取出兩個觸點
uitouch *touch1 = [alltouchs firstobject];
uitouch *touch2 = [alltouchs lastobject];
//求出兩個手指對象目前的位置
cgpoint firsttouch1 = [touch1 locationinview:self];
cgpoint secondtouch2 = [touch2 locationinview: self];
//求出目前兩個手指的間距
cgfloat currentdistance = [self distancefromefirstpoint:firsttouch1 secondpoint:secondtouch2];
//求出之前兩個手指的位置
cgpoint previousfirstpoint = [touch1 previouslocationinview:self];
cgpoint previoussecondpoint = [touch2 previouslocationinview:self];
//求出之前兩個點的距離
cgfloat previousdistance = [self distancefromefirstpoint:previousfirstpoint secondpoint:previoussecondpoint];
//擷取捏合前後兩個手指間距的比例
cgfloat rate = currentdistance / previousdistance;
//縮放視圖,如果視圖的大小發生變化時,而中心點的位置不發生變化,修改bounds就可以了
self.bounds = cgrectmake(0, 0, self.bounds.size.width * rate, self.bounds.size.height * rate);
//封裝一個計算距離的方法(sqrt求開方)
- (cgfloat)distancefromefirstpoint : (cgpoint)firstpoint secondpoint : (cgpoint)secondpoint{
cgfloat dx = firstpoint.x - secondpoint.x;
cgfloat dy = firstpoint.y - secondpoint.y;
//目前兩點距離
return sqrt(dx * dx + dy * dy);
================================================================================
響應者鍊:
appdelegate.m
//建立 responder對象
responderviewcontroller *respondervc = [[responderviewcontroller alloc]init];
//将respondervc指定為根控制器
self.window.rootviewcontroller = respondervc;
[respondervc release];
responderviewcontroller.m
1、 uiresponder 響應者類,繼承自nsobject,它是一個響應者的基類,它提供了一些處理事件的方法
//什麼是響應者(響應者對象):1.繼承自uiresponder 2.能對ios事件中(觸摸事件,晃動事件,遠端事件)做出響應的對象就叫做響應者
// uilabel ,uiimageview 預設使用者互動是關閉的
響應者鍊:确定事件作用的對象時,uiappcation --->uiappcationdelegate--->window--->視圖控制器--->視圖控制器上的子視圖
響應事件:(有大到小)視圖控制器上的子視圖--->視圖控制器--->window--->uiappcationdelegate---uiappcation 如果都不處理這個事件,事件就會被丢棄
responderview.m
responderview *redview = [[responderview alloc]initwithframe:[uiscreenmainscreen].bounds];
redview.tag = 101;
redview.userinteractionenabled = no;
redview.backgroundcolor = [uicolor redcolor];
[self.view addsubview:redview];
[redview release];
responderview *yellowview = [[responderview alloc]initwithframe:cgrectmake(30, 360, 320, 284)];
yellowview.tag = 102;
//關閉使用者的互動造成響應者鍊就到這個位置斷開了,事件隻能找他的上一級處理,如果上一級都不處理,此事件就不了了之了
yellowview.userinteractionenabled = yes;
yellowview.backgroundcolor = [uicolor yellowcolor];
[redview addsubview:yellowview];
[yellowview release];
responderview *greenview = [[responderview alloc]initwithframe:cgrectmake(20, 20, 280, 244)];
greenview.tag = 103;
greenview.backgroundcolor = [uicolor greencolor];
[yellowview addsubview:greenview];
[greenview release];
responderview *blueview = [[responderview alloc]initwithframe:cgrectmake(20, 20, 240, 204)];
blueview.tag = 104;
blueview.backgroundcolor = [uicolor bluecolor];
[greenview addsubview:blueview];
[blueview release];
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event{
switch (self.tag) {
case 101:
nslog(@"紅色視圖");
break;
case 102:
nslog(@"黃色視圖");
case 103:
nslog(@"綠色視圖");
case 104:
nslog(@"藍色視圖");
default:
==============================================
歡迎學習本文,未經部落客許可,禁止轉載!