OC實現簡單的關燈遊戲,oc實現關燈

來源:互聯網
上載者:User

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。 總之給蘋果開發會有一定難處,先想好吧,不然浪費時間。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.