designmodeler : 設計模式 gesturerecginzer:手勢識别
作者:韓俊強
1、第一種設計模式
delegateviewcontroller.m
- (void)viewdidload {
[super viewdidload];
//改變自身顔色
actionview *redview = [[actionview alloc]initwithframe:cgrectmake(20, 20, 280, 100)];
redview.tag = 101;
[ redview addtarget:self action:@selector(changeselfbackground :)];
redview.backgroundcolor = [uicolor randomclolor];
[self.view addsubview:redview];
[redview release];}
#pragma mark --target....action 設計模式的方法實作
//改變自身顔色
- (void)changeselfbackground : (actionview *)view{
view.backgroundcolor = [uicolor randomclolor];
}
actionview.h
@interface actionview : uiview
//給外界提供一個接口(方法),用來給actionview 制定事件的響應對象(目标 target),以及target目标響應的方法
- (void)addtarget : (id)target action:(sel)action;
@end
actionview.m
@interface actionview ()
{
id _target;//存儲傳入的響應對象
sel _action;//存儲響應對象執行的方法
@implementation actionview
actionview 是自定義的視圖,我們想把它封裝成完成的類,一個完整的類,就是無論你再有什麼樣的需求,都無需去修改它的源檔案
以前的處理方式不夠靈活,因為actionview建立的對象沒接到觸摸消息,都要自己去處理事件,是以每建立一個對象,提出一個新的需求都要區修改它的源檔案,此時actionview對象和事件就捆綁到一起了,此時耦合性太強
通過target ...action設計模式,将事件交由其他對象處理,讓我們的actionview像uibutton一樣靈活,此時它隻需負責通過目标幹活就可以了,此時的actionview對象和事件就不再捆綁到一起了,耦合性降低了,内聚就升高了
- (void)addtarget : (id)target action:(sel)action{
//此時在這個方法中要把外界傳進來的目标對象,和目标對象要執行的方法存儲起來
_target = target;
_action = action;
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event{
//開始觸摸,當actionview 對象接受到觸摸事件的時候,自己不處理事件,需要由_target 來處理
//目标選擇方法去為self執行事件,而且方法中如果有參數,參數就指的是addtarget ...action 方法的調用
[_target performselector:_action withobject:self];
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)
}===================================================
2、第二種模式: 使用代理和設計模式,完成touchview的觸摸事件的響應操作,其實就是通過這種方式解除 事件和touchview的耦合
使用代理和協定步驟:
1.制訂協定(代理要完成的任務)
2.定義代理屬性(存儲代理對象)
3.在其他檔案中指定touchview的代理對象 (幫touchview幹活)
4.讓代理對象服從協定(代理對象答應幫touchview)幹活
5.讓代理對象實作協定中的方法(代理對象知道怎麼去幹活)
6.委托人通知代理對象什麼時候執行協定中的方法(通知代理去幹活)
touchview.h
//第一步:制定協定
@class touchview;
@protocol touchviewdelegate <</span>nsobject>
@optional
//剛開始觸摸的時候讓代理對象完成這個方法
- (void)touchbeganwithtouchview : (touchview *)touchview;
@interface touchview : uiview
//第二步:第一代理屬性
————————————————————————————
touchview.m
@implementation touchview
//第六步:讓代理對象幹活
if ([_delegate respondstoselector:@selector(touchbeganwithtouchview:)]) {
[_delegate touchbeganwithtouchview:self];}}
————————————————————————————
delegateviewcontroller.m
//第四步:讓代理對象遵循協定
@interface delegateviewcontroller ()<</span>touchviewdelegate>
@implementation delegateviewcontroller
self.view.backgroundcolor = [uicolor yellowcolor];
touchview *redview = [[touchview alloc]initwithframe:cgrectmake(20, 30, 280, 100)];
redview.backgroundcolor = [uicolor redcolor];
//第三步:指定代理對象
redview.delegate = self;
[redview release];
//第五步:實作協定中的方法
- (void)touchbeganwithtouchview:(touchview *)touchview{
touchview.backgroundcolor = [uicolor randomclolor];//随機顔色自己做一下封裝
======================================================================
3、gesturerecginzer:手勢識别
appdelegate.m
gestureviewcontroller *gesture = [[gestureviewcontrolleralloc]init];
self.window.rootviewcontroller = gesture;
[gesture release];
——————————————————————
gestureviewcontroller.m
#import "gestureviewcontroller.h"
#import "uicolor+randomcolor.h"
self.view.backgroundcolor = [uicolor colorwithpatternimage:[uiimageimagenamed:@"444.jpg"]];
// uigesturerecognizer 手勢識别器的基類 ,拓為我們提供了手勢識别器的一些基本的功能,我們在螢幕上的手勢全部由手勢識别器來完成識别,此時我們隻需要關心手勢識别之後應該做出什麼處理 ,它由6大子類,還有一個孫子類(螢幕邊緣手勢,是平移手勢的子類)
uiview *redview = [[uiview alloc]initwithframe:cgrectmake(0, 20, 320, 500)];
1、輕拍手勢
uitapgesturerecognizer *tapgesture = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(handletap :)];
//配置屬性
//設定輕拍手勢觸發時間所需的輕拍次數
tapgesture.numberoftapsrequired = 1;//預設為1下
//設定輕拍需要的手指對象個數
tapgesture.numberoftouchesrequired = 2;
//給redview添加輕拍的手勢對象
[redview addgesturerecognizer:tapgesture];
[tapgesture release];
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)
#pragma mark 輕拍手勢的方法實作
- (void)handletap : (uitapgesturerecognizer *)tapgesture{
tapgesture.view.backgroundcolor = [uicolor randomclolor];}
————————————————————————————————————————
2、長按手勢uilongpressgesturerecognizer
uilongpressgesturerecognizer *longgesture = [[uilongpressgesturerecognizer alloc]initwithtarget:self action:@selector(handlelongpress :)];
//設定長按手勢最小多長時間觸發方法
longgesture.minimumpressduration = 1;//預設為.05秒
//在視圖上添加手勢對象
[redview addgesturerecognizer:longgesture];
//釋放
[longgesture release];
#pragma mark 長按手勢方法實作
- (void) handlelongpress : (uilongpressgesturerecognizer *)longpress{
//改變手勢所在視圖的父視圖的顔色
//根據手勢狀态,選擇執行相應操作
if (longpress.state == uigesturerecognizerstatebegan) {
longpress.view.superview.backgroundcolor = [uicolor randomclolor];
}
————————————————————————
3、輕掃手勢uiswipegesturerecognizer
uiswipegesturerecognizer *swipegesture = [[uiswipegesturerecognizer alloc]initwithtarget:selfaction:@selector(handleswipe:)];
//設定輕掃的方向
swipegesture.direction = uiswipegesturerecognizerdirectionright;
[redview addgesturerecognizer:swipegesture];
[swipegesture release];
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)
#pragma mark 輕掃手勢方法的實作
- (void)handleswipe: (uiswipegesturerecognizer *)swipegesture{
swipegesture.view.backgroundcolor = [uicolor randomclolor];
————————————————————————-
4、平移手勢uipangesturerecognizer
uipangesturerecognizer *pangesture = [[uipangesturerecognizer alloc]initwithtarget:selfaction:@selector(handlepan : )];
[redview addgesturerecognizer:pangesture];
[pangesture release];
#pragma mark 平移手勢方法的實作
- (void)handlepan : (uipangesturerecognizer *)pangesture{
//1.擷取平移增量
cgpoint point = [pangesture translationinview:pangesture.view];
//2.仿射變換(變形)
//第一個參數:變形之前的視圖位置和大小
//第二個參數:x軸上的形變量(x軸上的增量)
//第三個參數:y軸上的形變量(y軸上的增量)
pangesture.view.transform = cgaffinetransformtranslate(pangesture.view.transform,point.x, point.y);
//3.将之前的增量清0;
//cgpointzero 代表{0,0}點 cgpointmake(0,0);
[pangesture settranslation:cgpointzero inview:pangesture.view];
————————————————————————————————
5、捏合手勢 uipinchgesturerecognizer
uipinchgesturerecognizer *pinchgesture = [[uipinchgesturerecognizer alloc]initwithtarget:selfaction:@selector(handlepinch :)];
[redview addgesturerecognizer:pinchgesture];
[pinchgesture release];
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)
#pragma mark 捏合手勢方法的實作
- (void)handlepinch : (uipinchgesturerecognizer *)pinchgesture{
pinchgesture.view.transform = cgaffinetransformscale(pinchgesture.view.transform, pinchgesture.scale, pinchgesture.scale);
//scale 縮放比例
//将之前的形變量置為1;
pinchgesture.scale = 1;
6.旋轉手勢 uirotationgesturerecognizer
uirotationgesturerecognizer *rotationgesture = [[uirotationgesturerecognizeralloc]initwithtarget:self action:@selector(handlerotationgesture : )];
[redview addgesturerecognizer:rotationgesture];
[rotationgesture release];
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)
#pragma mark 旋轉手勢方法的實作
- (void)handlerotationgesture : (uirotationgesturerecognizer *)rotationresture{
rotationresture.view.transform = cgaffinetransformrotate(rotationresture.view.transform, rotationresture.rotation);
//将之前的角度增量置為0
rotationresture.rotation = 0;
——————————————————————————————
7.螢幕邊緣手勢uiscreenedgepangesturerecognizer
//螢幕邊緣的手勢必須和硬體裝置的邊緣重合,此時這個手勢才有作用
uiscreenedgepangesturerecognizer *screengesture = [[uiscreenedgepangesturerecognizeralloc]initwithtarget:self action:@selector(handlescreengesture: )];
//設定螢幕的邊緣
screengesture.edges = uirectedgeleft;
[redview addgesturerecognizer:screengesture];
[screengesture release];
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)
#pragma mark 螢幕邊緣手勢方法的實作
- (void) handlescreengesture : (uiscreenedgepangesturerecognizer *)screen{
nslog(@"小樣你能行嗎?");
=================================================
歡迎學習本文,未經部落客允許,禁止轉載!