Iphone 如何更改按钮的背景图像并使其立即更新?
我正在开发一个记忆游戏。当我点击一个按钮时,我想切换背景。当我第二次点击卡片时,我希望背景切换一秒钟,然后回到原来的位置(如果不是正确的卡片)。但是按钮的背景直到我从方法返回(buttonPressed)后才会改变。为什么不立即改变呢?请帮帮我!:) 这是我正在与之合作的班级:Iphone 如何更改按钮的背景图像并使其立即更新?,iphone,objective-c,image,background,uibutton,Iphone,Objective C,Image,Background,Uibutton,我正在开发一个记忆游戏。当我点击一个按钮时,我想切换背景。当我第二次点击卡片时,我希望背景切换一秒钟,然后回到原来的位置(如果不是正确的卡片)。但是按钮的背景直到我从方法返回(buttonPressed)后才会改变。为什么不立即改变呢?请帮帮我!:) 这是我正在与之合作的班级: #import "Button.h" #import <UIKit/UIKit.h> #import <AVFoundation/AVAudioPlayer.h> @class ZogajApp
#import "Button.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@class ZogajAppDelegate;
@interface PairPlayPlayViewController : UIViewController <AVAudioPlayerDelegate> {
ZogajAppDelegate *appDelegate;
AVAudioPlayer *av;
Boolean isPairActive;
Button *firstButton;
Button *secondButton;
}
@property (nonatomic, retain) AVAudioPlayer *av;
@property (nonatomic, retain) Button *firstButton;
@property (nonatomic, retain) Button *secondButton;
- (void)buttonPressed:(id)sender;
- (void)secondCard;
@end
#import "PairPlayPlayViewController.h"
#import "ZogajAppDelegate.h"
#import "Button.h"
#import "AVFoundation/AVAudioPlayer.h"
@implementation PairPlayPlayViewController
@synthesize av, firstButton, secondButton;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"double_card" ofType:@"mp3"];
NSString *newAudioFile2 = [[NSBundle mainBundle] pathForResource:@"right_sound" ofType:@"mp3"];
av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av prepareToPlay];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile2] error:NULL];
[av prepareToPlay];
//av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
//[av prepareToPlay];
isPairActive = FALSE;
appDelegate = (ZogajAppDelegate *)[[UIApplication sharedApplication] delegate];
int position;
for(int i = 0; i < ((appDelegate.numberOfPairs*2)/5); i++){
for (int j = 0; j<5 ;j++){
Button *button = [[Button alloc] init];
//Sätter bild nummer..
position = (arc4random() % [appDelegate.cardsInPlay count]);
button.picNumber = [[appDelegate.cardsInPlay objectAtIndex:position]intValue];
NSLog(@"%d", button.picNumber);
[appDelegate.cardsInPlay removeObjectAtIndex:position];
button.frame = CGRectMake( 3 + (65 * j), 5 + (65 * i), 55, 55);
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
for (int j = 0; j<((appDelegate.numberOfPairs*2) % 5) ;j++){
Button *button = [[Button alloc] init];
//Sätter bild nummer..
position = (arc4random() % [appDelegate.cardsInPlay count]);
button.picNumber = [[appDelegate.cardsInPlay objectAtIndex:position]intValue];
NSLog(@"%d", button.picNumber);
[appDelegate.cardsInPlay removeObjectAtIndex:position];
button.frame = CGRectMake( 3 + (65 * j), 5 + (65 * ((appDelegate.numberOfPairs*2)/5) + 1), 55, 55);
//button.imageView.contentMode = UIViewContentModeScaleAspectFit;
//button.imageView.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventAllTouchEvents];
[self.view addSubview:button];
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(void)buttonPressed:(id)sender {
Button *button = [[Button alloc] init];
button = sender;
NSLog(@"%d och spela upp ljud", button.picNumber);
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"double_card" ofType:@"mp3"];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av play];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",button.picNumber]] forState:UIControlStateNormal];
[self.view addSubview:button];
[NSThread sleepForTimeInterval:1.0];
if(isPairActive == FALSE){
isPairActive = TRUE;
firstButton = button;
}
else {
secondButton = button;
[self secondCard];
}
return;
}
- (void)secondCard {
[NSThread sleepForTimeInterval:1.0];
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"double_card" ofType:@"mp3"];
isPairActive = FALSE;
if([appDelegate.dictionary objectForKey:[NSString stringWithFormat:@"%d",secondButton.picNumber]] == [NSString stringWithFormat:@"%d",firstButton.picNumber]){
NSString *newAudioFile2 = [[NSBundle mainBundle] pathForResource:@"right_sound" ofType:@"mp3"];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile2] error:NULL];
[av play];
}
else {
[firstButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[secondButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av play];
}
return;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[av release];
[firstButton release];
[secondButton release];
}
@end
#导入“Button.h”
#进口
#进口
@ZogajAppDelegate类;
@接口对播放播放视图控制器:UIViewController{
ZogajAppDelegate*appDelegate;
AVAudioPlayer*av;
布尔isPairActive;
按钮*第一个按钮;
按钮*第二个按钮;
}
@属性(非原子,保留)AVAudioPlayer*av;
@属性(非原子,保留)按钮*firstButton;
@属性(非原子,保留)按钮*secondButton;
-(无效)按钮按下:(id)发送者;
-(作废)第二张信用卡;
@结束
和.m文件:
#import "Button.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@class ZogajAppDelegate;
@interface PairPlayPlayViewController : UIViewController <AVAudioPlayerDelegate> {
ZogajAppDelegate *appDelegate;
AVAudioPlayer *av;
Boolean isPairActive;
Button *firstButton;
Button *secondButton;
}
@property (nonatomic, retain) AVAudioPlayer *av;
@property (nonatomic, retain) Button *firstButton;
@property (nonatomic, retain) Button *secondButton;
- (void)buttonPressed:(id)sender;
- (void)secondCard;
@end
#import "PairPlayPlayViewController.h"
#import "ZogajAppDelegate.h"
#import "Button.h"
#import "AVFoundation/AVAudioPlayer.h"
@implementation PairPlayPlayViewController
@synthesize av, firstButton, secondButton;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"double_card" ofType:@"mp3"];
NSString *newAudioFile2 = [[NSBundle mainBundle] pathForResource:@"right_sound" ofType:@"mp3"];
av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av prepareToPlay];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile2] error:NULL];
[av prepareToPlay];
//av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
//[av prepareToPlay];
isPairActive = FALSE;
appDelegate = (ZogajAppDelegate *)[[UIApplication sharedApplication] delegate];
int position;
for(int i = 0; i < ((appDelegate.numberOfPairs*2)/5); i++){
for (int j = 0; j<5 ;j++){
Button *button = [[Button alloc] init];
//Sätter bild nummer..
position = (arc4random() % [appDelegate.cardsInPlay count]);
button.picNumber = [[appDelegate.cardsInPlay objectAtIndex:position]intValue];
NSLog(@"%d", button.picNumber);
[appDelegate.cardsInPlay removeObjectAtIndex:position];
button.frame = CGRectMake( 3 + (65 * j), 5 + (65 * i), 55, 55);
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
for (int j = 0; j<((appDelegate.numberOfPairs*2) % 5) ;j++){
Button *button = [[Button alloc] init];
//Sätter bild nummer..
position = (arc4random() % [appDelegate.cardsInPlay count]);
button.picNumber = [[appDelegate.cardsInPlay objectAtIndex:position]intValue];
NSLog(@"%d", button.picNumber);
[appDelegate.cardsInPlay removeObjectAtIndex:position];
button.frame = CGRectMake( 3 + (65 * j), 5 + (65 * ((appDelegate.numberOfPairs*2)/5) + 1), 55, 55);
//button.imageView.contentMode = UIViewContentModeScaleAspectFit;
//button.imageView.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventAllTouchEvents];
[self.view addSubview:button];
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(void)buttonPressed:(id)sender {
Button *button = [[Button alloc] init];
button = sender;
NSLog(@"%d och spela upp ljud", button.picNumber);
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"double_card" ofType:@"mp3"];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av play];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",button.picNumber]] forState:UIControlStateNormal];
[self.view addSubview:button];
[NSThread sleepForTimeInterval:1.0];
if(isPairActive == FALSE){
isPairActive = TRUE;
firstButton = button;
}
else {
secondButton = button;
[self secondCard];
}
return;
}
- (void)secondCard {
[NSThread sleepForTimeInterval:1.0];
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"double_card" ofType:@"mp3"];
isPairActive = FALSE;
if([appDelegate.dictionary objectForKey:[NSString stringWithFormat:@"%d",secondButton.picNumber]] == [NSString stringWithFormat:@"%d",firstButton.picNumber]){
NSString *newAudioFile2 = [[NSBundle mainBundle] pathForResource:@"right_sound" ofType:@"mp3"];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile2] error:NULL];
[av play];
}
else {
[firstButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[secondButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av play];
}
return;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[av release];
[firstButton release];
[secondButton release];
}
@end
#导入“PairPlayViewController.h”
#导入“ZogajAppDelegate.h”
#导入“Button.h”
#导入“AVFoundation/AVAudioPlayer.h”
@PairPlayViewController的实现
@合成av,第一个按钮,第二个按钮;
/*
//指定的初始值设定项。如果以编程方式创建控制器并希望执行不适用于viewDidLoad的自定义,请重写。
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
if((self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])){
//自定义初始化
}
回归自我;
}
*/
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
[超级视图下载];
NSString*newAudioFile=[[NSBundle mainBundle]路径资源:@“mp3”类型的“双U卡”;
NSString*newAudioFile2=[[NSBundle mainBundle]路径资源:@“mp3”类型的“右音”;
av=[[AvaudioLayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile]错误:NULL];
[视听游戏];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile2]错误:NULL];
[视听游戏];
//av=[[AvaudioLayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile]错误:NULL];
//[视听游戏];
isPairActive=FALSE;
appDelegate=(ZogajAppDelegate*)[[UIApplication sharedApplication]委托];
内部位置;
对于(int i=0;i<((appDelegate.numberOfPairs*2)/5);i++){
对于(int j=0;j在按钮中按下:
更改背景,设置一个NSTimer回叫您,然后返回。在NSTimer回叫中,将背景设置回您想要的
绘图不会在您要求的时候发生。这会给您带来糟糕的性能。所有绘图都会合并到运行循环的绘图部分。只有从该iAction方法返回,您才能到达该部分。在按钮中按下:
更改背景,设置一个NSTimer来回叫您,然后返回。在NSTi中mer callback,将背景设置回您想要的
绘图不会在您要求的时候发生。这会给您带来糟糕的性能。所有绘图都会合并到运行循环的绘图部分。只有从该iAction方法返回后,您才能到达该部分。正如您所知,从该方法返回后,图像将被更改。就是这样
您必须从该方法返回,因此您必须更改图像,执行选择器以执行您要执行的其余代码,并从该方法返回
UIKit只能在主线程上运行,因此您必须从方法返回,这将阻止它,然后它将执行GUI操作
编辑:
不要让NSThread休眠,而是延迟执行选择器。正如您已经知道的,从方法返回后,图像将被更改。就是这样
您必须从该方法返回,因此您必须更改图像,执行选择器以执行您要执行的其余代码,并从该方法返回
UIKit只能在主线程上运行,因此您必须从方法返回,这将阻止它,然后它将执行GUI操作
编辑:
不要让NSThread休眠,而是延迟执行选择器。我认为应该使用NSTimer而不是混用NSThread。我认为应该使用NSTimer而不是混用NSThread。