Swift/OC에서 View 또는 ImageView에 대한 채팅 메시지의 작은 삼각형 설정

7985 단어

Swift 코드

override func viewDidLoad() {
    super.viewDidLoad()

    //UIImageView
    let showView = UIImageView(frame: CGRectMake(100, 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, 100, 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, 100, 100)];
    blankView.backgroundColor = [UIColor redColor];
    blankView.layer.mask = [self testWithView:blankView];
    [self.view addSubview:blankView];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(250, 100, 100, 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;
}

좋은 웹페이지 즐겨찾기