OC實現簡單的關燈遊戲,oc實現關燈
//將燈的圖片在視圖上布局,5行5列 for (int i = 0; i < 5; i++) { for (int j = 0 ; j < 5; j++) { UIButton *lightButton = [UIButton buttonWithType:UIButtonTypeSystem]; lightButton.frame = CGRectMake(i * 64, 120 + (j *64), 64, 64); [lightButton setBackgroundImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal]; [lightButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:lightButton]; lightButton.tag = 100 + j + 10 * i;//左右button的tag相差1,上下button的tag相差10 } }
//button的點擊事件- (void)click:(UIButton *)button{ //定義數組儲存所點擊的button和他的上下左右的button int array[5] = {button.tag , button.tag + 1, button.tag + 10, button.tag - 1, button.tag - 10}; //判斷當前buton的背景圖片是不是亮燈(2.png是亮等圖片名) if ([button.currentBackgroundImage isEqual:[UIImage imageNamed:@"2.png"]]) { //遍曆數組 for (int i = 0; i < 5; i++) { UIButton *aButton = (UIButton*)[self.window viewWithTag:array[i]]; //判斷所點擊button的前後左右button的狀態,如果是滅著的燈就將其背景圖片變為亮著的燈(1.png是滅著的燈的圖片) if ([aButton.currentBackgroundImage isEqual:[UIImage imageNamed:@"1.png"]]) { [ aButton setBackgroundImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal]; } else { [aButton setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal]; } } //else判斷當前buton的背景圖片是不是滅燈 } else { for (int i = 0; i < 5; i++) { UIButton *aButton = (UIButton*)[self.window viewWithTag:array[i]]; if ([aButton.currentBackgroundImage isEqual:[UIImage imageNamed:@"2.png"]]) { [ aButton setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal]; } else [aButton setBackgroundImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal]; } }}
怎理解oc的單例模式 最好簡單點 可以舉兩個例子 用了回怎 不用回怎 謝了,本人初學
意圖:使得類的一個對象成為系統的唯一執行個體。
類只能有一個執行個體,而且必須從一個為人熟知的訪問點對其進行訪問,比如Factory 方法。
這個唯一的執行個體只能通過子類化進行擴充,而且擴充的對象不會破壞用戶端代碼。
@interface Singletone:NSObject
{
}
+ (Singleton *) shareInstance;
@end
@implementation Singleton
static Singleton *shareSingleton_ = nil;
+ (SIngleton *) sharedInstance
{
if (sharedSingleton_ = nil)
{
sharedSingleton_= [[Singleton alloc] init];
}
return shareSingleton_;
}
@end
聽說現在IPHONE軟體開發很火,我會簡單的OC,可以學?
找本書自學唄,我會的就是c,java, 兩星期搞定OC, 費時的是iphone SDK。 總之給蘋果開發會有一定難處,先想好吧,不然浪費時間。