阴影效果片段代码

Mark!!!

给一个对象简单设置阴影效果:

    Lable.shadowColor = color;
    Lable.shadowOffset = CGSizeMake(0, -1.0);

    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 130, 130)];
    [testView setBackgroundColor:[UIColor grayColor]];
    [[testView layer] setShadowOffset:CGSizeMake(15, 15)];  //设置阴影位置相对于Object位置的偏移值
    [[testView layer] setShadowRadius:0];
    [[testView layer] setShadowOpacity:0.6];
    [[testView layer] setShadowColor:[UIColor grayColor].CGColor];
    [self.view addSubview:testView];
    [testView release];

根据path设置阴影效果:

#import <QuartzCore/QuartzCore.h>

@implementation UIView (NKShadow)


// add the shadow effect to the view
-(void)addShadow{
    
    self.layer.shadowOpacity = 0.4;
    self.layer.shadowRadius = 0.9;
    self.layer.shadowOffset = CGSizeMake(0, 0);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    CGPoint p1 = CGPointMake(self.frame.origin.x, self.frame.origin.y+self.frame.size.height);
    CGPoint p2 = CGPointMake(self.frame.origin.x+self.frame.size.width, p1.y);
    CGPoint c1 = CGPointMake((p1.x+p2.x)/4 , p1.y+6.0);
    CGPoint c2 = CGPointMake(c1.x*3, c1.y);        
    
    [path moveToPoint:p1];
    [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
    
    self.layer.shadowPath = path.CGPath;
}

-(void)addGrayGradientShadow{
    // 0.8 is a good feeling shadowOpacity
    self.layer.shadowOpacity = 0.4;
    
    // The Width and the Height of the shadow rect
    CGFloat rectWidth = 10.0;
    CGFloat rectHeight = self.frame.size.height;
    
    // Creat the path of the shadow
    CGMutablePathRef shadowPath = CGPathCreateMutable();
    // Move to the (0, 0) point
    CGPathMoveToPoint(shadowPath, NULL, 0.0, 0.0);
    // Add the Left and right rect
    CGPathAddRect(shadowPath, NULL, CGRectMake(0.0-rectWidth, 0.0, rectWidth, rectHeight));
    CGPathAddRect(shadowPath, NULL, CGRectMake(self.frame.size.width, 0.0, rectWidth, rectHeight));
    
    self.layer.shadowPath = shadowPath;
    CGPathRelease(shadowPath);
    // Since the default color of the shadow is black, we do not need to set it now
    //self.layer.shadowColor = [UIColor blackColor].CGColor;
    
    self.layer.shadowOffset = CGSizeMake(0, 0);
    // This is very important, the shadowRadius decides the feel of the shadow
    self.layer.shadowRadius = 10.0;
}


@end

UIView以外的区域设置阴影效果:

[[UIColor colorWithWhite:0.0f alpha:0.5f] setFill];//阴影效果  根据透明度来设计
    UIRectFill( rect );
    CGRect holeRect = CGRectMake(holeX, holeY, holeWidth, holeHeight);//设置透明范围
    CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );    
    [[UIColor clearColor] setFill];
    UIRectFill( holeRectIntersection ); 

UIView设置边框:

    //UIView设置边框
    [[testView layer] setCornerRadius:5];
    [[testView layer] setBorderWidth:2];
    [[testView layer] setBorderColor:[UIColor redColor].CGColor];


拉伸一张图片:

    UIImage *smallImage = [UIImage imageNamed:@"hc_03.png"];    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:smallImage];  
    [imageView setFrame:CGRectMake(0, 20, 320, 44)];  
    imageView.contentMode = UIViewContentModeScaleToFill;  
    [self.view addSubview:imageView];  
  
    [imageView release];  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值