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.