Have you seen enough of the square and the orderly display shown by default in iOS?
This paper presents an algorithm design and implementation of a random triangular tiling layout. This arrangement is a tradeoff between rules and randomness, making it look fresh and not messy.
The focus of this implementation is the layout algorithm design and implementation, you can change the color or add pictures.
Latest Source code: https://github.com/duzixi/Varied-Layouts (Continuous maintenance, welcome to mutual powder)
The starting address of the blog: Http://blog.csdn.net/duzixi
The layout generates the following effects:
The core algorithm design and code implementation are as follows:
triangleviewcontroller.m//trianglelayout////Created by Chieh XI (duzixi) on 14-8-24.//Copyright (c) 2014 lanou3g.co M All rights reserved.//#import "TriangleViewController.h" #import "Triangle.h" #import <quartzcore/quartzcore.h > #define PADDING 10#define SIZE 100#define COL (self.view.frame.size.width/size) #define ROW ( self.view.frame.size.height/size) @interface Triangleviewcontroller () @end @implementation triangleviewcontroller-( void) viewdidload{[Super viewdidload];//do any additional setup after loading the view, typically from a nib. _randompoints = [Nsmutablearray array]; _triangles = [Nsmutablearray array]; int oy =-SIZE/2; for (int i = 0, i < ROW + 1; i++) {for (int j = 0; J < COL; J + +) {int ox = (i% 2 = = 1)? J * SI ZE:J * SIZE-0.5 * SIZE; Ox-= SIZE/4; Step 1: Draw lattice UIView *view = [[UIView alloc] Initwithframe:cgrectmake (OX, I * SIZE + oy, size, size)]; if ((j + i)% 2 = = 0) {View.backgroundcolor = [uicolor Graycolor]; }//[Self.view Addsubview:view]; Step 2: Generate random points in the lattice int x = arc4random ()% (size-padding * 2) + view.frame.origin.x + PADDING; int y = arc4random ()% (size-padding * 2) + VIEW.FRAME.ORIGIN.Y + PADDING; Cgpoint p = cgpointmake (x, y); Nsvalue *v = [Nsvalue valuewithcgpoint:p]; [_randompoints Addobject:v]; UIView *pview = [[UIView alloc] Initwithframe:cgrectmake (p.x, P.Y, 2, 2)]; Pview.backgroundcolor = [Uicolor Bluecolor]; [Self.view Addsubview:pview]; UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (p.x, P.Y, 50, 20)]; Label.text = [NSString stringwithformat:@ "%lu", (unsigned long) [_randompoints Count]]; [Self.view Addsubview:label]; }} int k = 0; for (int i = 0; i < ROW; i++) {NSLog(@"-----------------"); for (int j = 0; J < COL-1, J + +) {//Step 3: I triangle will point classification k = i * COL + j + i; Triangle *t = [[Triangle alloc] init]; T.P1 = [_randompoints[k] cgpointvalue]; T.P2 = [_randompoints[k + 1] cgpointvalue]; int col = i% 2 = = 0? Col:col + 1; T.P3 = [_randompoints[k + 1 + col] Cgpointvalue]; NSLog (@ "%d%d%d", K, K + 1, K + 1 + col); [_triangles Addobject:t]; Step 4: The rectangular region where the triangle is generated int minX = t.p1.x < t.p2.x? t.p1.x:t.p2.x; MinX = MinX < t.p3.x? minx:t.p3.x; int miny = T.p1.y < t.p2.y? T.P1.Y:T.P2.Y; Miny = Miny < t.p3.y? MINY:T.P3.Y; int MaxX = t.p1.x > t.p2.x? t.p1.x:t.p2.x; MaxX = MaxX > t.p3.x? maxx:t.p3.x; int maxy = T.P1.Y > t.p2.y? T.P1.Y:T.P2.Y; Maxy = Maxy > t.p3.y? MaxY:T.P3.Y; k++; Uiimageview *view = [[Uiimageview alloc] Initwithframe:cgrectmake (MinX, Miny, Maxx-minx, Maxy-miny)]; View.backgroundcolor = [Uicolor Orangecolor]; Step 5: Create a mask based on a triangle uibezierpath *path = [Uibezierpath Bezierpath]; [Path Movetopoint:cgpointmake (T.p1.x-minx, T.p1.y-miny)]; [Path Addlinetopoint:cgpointmake (T.p2.x-minx, T.p2.y-miny)]; [Path Addlinetopoint:cgpointmake (T.p3.x-minx, T.p3.y-miny)]; [Path Closepath]; Cashapelayer *masklayer = [Cashapelayer layer]; Masklayer.path = [path Cgpath]; View.layer.mask = Masklayer; [Self.view Addsubview:view]; }}}-(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
triangle.h// trianglelayout//// Created by Chieh XI (duzixi) on 14-8-24.// Copyright (c) 2014 Lanou3g.com All rights reserved.//#import <Foundation/Foundation.h> @interface Triangle:nsobject@property ( Nonatomic, assign) Cgpoint P1, @property (nonatomic, assign) cgpoint p2; @property (nonatomic, assign) Cgpoint P3; @end
In order for everyone to see the layout process, the code retains some intermediate procedures (commented out).
Open the comments to see the grid, random points and so on.
The next goal is to rewrite it into the Uicollectionview layout, so please look forward to
Random triangle tiling Layout algorithm (iOS implementation)