简体   繁体   English

以编程方式按下UIButton时在UIImageView中更改图像

[英]Change image in UIImageView when UIButton is pressed programmatically

I am trying to change the image inside an imageview when a UIButton is pressed. 我试图在按下UIButton时更改imageview内的图像。 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: . 一个问题是您说的是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. performSelectorOnMainThread:当您已经在主线程上时,实际上可能会导致延迟,因为现在我们必须等待主线程变为空闲后才能运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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