Swift/OC 为View或ImageView设置聊天消息的小三角

前端之家收集整理的这篇文章主要介绍了Swift/OC 为View或ImageView设置聊天消息的小三角前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Swift代码


override func viewDidLoad() {
    super.viewDidLoad()

    //UIImageView
    let showView = UIImageView(frame: CGRectMake(100,100,100))
    showView.image = UIImage(named: "Snip20160422_3")
    showView.layer.mask = test(showView)
    self.view.addSubview(showView)

    //UIView
    let blankView = UIView(frame: CGRectMake(250,100))
    blankView.backgroundColor = UIColor.greenColor()
    blankView.layer.mask = test(blankView)
    self.view.addSubview(blankView)
}

///把设置小三角
func test(showView: UIView) -> CAShapeLayer {
    let viewWidth = CGFloat(CGRectGetWidth(showView.frame))
    let viewHeight = CGFloat(CGRectGetHeight(showView.frame))

    //所占的宽度,整个view所占的宽度不会变,已经被制定,所以这个宽度会占用整个view的宽度,
    let rightSpace: CGFloat = 10
    //距离顶部的距离
    let topSpace: CGFloat = 30

    let point1 = CGPointMake(0,0)
    let point2 = CGPointMake(viewWidth - rightSpace,0)
    let point3 = CGPointMake(viewWidth - rightSpace,topSpace)
    let point4 = CGPointMake(viewWidth,topSpace)
    let point5 = CGPointMake(viewWidth - rightSpace,topSpace + 10)
    let point6 = CGPointMake(viewWidth - rightSpace,viewHeight)
    let point7 = CGPointMake(0,viewHeight)

    let path = UIBezierPath()
    path.moveToPoint(point1)
    path.addLineToPoint(point2)
    path.addLineToPoint(point3)
    path.addLineToPoint(point4)
    path.addLineToPoint(point5)
    path.addLineToPoint(point6)
    path.addLineToPoint(point7)

    let layer = CAShapeLayer()
    layer.path = path.CGPath
    return layer
    }
}

OC代码


- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(100,100)];
    blankView.backgroundColor = [UIColor redColor];
    blankView.layer.mask = [self testWithView:blankView];
    [self.view addSubview:blankView];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(250,100)];
    imageView.image = [UIImage imageNamed:@"Snip20160422_3"];
    imageView.layer.mask = [self testWithView:blankView];
    [self.view addSubview:imageView];
}

- (CAShapeLayer *)testWithView:(UIView *)view
{
    CGFloat viewWidth = CGRectGetWidth(view.frame);
    CGFloat viewHeight = CGRectGetHeight(view.frame);

    CGFloat rightSpace = 10;
    CGFloat topSpace = 15;

    CGPoint point1 = CGPointMake(0,0);
    CGPoint point2 = CGPointMake(viewWidth-rightSpace,0);
    CGPoint point3 = CGPointMake(viewWidth-rightSpace,topSpace);
    CGPoint point4 = CGPointMake(viewWidth,topSpace);
    CGPoint point5 = CGPointMake(viewWidth-rightSpace,topSpace+10.);
    CGPoint point6 = CGPointMake(viewWidth-rightSpace,viewHeight);
    CGPoint point7 = CGPointMake(0,viewHeight);

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:point1];
    [path addLineToPoint:point2];
    [path addLineToPoint:point3];
    [path addLineToPoint:point4];
    [path addLineToPoint:point5];
    [path addLineToPoint:point6];
    [path addLineToPoint:point7];
    [path closePath];

    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    return layer;
}
原文链接:https://www.f2er.com/swift/323961.html

猜你在找的Swift相关文章