iPhone開發 多點觸控的問題

雖然這個問題很簡單,可是對於我這接觸兩天的菜鳥來講也弄了好久,網上又找不到相關的解決方法,避免其餘人和我同樣,仍是記錄一下 code

通常網上找到的教程是這麼教: 教程

-(void )touchesBegin:(NSSet *)touches withEvent:(UIEvent *)event 編譯器

{ NSArray *twoTouches = [touches allObjects]; UITouch *firstTouch = [twoTouches objectAtIndex:0]; UITouch *secondTouch = [twoTouches objectAtIndex:1]; CGPoint point1 =[touch1 locationInView:[touch1 view]]; CGPoint point2 =[touch2 locationInView:[touch2 view]]; NSLog(@"point1:%@",NSStringFromCGPoint(point1)); NSLog(@"point2:%@",NSStringFromCGPoint(point2)); }


可是這裏面首先NSArray這個累不知道能不能去NSSet這個類的東西 it

接着最主要的問題是touchesBegin這個方法在你第一隻手指觸碰就當即觸發這方法。 io

因此你想去兩個手指的話這確定不行,雖然編譯器不會報錯,可是一碰就死機 編譯

其實只要改個方法就行,用touchesMoved event

-(void )touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches];
    if(allTouches.count ==2)
    {
    UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
    UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
    
    CGPoint point1 =[touch1 locationInView:[touch1 view]];
    CGPoint point2 =[touch2 locationInView:[touch2 view]];

    NSLog(@"point1:%@",NSStringFromCGPoint(point1));
    NSLog(@"point2:%@",NSStringFromCGPoint(point2));
    }

}




哈哈,在這裏謝謝@彭博的指點~
相關文章
相關標籤/搜索