简体   繁体   中英

Why this code doens't work??? trying to change image from UIImageView if swiped right or left

I'm getting crazy why the code below doesn't work...

I have a simple UIViewController with UIImageView and want to try to change image if the user swiped right or left. I'm new with iphone development;

thanks

#import "SummerViewController.h"


@implementation SummerViewController

//@synthesize scroll;
@synthesize imgClothes, images;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


-(void)LeftSwiped
{
    NSLog(@"swiped left");
    imgClothes.image = [UIImage imageNamed:[images objectAtIndex:0]];

}

-(void)RightSwiped
{
    NSLog(@"swiped right");
    imgClothes.image = [UIImage imageNamed:[images objectAtIndex:1]];
}

- (void)viewDidLoad
{

    images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpg"],
                       [UIImage imageNamed:@"2.jpg"], nil];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(LeftSwiped)];

    swipeLeft.numberOfTouchesRequired = 1;
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(RightSwiped)];

    swipeRight.numberOfTouchesRequired = 1;
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];





    [super viewDidLoad];


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

.h file:

@interface SummerViewController : UIViewController
{
    //IBOutlet UIScrollView *scroll;
    IBOutlet UIImageView *imgClothes;
    NSArray *images;
}

@property (nonatomic, retain) IBOutlet UIImageView *imgClothes;
@property (nonatomic, retain) IBOutlet NSArray *images;

@end
  1. I can't see where are you initing your UIImageView *imgClothes . You should write something like imgClothes = [[UIImageView alloc] init(...)];
  2. I didn't work with UISwipeGestureRecognizer, but sometimes you should write in your .h file something like this: @interface SummerViewController : UIViewController <UIGestureRecognizerDelegate>
  3. I prefer do my initialization in initWithNibName , not in ViewDidLoad . But this should not be cause of your problems.

All you code is correct for changing an Image for UIImageView.According to your code snippet I found two scenarios

1.Make sure to add IBOutlet reference from the XIB to the imgClothes
2.if "1" point is conformed try to check your imgClothes reference added to self.view or not.

Hope above two scenarios will make it out of the issue.Good luck.

Don't forget one things

1) swipeRight.delegate = self; // assign delegate to swipe object.

If you're using sdk 3.2 or higher, this is dead easy with the UIGestureRecognizer class.Otherwise visit following reference for detect swipe

https://gist.github.com/791725

Hope, this will help you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM