iOS 檢視控制元件設定圓角、陰影

b10l07發表於2017-08-22
1501113-a09a196feec15014.png
上下兩種不同處理方式
 //imageView的superView
    UIView *fangkuanView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    //imageView的superView 的陰影view
    UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 300, 200)];//這裡的大小將不影響fangkuanView1 的顯示(即這裡只起 設定frame.origin的作用)
    //陰影設定
    shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
    shadowView.layer.shadowOffset = CGSizeMake(4, 4);
    shadowView.layer.shadowOpacity = 0.5;
    shadowView.layer.shadowRadius = 4.0;
    shadowView.layer.cornerRadius = 10.0;
    
    //superView
    fangkuanView1.layer.masksToBounds = YES;
    fangkuanView1.layer.cornerRadius = 10;
    fangkuanView1.clipsToBounds = YES;
    fangkuanView1.backgroundColor = [UIColor whiteColor];
    //imageView
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
    imageView.image = [UIImage imageNamed:@"atop"];
    
    [fangkuanView1 addSubview:imageView];
    [shadowView addSubview:fangkuanView1];
    [self.view addSubview:shadowView];

下面兩個圓角的設定


//設定任意角為圓角
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 400, 80, 80)];
    view2.backgroundColor = [UIColor redColor];
    [self.view addSubview:view2];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view2.bounds;
    maskLayer.path = maskPath.CGPath;
    view2.layer.mask = maskLayer;

相關文章