我想在用戶按下UIButton時更改UIButton上的圖像。該圖像具有透明效果(這是一個僅顯示邊緣的.png)。問題是,當輕按按鈕時,所有工作都正常(與其相關的其他功能),但應用程序會在第二秒內覆蓋2個不同的圖像!當按下按鈕時,我想顯示第二張圖片,並刪除第一張(這是不必要的)。 PS:我不想使用圖像陣列。在此按鈕的代碼:將UIButton圖像更改爲另一個目標C
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //just graphics
[overlayButton addTarget:self action:@selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside]; //just to set a variable i++ for my cycle
[overlayButton addTarget:self action:@selector(changeshape:) forControlEvents:UIControlEventTouchUpInside]; //function that changes another shape
[[self view] addSubview:overlayButton];
我的簡單循環
- (void)buttonshapeClicked:(id)sender{
shape++;
if (shape == 4) {
shape = 1;
}}
這裏的主要功能,可以改變屏幕上的初級形狀
-(void) changeshape:(id)sender {
if (shape==1 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"passaquadrato.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:@selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:@selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:@"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}
else if (shape==2 && people==2) {
//bottone forma
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"passarettangolo.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:@selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
[overlayButton addTarget:self action:@selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];
//bottone people
UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButtonp setImage:[UIImage imageNamed:@"passatre.png"] forState:UIControlStateNormal];
[overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
[overlayButtonp addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButtonp];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"square2.png"]];
[overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
[[self view] addSubview:overlayImageView];
}}
我想有同樣的按鈕,但有不同的圖像顯示
是的,我已經嘗試過!所以,現在我寫了一個方法,從視圖中清除所有內容。我知道這不是最好的解決方案,但它是一個解決方案。 :)現在的問題是,當我嘗試重新初始化循環時,它不起作用,因爲我有另一個概述問題! – TheInterestedOne
在我能提供幫助之前,你必須解釋一下你所說的「概覽問題」。細節很重要。 – CrimsonDiego
問題是,當我嘗試重新初始化我的變量時,我已經解決了同樣的問題。這裏是重新初始化我的變量的方法。這是對的嗎? - (void)buttonshapeClicked(id)sender // PROBLEMA DOPO UN CICLO COMPLETO shape ++;如果(shape == 4){shape = 1; } } – TheInterestedOne