iOS--為視圖添加陰影

來源:互聯網
上載者:User

標籤:ios   陰影   

iOS–為視圖添加陰影情況一:視圖添加圓角,在添加陰影
   //陰影視圖    self.viewShadow = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];    self.viewShadow.backgroundColor = [UIColor redColor];    self.viewShadow.layer.shadowOpacity = 1;    self.viewShadow.layer.cornerRadius = 5;    self.viewShadow.layer.borderWidth = 1;    self.viewShadow.layer.masksToBounds = YES;    //陰影視圖背景    self.viewShadowBg = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    self.viewShadowBg.layer.shadowOpacity = 1;    self.viewShadowBg.layer.shadowOffset = CGSizeZero;    self.viewShadowBg.layer.shadowColor = [UIColor greenColor].CGColor;    self.viewShadowBg.layer.shadowRadius = 10;    self.viewShadowBg.layer.borderWidth = 1;    self.viewShadowBg.layer.borderColor = [UIColor grayColor].CGColor;    self.viewShadowBg.layer.cornerRadius = 5;
情況二:自訂視圖陰影製作效果

自訂視圖陰影,我們用到的是這個屬性:view.layer.shadowPath,我們通過這個屬性來自訂視圖陰影。

貝茲路徑

這裡我們還用到了貝茲路徑,通過數學方式來描述圖形效果,常見於電腦中映像的描述。objective-c中通過UIBezierPath來描述貝茲路徑。貝茲路徑知識點補充:
Bezier曲線簡介
貝茲路徑-維基百科

實現正常矩形效果:
    UIView *view = [[UIView alloc] init];    view.frame = CGRectMake(100, 100, 100, 100);    view.center = self.view.center;    view.backgroundColor = [UIColor clearColor];    view.layer.shadowColor = [UIColor blackColor].CGColor;    view.layer.shadowOpacity = 0.7f;    view.layer.shadowOffset = CGSizeMake(30.0f, 10.0f);    view.layer.shadowRadius = 2.0f;    view.layer.masksToBounds = NO;    //正常矩形    UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];    view.layer.shadowPath = path.CGPath;    [self.view addSubview:view];
實現自訂畫梯形:
    UIView *view = [[UIView alloc] init];    view.frame = CGRectMake(100, 100, 100, 100);    view.center = self.view.center;    view.backgroundColor = [UIColor clearColor];    view.layer.shadowColor = [UIColor blackColor].CGColor;    view.layer.shadowOpacity = 0.7f;    view.layer.shadowOffset = CGSizeMake(30.0f, 10.0f);    view.layer.shadowRadius = 2.0f;    view.layer.masksToBounds = NO;    //自訂畫梯形    CGSize size = view.bounds.size;    UIBezierPath *path = [UIBezierPath bezierPath];    [path moveToPoint:CGPointMake(size.width * 0.33f, size.height * 0.66f)];    [path addLineToPoint:CGPointMake(size.width * 0.66f, size.height * 0.66f)];    [path addLineToPoint:CGPointMake(size.width * 1.15f, size.height * 1.15f)];    [path addLineToPoint:CGPointMake(size.width * -0.15f, size.height * 1.15f)];    view.layer.shadowPath = path.CGPath;    [self.view addSubview:view];
實現自訂大小橢圓形:
    UIView *view = [[UIView alloc] init];    view.frame = CGRectMake(100, 100, 100, 100);    view.center = self.view.center;    view.backgroundColor = [UIColor clearColor];    view.layer.shadowColor = [UIColor blackColor].CGColor;    view.layer.shadowOpacity = 0.7f;    view.layer.shadowOffset = CGSizeMake(30.0f, 10.0f);    view.layer.shadowRadius = 2.0f;    view.layer.masksToBounds = NO;    //自訂大小橢圓形    CGRect ovalRect = CGRectMake(0, 0, 100, 20);    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect];    view.layer.shadowPath = path.CGPath;    [self.view addSubview:view];

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

iOS--為視圖添加陰影

聯繫我們

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