iOS開發UI篇—Quartz2D簡單使用(三)

來源:互聯網
上載者:User

標籤:

一、通過slider控制圓的縮放

1.實現過程

建立一個項目,建立一個繼承自UIview的類,並和storyboard中自訂的view進行關聯。

介面搭建,

程式碼範例:

  YYViewController.m檔案

 1 // 2 //  YYViewController.m 3 //  04-對圓進行縮放 4 // 5 //  Created by apple on 14-6-11. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "YYView.h"11 12 @interface YYViewController ()13 @property (weak, nonatomic) IBOutlet YYView *circleView;14 - (IBAction)valueChange:(UISlider *)sender;15 16 @end17 18 @implementation YYViewController19 20 - (void)viewDidLoad21 {22     [super viewDidLoad];23     // Do any additional setup after loading the view, typically from a nib.24 }25 26 27 - (IBAction)valueChange:(UISlider *)sender {28     // 當值改變的時候,把值傳遞給view,改變圓的半徑29     NSLog(@"%f",sender.value);30     // 把sender的值傳遞給自訂view,設定圓的半徑31     self.circleView.radius = sender.value;32 }33 @end

YYView.h檔案

////  YYView.h//  04-對圓進行縮放////  Created by apple on 14-6-11.//  Copyright (c) 2014年 itcase. All rights reserved.//#import <UIKit/UIKit.h>@interface YYView : UIView// 提供一個屬性來接收外界傳入的半徑@property(nonatomic,assign)float radius;@end

YYView.m檔案

 1 // 2 //  YYView.m 3 //  04-對圓進行縮放 4 // 5 //  Created by apple on 14-6-11. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYView.h"10 11 @implementation YYView12 // 自訂view中的圓不顯示13 // 重寫set方法,為半徑賦值14 -(void)setRadius:(float)radius15 {16     _radius = radius;17     // 通知自訂的view重新繪製圖形18     [self setNeedsDisplay];19 }20 21 // 如果view是從xib或storyboard中建立出來的會先調用awakefromnib方法22 - (void)awakeFromNib23 {24     // 在這裡為圓的半徑設定一個初始的值25     self.radius = 20;26 }27 28 - (void)drawRect:(CGRect)rect29 {30     // 1.擷取圖形上下文31     CGContextRef ctx = UIGraphicsGetCurrentContext();32     // 2.繪圖33     // 在自訂的view中畫一個圓34     CGContextAddArc(ctx, 100, 100, self.radius, 0, 2*M_PI, 0);35     // 設定圓的填充顏色36     [[UIColor grayColor] set];37     38     // 3.渲染39 //    CGContextStrokePath(ctx);40     CGContextFillPath(ctx);41 }42 43 44 @end

  效果:

       

2.注意點:

drawRect:方法不能由我們自己手動調用,只能由系統來調用。drawRect:調用的時機:當第一次顯示或者一個重繪事件發生時調用。setNeedsDisplay方法:重新繪製,調用這個方法就會通知自訂的view重新繪製畫面,調用drawRect:。提示:當一個view從xib或storyboard建立出來時,會調用awakefromnib方法。3.補充可以通過slider的value屬性監聽值的改變,當然也可以指定value的取值範圍(這裡設定為0~100)。二、刷幀效果說明:把雪花狀的圖片繪製到view上,實現圖片在視圖中下落的效果。  1.實現代碼:
 1 // 2 //  YYView.m 3 //  05-刷幀動畫 4 // 5 //  Created by apple on 14-6-11. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYView.h"10 11 // 私人擴充12 @interface YYView  ()13 @property(nonatomic, assign) float imageY;14 15 @end16 @implementation YYView17 18 19 -(id)initWithCoder:(NSCoder *)aDecoder20 {21     // 請注意這裡一定要先初始化父類的構造方法22     if (self = [super initWithCoder:aDecoder]) {23         NSLog(@"initWithCoder:");24         25         // NSTimer一般用於定時的更新一些非介面上的資料,告訴多久調用一次26         // 使用定時器,使用該定時器會出現卡頓的現象27 //        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateImage) userInfo:nil repeats:YES];28         29         //  CADisplayLink刷幀,預設每秒重新整理60次30         // 該定時器建立之後,預設是不會執行的,需要把它載入到訊息迴圈中31        CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImage)];32         [display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];33      34     }35     return self;36 }37 38 -(void)updateImage39 {40     // 調用該方法重繪畫面41     [self setNeedsDisplay];42 }43 -(void)awakeFromNib44 {45     NSLog(@"awakeFromNib");46 }47 48 - (void)drawRect:(CGRect)rect49 {50     // 把圖片繪製到view上51 52     // 每次調用該方法對畫面進行重繪時,imageY的值就+553     self.imageY += 5;54       // 判斷,當雪花超出螢幕的時候,讓圖片從頭開始降落55     if (self.imageY > rect.size.height) {56         self.imageY = 0;57     }58     UIImage *image = [UIImage imageNamed:@"snow"];59     [image drawAtPoint:CGPointMake(0, self.imageY)];60 61     UIImage *image2 = [UIImage imageNamed:@"me"];62     [image2 drawAtPoint:CGPointMake(80, self.imageY)];63     64 }65 66 @end

  實現效果

         

2.重要說明

(1)下面兩個方法的調用順序

-(void)awakeFromNib

-(id)initWithCoder:(NSCoder *)aDecoder

提示:如果view是從xib或storyboard中建立可以調用awakefromnib方法,歸檔。從檔案建立view,其實會先調用initwithcoder這個方法。xib和storyboard也是檔案。

上面兩個方法,-(id)initWithCoder:(NSCoder *)aDecoder會先調用。實現該方法需要實現NSCoding協議,由於建立的UIView預設就已經實現了該協議。

可以進入到標頭檔查看:

運行建立的程式,通過列印可以驗證上面兩個方法的調用順序。

(2)兩個定時器

第一個:

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateImage) userInfo:nil repeats:YES];

說明: NSTimer一般用於定時的更新一些非介面上的資料,告訴多久調用一次

第二個:

        CADisplayLink *display= [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImage)];

        [display addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSDefaultRunLoopMode];

  說明: CADisplayLink刷幀,預設每秒重新整理60次。該定時器建立之後,預設是不會執行的,需要把它載入到訊息迴圈中

 

iOS開發UI篇—Quartz2D簡單使用(三)

聯繫我們

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