How IOS uses Cashapelayer to implement complex view masking effects _ios

Source: Internet
Author: User

First, the case demonstration
Recently, when organizing a chat project, send a picture, there will be a triangular point of effect, point to the sender of this picture. The server returned to us is just a picture of a rectangle, how we put a rectangular picture or view, plus a layer of custom mask effect, this is what this article is about. The effect is as follows: The first one is a view mask effect, and the second one is the uiimageview of the mask effect.

II. Implementation Mechanism
There is a mask attribute in the layer layer of each view, and he is dedicated to setting the view's matte effect. The mask itself is also a layer layer. We just need to generate a custom layer, and then overlay the view that needs a mask. The problem is how to generate the layer of the irregular picture shown in the figure above. The cashapelayer can produce a closed space layer according to the sequence of several points. As shown in the following illustration:

Third, the implementation of the Code
Realization of the way to achieve the cashapelayer of the Viewmask category.

@implementation Cashapelayer (Viewmask) + (Instancetype) Createmasklayerwithview: (UIView *) view{cgfloat =
  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;

 } @end

Four, call the way

UIView *view = [[UIView alloc] Initwithframe:cgrectmake (n, M, MB)];
View.backgroundcolor = [Uicolor orangecolor];
[Self.view Addsubview:view];

Cashapelayer *layer = [Cashapelayer Createmasklayerwithview:view];
View.layer.mask = layer;

Author: yixiangboy
Address: http://blog.csdn.net/yixiangboy/article/details/50485250

The above is the entire content of this article, I hope to learn about iOS program design help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.