2012年9月6日 星期四

IOS手指辨識基本實作(四)

雙指辨識基本使用



1. 沿用前一篇的例子來修改,將storyboard修改如下


2. 將新加入的Label註冊到myTouchViewController.h

@interface myTouchViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *startPointLabel;
@property (weak, nonatomic) IBOutlet UILabel *startPoint2Label;


@property (weak, nonatomic) IBOutlet UILabel *movePointLabel;
@property (weak, nonatomic) IBOutlet UILabel *movePoint2Label;


@property (weak, nonatomic) IBOutlet UILabel *endPointLabel;
@property (weak, nonatomic) IBOutlet UILabel *endPoint2Label;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@end

3. 將storyboard中的 UIView的multipleTouch打開。有兩種方法,分別是右上的MultipleTouch勾選,或是在viewDiwLoad中加入self.view.multipleTouchEnabled=YES。


4. 修改myTouchViewController.m

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    /*
    int count = [[touches allObjects] count];
   
    CGPoint point = [[touches anyObject] locationInView:self.view];
    startPointLabel.text = [NSString stringWithFormat:@"起點 x= %3.2f, y= %3.2f", point.x, point.y];
    */
   
   
    //計算同一時間有多少觸碰點
    int count = [[touches allObjects] count];
   
    //從陣列中取得第一個觸碰點(編號0)
    CGPoint p1 = [[[touches allObjects] objectAtIndex:0]locationInView:self.view];
   
   
    startPointLabel.text = [NSString stringWithFormat:@"起始點 P1.x = %3.2f, P1.y= %3.2f", p1.x, p1.y];
   
    if (count ==2 ) {
        //從陣列中取得第二個觸碰點(編號1)
        CGPoint p2 = [[[touches allObjects] objectAtIndex:1]locationInView:self.view];
       
        startPoint2Label.text = [NSString stringWithFormat:@"起始點 P2.x = %3.2f, P2.y= %3.2f", p2.x, p2.y];
    }
   
    countLabel.text = [NSString stringWithFormat:@"count = %d", count];
   
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    /*
    CGPoint point = [[touches anyObject] locationInView:self.view];
    movePointLabel.text = [NSString stringWithFormat:@"現在點 x= %3.2f, y= %3.2f", point.x, point.y];
    */
   
    //計算同一時間有多少觸碰點
    int count = [[touches allObjects] count];
   
    //從陣列中取得第一個觸碰點(編號0)
    CGPoint p1 = [[[touches allObjects] objectAtIndex:0]locationInView:self.view];
   
   
    movePointLabel.text = [NSString stringWithFormat:@"移動點 P1.x = %3.2f, P1.y= %3.2f", p1.x, p1.y];
   
    if (count ==2 ) {
        //從陣列中取得第二個觸碰點(編號1)
        CGPoint p2 = [[[touches allObjects] objectAtIndex:1]locationInView:self.view];
       
        movePoint2Label.text = [NSString stringWithFormat:@"移動點 P2.x = %3.2f, P2.y= %3.2f", p2.x, p2.y];
    }
   
   
}


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    /*
    CGPoint point = [[touches anyObject] locationInView:self.view];
    endPointLabel.text = [NSString stringWithFormat:@"終點 x = %3.2f, y= %3.2f", point.x , point.y];
    */
   
    //計算同一時間有多少觸碰點
    int count = [[touches allObjects] count];
   
    //從陣列中取得第一個觸碰點(編號0)
    CGPoint p1 = [[[touches allObjects] objectAtIndex:0]locationInView:self.view];
   
   
    endPointLabel.text = [NSString stringWithFormat:@"結束點 P1.x = %3.2f, P1.y= %3.2f", p1.x, p1.y];
   
    if (count ==2 ) {
        //從陣列中取得第二個觸碰點(編號1)
        CGPoint p2 = [[[touches allObjects] objectAtIndex:1]locationInView:self.view];
       
        endPoint2Label.text = [NSString stringWithFormat:@"結束點 P2.x = %3.2f, P2.y= %3.2f", p2.x, p2.y];
    }
   
   
}




5. 執行結果如下


其中兩點觸控的操作可參考

IOS模擬器操作

沒有留言: