Ios-The shape of the view

Source: Internet
Author: User
Tags uikit
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface uiview (Shape)

-(void ) Setshape: (cgpathref) shape;
@end
#import "Uiview+shape.h"

@implementation UIView (Shape)

-(void) Setshape: (cgpathref) Shape
{
    if ( Shape = = nil) {
        self.layer.mask = nil;
    }
    
    cashapelayer* Masklayer = [Cashapelayer layer];
    Masklayer.path = shape;
    Self.layer.mask = Masklayer;
}

@end
@interface Uibezierpath (Basicshape)
+ (Uibezierpath *) Cutcorner: (cgrect) originalframe length: (cgfloat) length;
@end
#import "Uibezierpath+basicshape.h"

@implementation Uibezierpath (basicshape)

+ (Uibezierpath *) Cutcorner: ( CGRect) originalframe Length: (cgfloat) length
{
    CGRect rect = originalframe;
    Uibezierpath *bezierpath = [Uibezierpath Bezierpath];

    [Bezierpath movetopoint:cgpointmake (0, length)];
    [Bezierpath addlinetopoint:cgpointmake (length, 0)];
    [Bezierpath addlinetopoint:cgpointmake (rect.size.width-length, 0)];
    [Bezierpath addlinetopoint:cgpointmake (rect.size.width, length)];
    [Bezierpath Addlinetopoint:cgpointmake (Rect.size.width, Rect.size.height-length)];
    [Bezierpath Addlinetopoint:cgpointmake (Rect.size.width-length, Rect.size.height)];
    [Bezierpath addlinetopoint:cgpointmake (length, rect.size.height)];
    [Bezierpath addlinetopoint:cgpointmake (0, Rect.size.height-length)];
    [Bezierpath Closepath];
    return bezierpath;
}

@end
How to: Add the following code in the [UIView viewwillappear:] Method
[Self.view Setshape:[uibezierpath cutCorner:self.view.bounds length:40]. Cgpath];

Effect:

Allow custom Button to respond to click events in Custom Shape
#import <UIKit/UIKit.h>
#import "uiview+shape.h"
#import "uibezierpath+basicshape.h"

@interface rfbutton:uibutton{
    cgpathref path;
}

@end
////  rfbutton.m
//Christapp////  Created by  haozhen Li on 13-12-6.
//

#import "RFButton.h"

@implementation Rfbutton

-(ID) initWithFrame: (CGRect) frame
{
    self = [super Initwithframe:frame];
    if (self) {
        //initialization code
    } return
    self;
}

-(void) Setshape: (cgpathref) shape
{
    [super Setshape:shape];
    Path = shape;
}

-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event
{
    if (cgpathisempty (path)) {return
        YES ;
    }
    Determine if the trigger point is within the specified Shape
    if (cgpathcontainspoint (path, nil, point, nil)) {return
        YES
    }
    return NO;
}
@end



Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.