設定指定邊界圓角,指定邊界圓角

來源:互聯網
上載者:User

設定指定邊界圓角,指定邊界圓角

最近項目中需要用到一些視圖的某些邊界設定為圓角,比如指定一個長方形view,需要設定其左邊為圓形,所以就封裝一個類來實現指定邊界的圓角

1,首先建立一個繼承於UIView的分類(由於項目中我需設定一個button的圓角, 所以命名為Btn)

 

2.在介面檔案中設定介面

這裡參數:distance 為需要設定圓角的那個邊界的高度或寬度

#import <UIKit/UIKit.h>typedef enum {    ZSSideRoundLeft,    ZSSideRoundRight,    ZSSideRoundUp,    ZSSideRoundDown} ZSSideRound;@interface UIView (ZSSideRoundBtn)- (void)roundSide:(ZSSideRound)side distance:(CGFloat)distance;@end

 

 

3.在.m中實現 (在這裡指定邊界設定為半圓)

#import "UIView+ZSSideRoundBtn.h"@implementation UIView (ZSSideRoundBtn)- (void)roundSide:(ZSSideRound)side distance:(CGFloat)distance{    UIBezierPath* maskPath;    if (side == ZSSideRoundLeft)    {        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                                         byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft)                                               cornerRadii:CGSizeMake(distance/2, distance/2)];    }    else if (side == ZSSideRoundRight)    {        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                                         byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)                                               cornerRadii:CGSizeMake(distance/2, distance/2)];    }    else if (side == ZSSideRoundUp)    {        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                                         byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)                                               cornerRadii:CGSizeMake(distance/2, distance/2)];    }    else    {        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                                         byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)                                               cornerRadii:CGSizeMake(distance/2, distance/2)];    }    // 建立形狀圖層,設定它的路徑    CAShapeLayer* maskLayer = [CAShapeLayer layer];    maskLayer.frame = self.bounds;    maskLayer.path = maskPath.CGPath;    // 新建立的形狀圖層設定為映像視圖層的面具    self.layer.mask = maskLayer;    [self.layer setMasksToBounds:YES];}@end

 

 

搞定,以後需要設定一些UIView的子類別檢視的邊界為半圓時,直接引用就OK

 

相關文章

聯繫我們

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