ユーザーが UIButton を押したときに、UIButton の画像を変更したいと考えています。画像には透明効果があります (エッジのみを示す .png です)。問題は、ボタンがタップされると、すべてが正常に機能する (それに接続されている他の機能) ことですが、アプリは 2 つの異なる画像を 1 秒ごとに上書きします! ボタンが押されると、2番目の画像を表示し、最初の画像を削除します(不要になります)。PS: 画像の配列を使用したくありません。ボタンのコードは次のとおりです。
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];
}}
同じボタンを使用したいが、表示する画像が異なります