ios開發之多點觸摸


  一般,Tap、pinch,pan、swipe只是一個簡單的單個觸摸,它有一定的局限性,所以多點觸摸誕生了~為實現多點觸摸,首先得做下列事情

  • 設置view的屬性multipleTouchEnabled = YES(注意了。。。默認值是NO);
  • 使用CFDictionaryRef來保存觸摸過程的參數 

對於使用多點觸摸處理事件,你必須頻繁地存儲以后進行觸摸比較的觸摸狀態。例如,你要比較每個觸摸的結束點位置和原始位置,你可以在touchesBegan:withEvent: 方法中獲取每個觸摸的原始位置(使用locationInView:方法),然后存儲在CFDictionaryRef對象中,使用UITouch對象地址作為KEY。然后你可以在touchesEnded:withEvent: 方法中取出原始點,和當前點進行比較。

需要注意的是這里使用CFDictionaryRef對象而不是NSDitionary對象,因為UITouch類沒有實現NSCopying協議。

代碼
 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 2      [self cacheBeginPointForTouches:touches];
 3 }
 4 - (void)cacheBeginPointForTouches:(NSSet *)touches {
 5     if ([touches count] > 0) {
 6         for (UITouch *touch in touches) {
 7             CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints,
 8 touch);
 9 } }
10     if (point == NULL) {
11         point = (CGPoint *)malloc(sizeof(CGPoint));
12         CFDictionarySetValue(touchBeginPoints, touch, point);
13 }
14     *point = [touch locationInView:view.superview];
15 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM