iOS 畫氣泡(bubble)

來源:互聯網
上載者:User

標籤:

本文是關於iOS代碼畫氣泡,手把手教你畫一個氣泡,其他形狀請讀者自行嘗試,希望大家玩的開心!

////  SpeechBubbleView.m//  demo////  Created by mygame on 15/3/4.//  Copyright (c) 2015年 mygame. All rights reserved.//#import "SpeechBubbleView.h"#import <CoreGraphics/CoreGraphics.h>#define kPopupTriangleHeigh 12#define kPopupTriangleWidth 22#define kBorderOffset       0//0.5f@implementation SpeechBubbleView- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [UIColor purpleColor];    }    return self;}- (void)drawRect:(CGRect)rect {    [super drawRect:rect];        CGFloat viewW = rect.size.width;    CGFloat viewH = rect.size.height;        CGFloat strokeWidth = 1;    CGFloat borderRadius = 10;    CGFloat offset = strokeWidth + kBorderOffset;        CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetLineJoin(context, kCGLineJoinRound); //     CGContextSetLineWidth(context, strokeWidth); // 設定畫筆寬度    CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor); // 設定畫筆顏色    CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor); // 設定填充顏色        // 畫三角形    /*     ---\/     */
  /*
    畫紅色部分
  */    CGContextBeginPath(context);    CGContextMoveToPoint(context, borderRadius+offset, viewH-kPopupTriangleHeigh-offset);    CGContextAddLineToPoint(context, round((viewW-kPopupTriangleWidth)/ 2.0f) + offset, viewH-kPopupTriangleHeigh-offset);    CGContextAddLineToPoint(context, round(viewW/2.0f), viewH-offset);    CGContextAddLineToPoint(context, round((viewW+kPopupTriangleWidth)/2.0f)+offset, viewH-kPopupTriangleHeigh-offset);      // 畫其餘部分
  /*
 
  

  */ CGContextAddArcToPoint(context, viewW-offset, viewH-kPopupTriangleHeigh-offset, viewW-offset, kPopupTriangleHeigh+offset, borderRadius-strokeWidth); CGContextAddArcToPoint(context, viewW-offset, offset, viewW-borderRadius-offset, offset, borderRadius-strokeWidth); CGContextAddArcToPoint(context, offset, offset, offset, borderRadius+offset, borderRadius-strokeWidth); CGContextAddArcToPoint(context, offset, viewH-kPopupTriangleHeigh-offset, borderRadius+offset, viewH-kPopupTriangleHeigh-offset, borderRadius-strokeWidth); CGContextClosePath(context); CGContextDrawPath(context, kCGPathFillStroke);}@end

關於CGContextAddArcToPoint介面解釋請點擊這裡

 

iOS 畫氣泡(bubble)

聯繫我們

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