iOS UI進階05,iosui進階

來源:互聯網
上載者:User

iOS UI進階05,iosui進階

  • Quartz2D
    • Quartz2D是二維的繪圖引擎
      • 經封裝的函數庫,方便開發人員使用。也就是說蘋果幫我們封裝了一套繪圖的函數庫
      • 用Quartz2D寫的同一份代碼,既可以運行在iphone上又可以運行在mac上,可以跨平台開發。
      • 開發中比較常用的是截屏/裁剪/自訂UI控制項。 Quartz2D在iOS開發中的價值就是自訂UI控制項。
      • 在drawRect:方法中才能擷取到上下文
  • Quartz2D繪圖
    • 自訂view:需要繪圖,就必須重寫drawRect:方法
      • 1 drawRect視圖要顯示的時候,才會調用,viewDidLoad後才會調用,因為那時候還沒顯示視圖。
      • 2 作用:用來繪圖
        • 畫一條線
        • 1 擷取圖形上下文
        • CG:表示這個類在CoreGraphics架構裡 Ref:引用
        • 想擷取圖形上下文,首先敲UIGraphics。
        • 2 拼接路徑:一般開發中用貝塞爾路徑,裡面封裝了很多東西,可以幫我畫一些基本的線段,矩形,圓等等。
        • 建立貝塞爾路徑
        • 起點:moveToPoint
        • 終點:addLineToPoint
        • 3 把路徑添加到上下文
      • CGPath轉換:UIKit架構轉CoreGraphics直接CGPath就能轉
        • 4> 把上下文渲染到視圖,圖形上下文本身不具備顯示功能。
        • 5總結:首先擷取圖形上下文,然後描述路徑,把路徑添加到上下文,渲染到視圖,圖形上下文相當於一個記憶體緩衝區,在記憶體裡面操作是最快的,比直接在介面操作快多了。
        • // 什麼時候調用:當前控制項即將顯示的時候才會調用這個方法繪製   // 作用:繪製內容,以後只要想在一個view中繪製內容,必須在drawRect裡面繪製   - (void)drawRect:(CGRect)rect {    // 繪製曲線    // 1.擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.拼接路徑    UIBezierPath *path = [UIBezierPath bezierPath];    // 設定起點    [path moveToPoint:CGPointMake(10, 125)];    // 描述曲線    [path addQuadCurveToPoint:CGPointMake(240, 125) controlPoint:CGPointMake(125, 240)];    [path addLineToPoint:CGPointMake(10, 125)];    // 3.添加路徑到上下文    CGContextAddPath(ctx, path.CGPath);    // 設定繪圖狀態,一定要再渲染之前    // 設定顏色    [[UIColor redColor] setStroke];    // 設定線段的寬度    CGContextSetLineWidth(ctx, 15);    // 設定線段的頂角樣式    CGContextSetLineCap(ctx, kCGLineCapRound);    // 設定串連樣式    CGContextSetLineJoin(ctx, kCGLineJoinRound);    // 4.渲染上下文    CGContextStrokePath(ctx);}
      • 畫兩跟不串連的線
        • 1 第二次畫的時候,重新設定起點,然後畫線。一個路徑可以包含多條線段。
        • 2 新建立一個路徑,添加到上下文。開發中建議使用這種,比較容易控制每根線。
        • // 繪製兩條路徑的方法- (void)drawTwoLine{    // 1.擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.拼接路徑,一個路徑中可以儲存多條線段    UIBezierPath *path = [UIBezierPath bezierPath];    [path moveToPoint:CGPointMake(10, 10)];    [path addLineToPoint:CGPointMake(20, 20)];    // 3.把路徑添加到上下文    CGContextAddPath(ctx, path.CGPath);    // 一根線對應一個路徑,只要繪製的線不串連,最好使用一根線對應一個路徑的方法    path = [UIBezierPath bezierPath];    // 拼接另一根直線    // 預設下一根線的起點就是上一根線的終點    // 設定第二根線的起點    //    [path moveToPoint:CGPointMake(20, 20)];    // 如果想要繪製不串連的線,重新設定起點    [path moveToPoint:CGPointMake(50, 50)];    [path addLineToPoint:CGPointMake(20, 200)];    // 3.把路徑添加到上下文    CGContextAddPath(ctx, path.CGPath);    // 4.渲染上下文    CGContextStrokePath(ctx);}
      • 圓弧
        • 分析:
          • 1> 圓弧屬於圓的一部分,因此先要有圓,才有弧。
          • 2> 圓需要起點嗎?畫線需要,圓也不另外。 -3> 起點在哪? 圓心右邊
          • 4> 畫圓弧還需要起始角度,結束角度,方向,角度必須是弧度
          • // 畫圓弧    // Center圓心    // radius:半徑    // startAngle起始角度    // endAngle:結束角度    // clockwise:Yes 順時針 No逆時針    CGPoint center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:NO];    [path1 stroke];
      • 畫扇形
        • // 畫扇形    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];    [path addLineToPoint:center];    [path addLineToPoint:CGPointMake(self.bounds.size.height * 0.5 + 100, self.bounds.size.height * 0.5)];    // 關閉路徑:從路徑的終點串連到起點    [path closePath];    // 設定填充顏色    [[UIColor redColor] setFill];    // 設定描邊顏色    [[UIColor greenColor] setStroke];    //    [path stroke];    // 如果路徑不是封閉的,預設會關閉路徑    [path fill];
      • 畫餅圖
        • #import "PieView.h"@implementation PieView// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{   //重繪    [self setNeedsDisplay];}- (void)drawRect:(CGRect)rect {    // Drawing code    NSArray *data = @[@25,@25,@20,@30];    //圓心    CGPoint center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);    //角度    CGFloat radius = self.bounds.size.width * 0.5;    CGFloat startA = 0;    CGFloat endA = 0;    CGFloat angle = 0;    for (NSNumber *num in data) {        // 畫一個扇形        startA = endA;        angle = [num intValue] / 100.0 * M_PI * 2;        endA = startA + angle;       UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];        [path addLineToPoint:center];        // set:同時設定描邊和填充顏色        [[self randomColor] set];        [path fill];    }}// 隨機顏色- (UIColor *)randomColor{    CGFloat r = arc4random_uniform(256) / 255.0;    CGFloat g = arc4random_uniform(256) / 255.0;    CGFloat b = arc4random_uniform(256) / 255.0;    return [UIColor colorWithRed:r green:g blue:b alpha:1];}@end

      • 畫字
        • - (void)drawRect:(CGRect)rect {    NSString *str = @"hello!";    // Attributes:屬性    // 給一個字串添加屬性,可以叫富文本,顏色,字型大小,空心,陰影    // 利用這個屬性字典給文本添加屬性    NSMutableDictionary *strAttr = [NSMutableDictionary dictionary];    // key,value    // 如何找到設定文本的屬性key    // 描述了字型    strAttr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:50];    // 設定描邊的顏色和寬度    strAttr[NSStrokeWidthAttributeName] = @1;    strAttr[NSStrokeColorAttributeName] = [UIColor redColor];    NSShadow *shadow = [[NSShadow alloc] init];    shadow.shadowColor = [UIColor yellowColor];    shadow.shadowOffset = CGSizeMake(10, 10);    shadow.shadowBlurRadius = 5;    // 陰影    strAttr[NSShadowAttributeName] = shadow;    // 文字顏色    strAttr[NSForegroundColorAttributeName] = [UIColor redColor];    [str drawAtPoint:CGPointZero withAttributes:strAttr];}

           

    • 定時器實現下雪
        • #import "SnowView.h"@implementation SnowView- (void)awakeFromNib{    // 設定定時器//    [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];    // 0.1 setNeedsDisplay 綁定一個標識,等待下次重新整理的時候才會調用drawRect方法    // 0.15 螢幕的重新整理時間    // 定時器    // 每次螢幕重新整理的時候就會調用,螢幕一秒重新整理60次    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];    // 只要把定時器添加到主運行迴圈就能自動執行    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];    // setNeedsDisplay:底層並不會馬上調用drawRect,只會給當前的控制項綁定一個重新整理的標識,每次螢幕重新整理的時候,就會把綁定了重新整理(重繪)標識的控制項重新重新整理(繪製)一次,就會調用drawRect去重繪    // 如果以後每隔一段時間需要重繪,一般不使用NSTimer,使用CADisplayLink,不會重新整理的時候有延遲}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code    static CGFloat snowY = 0;    UIImage *image = [UIImage imageNamed:@"雪花"];    [image drawAtPoint:CGPointMake(0, snowY)];    snowY += 10;    if (snowY > rect.size.height) {        snowY = 0;    }}

           

    • 圖形上下文狀態棧
        • #import "DrawView.h"@implementation DrawView// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code    // 1.擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.拼接路徑    UIBezierPath *path = [UIBezierPath bezierPath];    [path moveToPoint:CGPointMake(10, 125)];    [path addLineToPoint:CGPointMake(240, 125)];    // 3.把路徑添加到上下文    CGContextAddPath(ctx, path.CGPath);    // 儲存當前內容相關的預設狀態    CGContextSaveGState(ctx);    // 設定狀態    [[UIColor redColor] set];    CGContextSetLineWidth(ctx, 20);    // 渲染上下文    CGContextStrokePath(ctx);    // 建立第二根路徑    path = [UIBezierPath bezierPath];    [path moveToPoint:CGPointMake(125, 10)];    [path addLineToPoint:CGPointMake(125, 240)];    // 添加到上下文    CGContextAddPath(ctx, path.CGPath);    // 恢複下上下文狀態    // 取出之前的儲存的狀態覆蓋掉當前的狀態    CGContextRestoreGState(ctx);//    [[UIColor blackColor] set];//    CGContextSetLineWidth(ctx, 1);    // 4.渲染上下文到view的layer    // 在渲染之前,系統會查看下內容相關的狀態,根據狀態去渲染    CGContextStrokePath(ctx);}@end

      • 圖片截屏
        • #import "ViewController.h"@interface ViewController ()@property (nonatomic, weak) UIView *cover;@property (nonatomic, assign) CGPoint oriP;@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (weak, nonatomic) IBOutlet UIView *view1;@end@implementation ViewController- (UIView *)cover{    if (_cover == nil) {        UIView *view = [[UIView alloc] init];        view.backgroundColor = [UIColor blackColor];        view.alpha = 0.5;        _cover = view;        [self.view addSubview:view];    }    return _cover;}- (IBAction)pan:(UIPanGestureRecognizer *)sender {    // 擷取下當前的觸摸    CGPoint curP = [sender locationInView:_imageView];    if (sender.state == UIGestureRecognizerStateBegan) {        // 記錄下一開始的位置        _oriP = curP;    }    // 計算下黑色蒙版的frame    CGFloat w = curP.x - _oriP.x;    CGFloat h = curP.y - _oriP.y;    self.cover.frame = CGRectMake(_oriP.x, _oriP.y, w, h);    if (sender.state == UIGestureRecognizerStateEnded) { // 手指抬起        // 裁剪圖片,產生一張新圖片        // 開啟位元影像上下文        UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0);        // 設定裁剪地區        UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.cover.frame];        [path addClip];        // 繪製圖片        [_imageView.layer renderInContext:UIGraphicsGetCurrentContext()];        // 產生圖片        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();        // 關閉上下文        UIGraphicsEndImageContext();        _imageView.image = image;        [self.cover removeFromSuperview];    }}@end:截屏前
          :截屏後
           
      • 圖片擦除
        • #import "ViewController.h"@interface ViewController ()@end@implementation ViewController// 只要使用者手指在圖片上拖動.就會調用- (IBAction)pan:(UIPanGestureRecognizer *)sender {    // 拖動的時候,擦除圖片的某一部分    // 擷取手指的觸摸點   CGPoint curP = [sender locationInView:sender.view];    // 計算擦除的frame    CGFloat wh = 30;    CGFloat x = curP.x - wh * 0.5;    CGFloat y = curP.y - wh * 0.5;    CGRect frame = CGRectMake(x, y, wh, wh);    // 開啟位元影像上下文    UIGraphicsBeginImageContextWithOptions(sender.view.bounds.size, NO, 0);    // 擷取當前的上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 把控制項上的內容渲染到上下文    [sender.view.layer renderInContext:ctx];    // 清除上下文中某一部分的內容    CGContextClearRect(ctx, frame);    // 產生一張新的圖片    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    // 關閉上下文    UIGraphicsEndImageContext();    // 重新顯示到UIImageView    UIImageView *imageV = (UIImageView *)sender.view;    imageV.image = image;}@end:擦除前::擦除部分後

           

相關文章

聯繫我們

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