iOS:關於UIView切角的兩種實現方式

來源:互聯網
上載者:User

標籤:

轉載自:http://www.jianshu.com/p/451b7fa94e2a

 

第一種: 我想你一見到代碼,就瞬間有吐的衝動,最常用的一種方式。。。

UIButton *button = [[UIButton alloc]init];button.frame = CGRectMake(100, 100, 100, 40);button.backgroundColor = [UIColor redColor];button.layer.cornerRadius = 20.0f;button.layer.masksToBounds = YES;
[button setTitle:@"測試" forState:UIControlStateNormal];[self.view addSubview:button];

如此簡單...但是,它預設強制裁掉了四個角啊。。。那問題來了,假如需求只要求切一個角呢。。。看第二種方法

第二種: 還是在layer上做文章,不同採用的是類擴充的方法,接下來以UIButton為例,具體效果看:

#import "UIButton+Corner.h"@implementation UIButton (Corner)- (void)corner{    CGRect bounds = self.bounds;    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(20, 20)];    CAShapeLayer *maskLayer = [CAShapeLayer layer];    maskLayer.frame = bounds;    maskLayer.path = maskPath.CGPath;    [self.layer addSublayer:maskLayer];    self.layer.mask = maskLayer;}@end

從上不難看出代碼關鍵所在...這裡只做簡單的擴充...具體根據項目需求來
再次列出官方裁邊的可選項

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {    UIRectCornerTopLeft     = 1 << 0,    UIRectCornerTopRight    = 1 << 1,    UIRectCornerBottomLeft  = 1 << 2,    UIRectCornerBottomRight = 1 << 3,    UIRectCornerAllCorners  = ~0UL};

 

 

iOS:關於UIView切角的兩種實現方式

聯繫我們

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