iOS -雪花動畫

來源:互聯網
上載者:User

標籤:

隨機數方法

 

arc4random()  //[0,2^32-1]; 

 

隨機數聲明

unsigned int randomNum=arc4random()% 320;

int i = randomNum ;

其中,unsigned 沒有標記的 指的是只有正數,說明randomNum是正數

其中 arc4random() 的括弧絕對不可以忘掉

 

隨機數也可以直接用

snowView.frame = CGRectMake(arc4random() % 320 , 480-snowView.frame.size.height,snowView.frame.size.width, snowView.frame.size.height);

 

關於隨機數的意外?

即使是在證明的時候不用unsigned,在使用時仍然是只有正數出現

但是如果在後面的使用過程中對randomNum進行求餘運算,最後就會出現負值

這真是一個奇怪的現象

比如說

    for (int a=0; a<60; a++) {

        int randomNum=arc4random();

        int i=randomNum%320;

        NSLog(@"--------->>arc4random()為%d",arc4random()%320);

        

        NSLog(@"--------->>randomNum為%d",i);

    }

在寫了這樣的代碼之後,輸出的arc4random()為後面一直都是正數,而randomNum為後面就有負數出現

這到底是為什麼呢?

 

答案:    

   for (int a=0; a<60; a++) 

   {

        int randomNum=arc4random();

        int i=randomNum%320;

        NSLog(@"--------->>arc4random()為%d",arc4random()%320);

        

        NSLog(@"--------->>randomNum為%d",i);

    }

在這段代碼中,這裡的int是有符號的32位,而arc4random()是無符號的32位,比int大很多,賦給int類型的變數時,超過int範圍的隨機數,第一個  1   會被當成符號(即負號)來看待,所以會出現負數

對範珍老師的崇拜如滔滔江水源源不絕,這老師太牛了,簡直是無所不能啊

 

 

通過動畫塊傳參數

            [UIView beginAnimations:nil context:(__bridge void*)snowView];

            [UIView setAnimationDuration:6];

            [UIView setAnimationDelegate:self];

            snowView.center=CGPointMake(arc4random()%320, 475);

            [UIView commitAnimations];

這裡,context後面的參數是 void* 類型,貌似是c裡面的?,所以需要橋接強制轉換,bridge關鍵字可以使不同語言之間的變數相互轉換

這裡,動畫塊中使用了Delegate之後,動畫結束之後就會自動調用DidStop方法

 

下面是系統提供的DidStop方法

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

{

    //snow melt

    UIImageView *snowView=(__bridge UIImageView*)context;

    [UIView beginAnimations:nil context:(__bridge void*)snowView];

    [UIView setAnimationDuration:1];

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDidStopSelector:@selector(reAnimationDidStop:finished:context:)];

    snowView.alpha=0.1;

    [UIView commitAnimations];

    

}

 

這裡,參數有三個,暫時不用寫,而context後面的context代表的就是snowView,但是context是c語言中的,所以需要再次轉換一下

 

區分兩個動畫塊

因為只要動畫塊使用delegate,結束之後就會自動去調用DidStop方法,所以,如果有兩個動畫塊,而只有一個DidStop方法,那麼兩個動畫塊結束之後都會去調用哪個方法,為了使不同的動畫塊結束之後進行不同的後續操作,需要給動畫塊命名,然後再DidStop語句中用if判斷動畫塊的名字,然後執行相應操作

 

下面,給動畫塊命名的代碼

[UIView beginAnimations:@"snowDown" context:(__bridge void *)(snowView)];

 

判斷動畫塊名字的代碼

 if ([animationID isEqualToString:@"snowDown"])

這裡的animationID指的就是來調用這個DidStop方法的動畫塊的名字

iOS -雪花動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.