使用復雜的觸摸和手勢
Apple有各種手勢識別器的Class,下面,將使用幾個手勢識別器,實現:輕按、輕掃、張合、旋轉(搖動暫不涉及)。每個手勢都將有一個彈出式窗口的反饋。 在ViewController.m文件中, 1-點擊事件 -(void)foundTap:(UITapGestureRecognizer *)recognizer{ UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:@"tapped" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil]; [alert show]; } - (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *tapRecognizer; //創建一個輕按手勢識別器 tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(foundTap:)]; //初始化識別器,並使用函數指針(方法指針)觸發同時實現方法 tapRecognizer.numberOfTapsRequired=1; //輕按對象次數(1)觸發此行為 tapRecognizer.numberOfTouchesRequired=1; //要求響應的手指數 [self.view addGestureRecognizer:tapRecognizer]; //相應的對象添加控制器 } 仔細觀察實現的方法,均是按著以下步驟:新建一個控制器實例--實例初始化--將其添加到類對象--實現函數指針中的方法(@selector()). 【若想獲得輕按或輕掃手勢坐標,可添加: CGPoint location=[recognizer locationInView:<the view> <the view>即為手勢識別器的視圖名;location有兩個參數x和y】 2-輕掃: 接下來,我們按著上述步驟實現輕掃: 函數指針指向的方法: -(void)foundSwipe:(UISwipeGestureRecognizer *)recognizer{ UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:@"swiped" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } 還是viewDidLoad: UISwipeGestureRecognizer *swipeRecognizer; //創建一個輕掃識別器 swipeRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(foundSwipe:)]; //對象初始化 並有函數指針 swipeRecognizer.direction= UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft; //手勢相應 向左或向右滑動 swipeRecognizer.numberOfTouchesRequired=1; //掃動對象次數 [self.view addGestureRecognizer:swipeRecognizer]; //向對象添加控制器 相應掃動方向時,有四個方向可供選擇: UISwipeGestureRecognizerDirectionRight/Left/Up/Down; 3-張合: 將實現放大縮小 函數指針方法: -(void)foundPinch:(UIPinchGestureRecognizer *)recognizer{ //注意此處的類 NSString *message; double scale; scale=recognizer.scale; //識別器的scale(刻度尺、尺度) //可用scale結合openGLES的glScalef實現縮放 message=[[NSString alloc]initWithFormat:@"縮放:Scale:%1.2f,Velocity:%1.2f",recognizer.scale,recognizer.velocity]; UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } ViewDidLoad中: UIPinchGestureRecognizer *pinchReconizer; //新建一個張合識別器對象 pinchReconizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(foundPinch)]; //初始化對象 [self.view addGestureRecognizer:pinchReconizer]; //添加控制器 4-旋轉 首先Cocoa類使用弧度為單位,角度和弧度的轉換關系: 角度=弧度*180/Pi 函數指針指向的方法: -(void)foundRotation:(UIRotationGestureRecognizer *)recognizer{ NSString *message; double rotation; rotation=recognizer.rotation; //返回一個控制器角度 //在openGLES中可使用rotation結合glRoatef實現旋轉 message=[[NSString alloc] initWithFormat:@"旋轉,Radians:%1.2f,Velocity:%1.2f", recognizer.rotation,recognizer.velocity]; UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } viewDidLoad如下: UIRotationGestureRecognizer *rotationRecognizer; //新建旋轉手勢控制器對象 rotationRecognizer=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(foundRotation:)]; //初始化對象 [self.view addGestureRecognizer:rotationRecognizer]; //添加控制器