close
X

Quarz2D基礎(二)


- (void)drawRect:(CGRect)rect {    //獲取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    //建立橢圓路徑    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 50, 200, 100)];    /**     *  矩陣操作必須在新增路徑之前     */    //縮放    CGContextScaleCTM(ctx, 0.5, 0.5);    //移動    CGContextTranslateCTM(ctx, 20, 200);    //旋轉    CGContextRotateCTM(ctx, M_PI_2);    //將路徑新增至上下文    CGContextAddPath(ctx, path.CGPath);    //設定顏色    [[UIColor redColor] set];    //渲染上下文    CGContextFillPath(ctx);}

效果圖:

原圖

縮放

位移

旋轉

注意:這幾種效果可疊加.
- (void)drawRect:(CGRect)rect {    //建立富文字字典    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];    //設定富文字字型    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];    //設定富文字背景顏色//    dict[NSBackgroundColorAttributeName] = [UIColor redColor];    //設定繪製線寬    dict[NSStrokeWidthAttributeName] = @10;    //建立shadow對象    NSShadow *shadow = [[NSShadow alloc] init];    //設定shadow模糊度    shadow.shadowBlurRadius = 2;    //設定shadow的顏色    shadow.shadowColor = [UIColor orangeColor];    //設定shadow的偏移量    shadow.shadowOffset = CGSizeMake(5, 5);    //設定富文字陰影效果    dict[NSShadowAttributeName] = shadow;    //建立文字    NSString *str = @"iOS基礎知識";    //繪製文字    [str drawAtPoint:CGPointMake(50, 50) withAttributes:dict];}

效果圖:

繪製文字

- (void)drawRect:(CGRect)rect {    //建立圖片,同時設定圖片內容,此方法建立的image的尺寸與圖片尺寸相同    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Snip20160427_8"]];    //通過此方法繪製圖片時,座標預設在父控制元件的原點位置    [imageV drawRect:CGRectMake(100, 50, imageV.frame.size.width, imageV.frame.size.height)];}- (void)drawRect:(CGRect)rect {//    image.size = CGSizeMake(50, 50);    //繪製圖片,可控制座標位置UIImage *image = [UIImage imageNamed:@"Snip20160427_10"];    [image drawAtPoint:CGPointMake(50, 50)];}

效果圖:

drawRect

drawAtPoint

//定義變數static CGFloat snow = 0;// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    //建立image    UIImage *image = [UIImage imageNamed:@"Snip20160427_10"];    //繪製圖片    [image drawAtPoint:CGPointMake(0, snow)];    //設定image的位置下移    snow += 10;    //重複    if (snow > self.frame.size.height) {        snow = 0;    }}- (void)awakeFromNib{    //建立定時器    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(timeChange)];    //新增主執行迴圈 循環    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; }//定時器呼叫的方法- (void)timeChange{    //重繪    [self setNeedsDisplay];}

效果圖:

雪花效果









分享是一種美德,喜歡就幫我們讚一下支持吧~

為你推薦