I am trying to change the image inside an imageview when a UIButton is pressed. I need at least three or 4 clicks before the image is changed. What is wrong ?
Here is the code I am using
@interface ARViewController ()
@property (nonatomic, strong) NSMutableArray *pepsiPhotos;
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation ARViewController
{
IBOutlet UIView *overlayView;
int currentImageIndex;
}
@synthesize imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
_pepsiPhotos = [NSMutableArray arrayWithObjects:@"pepsican.jpeg",
@"pepsilight.jpeg", @"7up.jpeg", @"mirinda.jpeg", nil];
overlayView = [[UIView alloc] initWithFrame:CGRectMake(50.f, 80.f, 240.f, 275.f)];
[overlayView setHidden:YES];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.f, 20.f, 200.f, 150.f)];
[overlayView addSubview:imageView];
[self.view addSubview:overlayView];
currentImageIndex = 0;
[self showImages];
}
-(void) showImages
{
//
[overlayView setHidden:NO];
[overlayView setBackgroundColor:[UIColor clearColor]];
[overlayView setUserInteractionEnabled:YES];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
nextButton.frame = CGRectMake(165.f, 260.f, 40.f, 40.f);
[nextButton setTitle:@"Next" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchDown];
[nextButton setUserInteractionEnabled:YES];
[overlayView addSubview:nextButton];
[overlayView bringSubviewToFront:nextButton];
UIButton *previousButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
previousButton.frame = CGRectMake(0, 260.f, 40.f, 40.f);
[previousButton setTitle:@"Last" forState:UIControlStateNormal];
[previousButton addTarget:self action:@selector(previousButtonPressed:) forControlEvents:UIControlEventTouchDown];
[previousButton setUserInteractionEnabled:YES];
[overlayView addSubview:previousButton];
[imageView setImage:[UIImage imageNamed:[_pepsiPhotos objectAtIndex:currentImageIndex]]];
[overlayView addSubview:imageView];
[self.view addSubview:overlayView];
}
-(IBAction)nextButtonPressed:(id)sender
{
if (currentImageIndex < [_pepsiPhotos count] -1)
{
currentImageIndex++;
[self performSelectorOnMainThread:@selector(showNextImage) withObject:nil waitUntilDone:NO];
}
}
-(IBAction)previousButtonPressed:(id)sender
{
if (currentImageIndex > 0)
{
currentImageIndex--;
[self performSelectorOnMainThread:@selector(showNextImage) withObject:nil waitUntilDone:NO];
}
}
-(void)showNextImage
{
[imageView setImage:[UIImage imageNamed:[_pepsiPhotos objectAtIndex:currentImageIndex]]];
[overlayView addSubview:imageView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
One problem is that you are saying performSelectorOnMainThread:
. If this is an action method from your button, you are on the main thread. Just respond however you want to respond.
Saying performSelectorOnMainThread:
when you are already on the main thread can actually cause a delay, because now we have to wait for the main thread to become free before we can run the code.
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.