- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftHandle:)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; [[self view] addGestureRecognizer:swipeLeft]; [swipeLeft release]; } -(void) swipeLeftHandle:(UISwipeGestureRecognizer *) sender { if (sender.direction == UISwipeGestureRecognizerDirectionLeft) lblResult.text = @"swipe left"; NSLog(@"swipe left"); }
// // ViewController.h // swipeGesture // // Created by Weerachai on 11/17/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UILabel *lblResult; } @end
// // ViewController.m // swipeGesture // // Created by Weerachai on 11/17/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftHandle:)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; [[self view] addGestureRecognizer:swipeLeft]; [swipeLeft release]; UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightHandle:)]; swipeRight.direction = UISwipeGestureRecognizerDirectionRight; [[self view] addGestureRecognizer:swipeRight]; [swipeRight release]; UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUpHandle:)]; swipeUp.direction = UISwipeGestureRecognizerDirectionUp; [[self view] addGestureRecognizer:swipeUp]; [swipeUp release]; UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDownHandle:)]; swipeDown.direction = UISwipeGestureRecognizerDirectionDown; [[self view] addGestureRecognizer:swipeDown]; [swipeDown release]; } -(void) swipeLeftHandle:(UISwipeGestureRecognizer *) sender { if (sender.direction == UISwipeGestureRecognizerDirectionLeft) lblResult.text = @"swipe left"; NSLog(@"swipe left"); } -(void) swipeRightHandle:(UISwipeGestureRecognizer *) sender { if (sender.direction == UISwipeGestureRecognizerDirectionRight) lblResult.text = @"swipe right"; NSLog(@"swipe right"); } -(void) swipeUpHandle:(UISwipeGestureRecognizer *) sender { if (sender.direction == UISwipeGestureRecognizerDirectionUp) lblResult.text = @"swipe up"; NSLog(@"swipe up"); } -(void) swipeDownHandle:(UISwipeGestureRecognizer *) sender { if (sender.direction == UISwipeGestureRecognizerDirectionDown) lblResult.text = @"swipe down"; NSLog(@"swipe down"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [lblResult release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท