ios-Custom zigzag background view, using quartz2d

Source: Internet
Author: User

Because of the needs of the project, the UI designs a jagged background, and the program development results are as follows:


This used the quartz2d drawing, and my idea was to draw two jagged view:

Then two view slightly coincident a little, is the bottom of the view on the Internet to move, the top of the view of the lower sawtooth cover, but the result is disappointing, such as:

Finally, I re-painted the top view color of the jagged view on the bottom view, such as:

And then the bottom of the sawtooth view to move up, just the top of the view of the lower Sawtooth cover, and reached the final, such as


The specific code is as follows:

The SawtoothView.h header file code is as follows:

SawtoothView.h
//
Created by Hailonghan on 15/1/2.
Copyright (c) 2015 Cubead. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
* Sawtooth View
*/
@interface Sawtoothview:uiview

/**
* Set the wave background color, the number of waves, the height of the wave view
*
* @param color
* @param top color of Topcolor
* @param count Wave number
* Height of @param height wave
*/
-(void) SetColor: (Uicolor *) color Topcolor: (Uicolor *) Topcolor wavecount: (int) Count Waveheight: (cgfloat) height;

@end


The SAWTOOTHVIEW.M code is as follows:

//
sawtoothview.m
Easymarketing
//
Created by Hailonghan on 15/1/2.
Copyright (c) 2015 Cubead. All rights reserved.
//

#import "SawtoothView.h"

@interface Sawtoothview () {
int wavecount;
Uicolor *bgcolor;
Uicolor *viewtopcolor;
CGFloat viewheight;
}

@end

@implementation Sawtoothview

-(Instancetype) init
{
self = [super init];
if (self) {
Self.backgroundcolor = [Uicolor Whitecolor];
}
return self;
}

-(void) DrawRect: (CGRect) rect{
#pragma mark-First step: Get context
Get Drawing context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();

#pragma mark-Step two: Build the path
if (wavecount <= 0) {
Wavecount = 30;//default of 30
}

Width of a single wavy line
CGFloat itemwidth = Kscreen_width/wavecount;
The height of a single wavy line
CGFloat itemheight = 10;
The size of the entire view
if (viewheight <= 0) {
Viewheight = 50;//Default 50 size
}

Background color
if (BgColor = = nil) {
BgColor = [Uicolor blackcolor];
}

if (Viewtopcolor = = nil) {
Viewtopcolor = [Uicolor Orangecolor];
}

Move to the starting point, draw from top left to top right
Cgcontextmovetopoint (CTX, 0, itemheight);
for (int i = 0; i<wavecount; i++) {
int nextx = (i+1) *itemwidth;

Cgcontextaddlinetopoint (CTX, nextx-itemwidth*0.5, 0);
Cgcontextaddlinetopoint (CTX, NEXTX, itemheight);
}

Move top right to lower right corner
Cgcontextaddlinetopoint (CTX, Kscreen_width, viewheight-itemheight);

Lower right corner draw to lower left corner
for (int i = wavecount+1;i > 0;i--) {
int PreX = (i-1) *itemwidth;
Cgcontextaddlinetopoint (CTX, prex-itemwidth*0.5, viewheight);
Cgcontextaddlinetopoint (CTX, Prex-itemwidth, viewheight-itemheight);
}

#pragma mark-Step three: Draw the path to the view
Cgcontextsetrgbfillcolor (CTX, 1, 0, 0, 1);
Cgcontextsetfillcolorwithcolor (CTX, Bgcolor.cgcolor);
Cgcontextfillpath (CTX);


Start drawing the top fill
Cgcontextmovetopoint (CTX, 0, itemheight);
for (int i = 0; i<wavecount; i++) {
int nextx = (i+1) *itemwidth;
Cgcontextaddlinetopoint (CTX, nextx-itemwidth*0.5, 0);
Cgcontextaddlinetopoint (CTX, NEXTX, itemheight);
}
Cgcontextsetfillcolorwithcolor (CTX, Viewtopcolor.cgcolor);
Cgcontextaddlinetopoint (CTX, kscreen_width, 0);
Cgcontextaddlinetopoint (CTX, 0, 0);
Cgcontextfillpath (CTX);
}

/**
* Set the wave background color, the number of waves, the height of the wave view
*
* @param color
* @param top color of Topcolor
* @param count Wave number
* Height of @param height wave
*/
-(void) SetColor: (Uicolor *) color Topcolor: (Uicolor *) Topcolor wavecount: (int) Count Waveheight: (cgfloat) height{
BgColor = color;
Wavecount = count;
Viewheight = height;
Viewtopcolor = Topcolor;

[Self setneedsdisplay];
}

@end




Finally, the code is called in Viewcontroller:


Add a jagged view
Sawtoothview *sawtoothview1 = [Sawtoothview new];
Sawtoothview1.frame = CGRectMake (0, Cgrectgetmaxy (titlelabel.frame) +10, kscreen_width, 100);
[SawtoothView1 setcolor:[uicolor Warmgraycolor] topcolor:[uicolor Clearcolor] wavecount:30 waveheight:100];
[Self.view Addsubview:sawtoothview1];

Sawtoothview *sawtoothview2 = [Sawtoothview new];
Sawtoothview2.frame = CGRectMake (0, Cgrectgetmaxy (sawtoothview1.frame) -10, Kscreen_width, 200);
[SawtoothView2 setcolor:[uicolor Orangecolor] topcolor:[uicolor Clearcolor] wavecount:30 waveheight:100];
[Self.view Addsubview:sawtoothview2];



Note that: The value of y of frame SawtoothView2 is SawtoothView1 's frame maximum y value-10, this 10 is the height of the sawtooth, cover off on OK

ios-Custom zigzag background view, using quartz2d

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.