2012年9月7日 星期五

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

此為前一篇的改良版,加入了tap的功能

A. 右上點擊會到第一頁,同向上滑動
B. 左上點擊會到第五頁,同向下滑動
C. 右下點擊會到下一頁,同向右滑動
D. 左下點擊會到前一頁,同向左滑動

只有改動mySwipeViewController.m的程式碼
@interface mySwipeViewController ()

@end

@implementation mySwipeViewController
@synthesize bookImage;
@synthesize pageLabel;
@synthesize actionLabel;
@synthesize pages;

int currentTag = 1;
int lastTag = 1;

- (void) screenWasSwipe: (UISwipeGestureRecognizer *)recognizer {
   
   
    if (recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
        actionLabel.text = @"動作:向下滑動";
        currentTag =5;
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
        actionLabel.text = @"動作:向上滑動";
        currentTag =1;
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        actionLabel.text = @"動作:向右滑動";
        currentTag ++;
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        actionLabel.text = @"動作:向左滑動";
        currentTag --;
    }
    else {
        actionLabel.text = @"Not thing";
    }
   
    if (currentTag >=6)
        currentTag = 1;
   
    if (currentTag ==0)
        currentTag = 5;
   
    NSString *currentName = [pages objectAtIndex:currentTag-1];
   
    [bookImage setImage:[UIImage imageNamed:currentName]];
   
    [bookImage setUserInteractionEnabled:YES];
       
   
    bookImage.tag = currentTag;
   
   
    if (lastTag != currentTag)
       [self flipAnimation:currentTag action:recognizer];   
   
    lastTag = currentTag;
   
    pageLabel.text = [NSString stringWithFormat:@"頁碼:%d", currentTag ];
   
}


- (void) screenWasTap:(NSInteger *) tapLocation
{
   
    if (tapLocation == 1) {
        actionLabel.text = @"動作:向上滑動";
        currentTag =1;
    }
    else if (tapLocation == 2) {
        actionLabel.text = @"動作:向下滑動";
        currentTag =5;
    }
    else if (tapLocation == 3) {
        actionLabel.text = @"動作:向左滑動";
        currentTag --;
    }
    else if (tapLocation == 4) {
        actionLabel.text = @"動作:向右滑動";
        currentTag ++;
    }
    else {
        actionLabel.text = @"Not thing";
    }
   
    if (currentTag >=6)
        currentTag = 1;
   
    if (currentTag ==0)
        currentTag = 5;
   
    NSString *currentName = [pages objectAtIndex:currentTag-1];
   
    [bookImage setImage:[UIImage imageNamed:currentName]];
   
    [bookImage setUserInteractionEnabled:YES];
   
   
    bookImage.tag = currentTag;
   
    if (lastTag != currentTag)
       [self flipAnimationForTap:currentTag tapAction:tapLocation];
   
    lastTag = currentTag;
   
    pageLabel.text = [NSString stringWithFormat:@"頁碼:%d", currentTag ];
   
}


- (void) flipAnimation:(NSInteger *)currentTag action:(UISwipeGestureRecognizer *)recognizer
{
    //翻頁動畫效果
    UIView *currentView=[self.view viewWithTag:currentTag];
   
    [UIView beginAnimations:@"animationID" context:nil];
    [UIView setAnimationDuration:0.7f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
   

    if (recognizer.direction == UISwipeGestureRecognizerDirectionDown)
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp)
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
   // else
   //     actionLabel.text = @"Not thing";
   
    [currentView removeFromSuperview];
    [self.view addSubview:bookImage];
   
    [UIView commitAnimations];
}

- (void) flipAnimationForTap:(NSInteger *)currentTag tapAction:(NSInteger *)action
{
    //翻頁動畫效果
    UIView *currentView=[self.view viewWithTag:currentTag];
   
    [UIView beginAnimations:@"animationID" context:nil];
    [UIView setAnimationDuration:0.7f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
   
    if (action == 1)
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    else if (action == 2)
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    else if (action == 3)
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    else if (action == 4)
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    // else
    //     actionLabel.text = @"Not thing";
   
    [currentView removeFromSuperview];
    [self.view addSubview:bookImage];
   
    [UIView commitAnimations];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
   
   
    // swipe up
    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(screenWasSwipe:)];
   
    swipeUp.numberOfTouchesRequired = 1;
   
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
   
    [self.view addGestureRecognizer:swipeUp];
   
   
   
    // swipe down
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(screenWasSwipe:)];
   
    swipeDown.numberOfTouchesRequired = 1;
   
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
   
    [self.view addGestureRecognizer:swipeDown];
   
   
    // swipe Right
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(screenWasSwipe:)];
   
    swipeRight.numberOfTouchesRequired = 1;
   
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
   
    [self.view addGestureRecognizer:swipeRight];
   
   
   
    // swipe Left
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(screenWasSwipe:)];
   
    swipeLeft.numberOfTouchesRequired = 1;
   
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
   
    [self.view addGestureRecognizer:swipeLeft];
   
   
    pages=[[NSArray alloc] initWithObjects:@"Green Apple.gif",@"Orange.gif",@"Pear.gif",@"Red Apple.gif",@"Strawberry.gif",
           nil];

}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    NSInteger tapLocation =0;
   
    //int count = [[touches allObjects] count];
   
    //計算同一時間有多少觸碰點
    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];
       
   //     movePointLabel.text = [NSString stringWithFormat:@"P2.x = %3.2f, P2.y= %3.2f", p2.x, p2.y];
    }
   
    //countLabel.text = [NSString stringWithFormat:@"count = %d", count];
   
   
    if (p1.x > 200 && p1.y >300)
        [self  screenWasTap:4];
    else if (p1.x <100 && p1.y >300)
        [self  screenWasTap:3];
    else if (p1.x >200 && p1.y <100)
        [self  screenWasTap:1];
    else if (p1.x <100 && p1.y <100)
        [self  screenWasTap:2];
   
   
}

沒有留言: