簡體   English   中英

以編程方式按下UIButton時在UIImageView中更改圖像

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

我試圖在按下UIButton時更改imageview內的圖像。 更改圖片之前,我至少需要單擊三四次。 怎么了 ?

這是我正在使用的代碼

@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

一個問題是您說的是performSelectorOnMainThread: 如果這是您的按鈕操作方法,你是在主線程 只要回應即可,但您想回應。

performSelectorOnMainThread:當您已經在主線程上時,實際上可能會導致延遲,因為現在我們必須等待主線程變為空閑后才能運行代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM