標籤: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--為視圖添加陰影