iOS mask Layer 详解

来源:互联网 发布:python generator 编辑:程序博客网 时间:2024/06/05 05:05

CAShapeLayer 做为mask时,如何修改CAShapeLayer的边框颜色?   

path 是一个UIBezierPath,是一个多边形,
我需要显示多边形内部的东西,其余舍弃,
但我把shapeLayer作为UIImageView.layer的mask时,我设置的shapeLayer的颜色不起作用,是哪出了问题,
?
1
2
3
4
5
6
7
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer = [CAShapeLayer layer];
    shapeLayer.path           = path.CGPath;
    shapeLayer.strokeColor    = [UIColor redColor].CGColor;
    shapeLayer.borderColor    = [UIColor redColor].CGColor;
    shapeLayer.lineWidth      = 2;
    imageView.layer.mask = shapeLayer;
iOS 的mask layer 层:
mask是个遮罩,只有透明度信息有用,颜色信息是被忽略的,你设置它的颜色没有意义,不知道您是想实现什么功能?
您如果一定要一个边框,也可以这么做试试看

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer = [CAShapeLayer layer];
    shapeLayer.path           = path.CGPath;
CAShapeLayer *borderLayer=[CAShapeLayer layer];
    borderLayer.path    =   path.CGPath;
    borderLayer.fillColor  = [UIColor clearColor].CGColor;
    borderLayer.strokeColor    = [UIColor redColor].CGColor;
    borderLayer.lineWidth      = 2;
    borderLayer.frame=imageView.bounds;

    imageView.layer.mask = shapeLayer;
    [imageView.layer addSublayer:borderLayer];

参考文章:http://www.cocoachina.com/bbs/read.php?tid=295611
使用CALayer的mask属性来设置图片圆角

- (CALayer )userHeaderMask:(UIView )originalView{

CALayer * userMask = [CALayer layer];userMask.frame = originalView.bounds;CGFloat x = originalView.frame.size.width/2;CGFloat y = originalView.frame.size.height/2;self.userMaskLayer = [CAShapeLayer layer];self.userMaskLayer.bounds = originalView.bounds;self.userMaskLayer.path   = [UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES].CGPath;self.userMaskLayer.position  = CGPointMake(50, 50);[userMask addSublayer:self.userMaskLayer];return userMask;}


http://www.cnblogs.com/ihojin/p/custom-shape-view.html

iOS: 控制UIView的外形

复制代码
#import <UIKit/UIKit.h>#import <QuartzCore/QuartzCore.h>@interface UIView (Shape)- (void)setShape:(CGPathRef)shape;@end
复制代码
复制代码
#import "UIView+Shape.h"@implementation UIView (Shape)- (void)setShape:(CGPathRef)shape{    if (shape == nil) {        self.layer.mask = nil;    }        CAShapeLayer* maskLayer = [CAShapeLayer layer];    maskLayer.path = shape;    self.layer.mask = maskLayer;}@end
复制代码
@interface UIBezierPath (BasicShape)+ (UIBezierPath *)cutCorner:(CGRect)originalFrame length:(CGFloat)length;@end
复制代码
#import "UIBezierPath+BasicShape.h"@implementation UIBezierPath (BasicShape)+ (UIBezierPath *)cutCorner:(CGRect)originalFrame length:(CGFloat)length{    CGRect rect = originalFrame;    UIBezierPath *bezierPath = [UIBezierPath bezierPath];    [bezierPath moveToPoint:CGPointMake(0, length)];    [bezierPath addLineToPoint:CGPointMake(length, 0)];    [bezierPath addLineToPoint:CGPointMake(rect.size.width - length, 0)];    [bezierPath addLineToPoint:CGPointMake(rect.size.width, length)];    [bezierPath addLineToPoint:CGPointMake(rect.size.width, rect.size.height - length)];    [bezierPath addLineToPoint:CGPointMake(rect.size.width - length, rect.size.height)];    [bezierPath addLineToPoint:CGPointMake(length, rect.size.height)];    [bezierPath addLineToPoint:CGPointMake(0, rect.size.height - length)];    [bezierPath closePath];    return bezierPath;}@end
复制代码

在[UIView viewWillAppear:]方法中加入下面代码

[self.view setShape:[UIBezierPath cutCorner:self.view.bounds length:40].CGPath];

效果:

 

################

让自定义 Button 响应自定义 Shape 内的点击事件

复制代码
#import <UIKit/UIKit.h>#import "UIView+Shape.h"#import "UIBezierPath+BasicShape.h"@interface RFButton : UIButton{    CGPathRef path;}@end
复制代码
复制代码
////  RFButton.m//  ChristApp////  Created by Haozhen Li on 13-12-6.////#import "RFButton.h"@implementation RFButton- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code    }    return self;}- (void)setShape:(CGPathRef)shape{    [super setShape:shape];    path = shape;}- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{    if (CGPathIsEmpty(path)) {        return YES;    }    //判断触发点是否在规定的 Shape 内    if (CGPathContainsPoint(path, nil, point, nil)) {        return YES;    }    return NO;}@end
复制代码


0 0
原创粉丝点击