iOS中的時鐘動畫

來源:互聯網
上載者:User

標籤:

  iOS 動畫效果非常多,我們在開發中可能會遇到很多動畫特效,我們就會用到核心動畫架構CoreAnimation,核心動畫裡面的動畫效果有很多,都是在QuartzCore.framework架構裡面,今天我們看看其只一個CADisplayLink使用,並且完成一個雪花效果:

  如下:

1、引入架構

  2、引入標頭檔

  CADisplayLink最主要的特徵是能提供一個周期性的調用我們賦給它的selector的機制,從這點上看它很像定時器NSTimer。

  CADisplayLink是一個能讓我們以和螢幕重新整理率同步的頻率將特定的內容畫到螢幕上的定時器類。 CADisplayLink以特定模式註冊到runloop後, 每當螢幕顯示內容重新整理結束的時候,runloop就會向 CADisplayLink指定的target發送一次指定的selector訊息, CADisplayLink類對應的selector就會被調用一次。 

  NSTimer以指定的模式註冊到runloop後,每當設定的周期時間到達後,runloop會向指定的target發送一次指定的selector訊息。

3 聲明成員變數

@interface ViewController ()/*  定義對象**/@property(nonatomic,strong) CADisplayLink * link;/*  定義整數索引用來控制執行的次數**/@property(nonatomic,assign) NSInteger index;/*  設定要顯示的圖片**/@property(nonatomic,strong) UIImage * image;@end

  4、對成員變數進行初始化

- (void)viewDidLoad {    [super viewDidLoad];        //初始化圖片    self.image=[UIImage imageNamed:@"snow.png"];    self.view.backgroundColor=[UIColor blackColor];    //初始化時鐘動畫    self.link=[CADisplayLink displayLinkWithTarget:self selector:@selector(show)];        //加入到主迴圈    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];   }

  5、動畫執行產生雪花

#pragma mark - 顯示雪花- (void)show{     //時鐘大約每秒執行60次數,我們需要控制每秒顯示10片雪花    if (self.index%6==0) {                //隨機雪花的大小        NSInteger r= arc4random_uniform(20)+10;                UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(arc4random_uniform(375), -r, r, r)];        imageView.image=self.image;        [self.view addSubview:imageView];        //動畫改變雪花的位置        [UIView animateWithDuration:arc4random_uniform(3)+7 animations:^{            imageView.frame=CGRectMake(arc4random_uniform(375), 667+arc4random_uniform(170), r, r);            //改變雪花的透明度            imageView.alpha=0.3;            //讓雪花旋轉            imageView.transform=CGAffineTransformMakeRotation(M_PI);        } completion:^(BOOL finished) {            //結束後一定要移除雪花            [imageView removeFromSuperview];        }];        }        self.index++;}

 

傑瑞教育
出處:http://www.cnblogs.com/jerehedu/ 
著作權聲明:本文著作權歸煙台傑瑞教育科技有限公司和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。
技術諮詢: 

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.