// // ViewController.h // imageGesture // // Created by Weerachai on 11/17/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UIImageView *imgView; } @end
// // ViewController.m // imageGesture // // Created by Weerachai on 11/17/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () { NSMutableArray *myObject; int i; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. i = 0; myObject = [[NSMutableArray alloc] init]; [myObject addObject:@"girl1.jpg"]; [myObject addObject:@"girl2.jpg"]; [myObject addObject:@"girl3.jpg"]; [myObject addObject:@"girl4.jpg"]; imgView.image = [UIImage imageNamed:@"girl1.jpg"]; 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]; } -(void) swipeRightHandle:(UISwipeGestureRecognizer *) sender { if (sender.direction == UISwipeGestureRecognizerDirectionRight) { i = i - 1; if(i < 0) { i = myObject.count - 1; } NSString *arrValue = [myObject objectAtIndex:i]; imgView.image = [UIImage imageNamed:arrValue]; NSLog (@"%@" , [NSString stringWithFormat:@"Hello %@",arrValue]); } } -(void) swipeLeftHandle:(UISwipeGestureRecognizer *) sender { if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { i = i + 1; if(i >= myObject.count) { i = 0; } NSString *arrValue = [myObject objectAtIndex:i]; imgView.image = [UIImage imageNamed:arrValue]; NSLog (@"%@" , [NSString stringWithFormat:@"Hello %@",arrValue]); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [imgView release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท