#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(, length)];
[bezierPath addLineToPoint:CGPointMake(length, )];
[bezierPath addLineToPoint:CGPointMake(rect.size.width - length, )];
[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(, rect.size.height - length)];
[bezierPath closePath];
return bezierPath;
} @end

在[UIView viewWillAppear:]方法中加入下面代码

[self.view setShape:[UIBezierPath cutCorner:self.view.bounds length:].CGPath];

效果:

################

让自定义 Button 响应自定义 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;
}
//判断触发点是否在规定的 Shape 内
if (CGPathContainsPoint(path, nil, point, nil)) {
return YES;
}
return NO;
}
@end

iOS: 控制UIView的外形的更多相关文章

  1. iOS 控制单个控制器旋转

    iOS 控制单个控制器旋转 控制单个ViewController 的旋转 //不旋转,保持竖屏 //iOS 5 - (BOOL) shouldAutorotateToInterfaceOrientat ...

  2. 荼菜的iOS笔记--UIView的几个Block动画

    前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...

  3. ios 关于UIView 的multipleTouchEnabled 和 exclusiveTouch

    做项目时发现,在一个界面上的2个button竟然可以同时点击,依次push进去了2个 controller!我就产生了疑问,一个view的multipleTouchEnabled属性默认是false啊 ...

  4. iOS学习——UIView的研究

    在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...

  5. iOS开发UIView.h简介

    1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...

  6. iOS 使用UIView的一种有效方法

    在一个典型的MVC结构 中,Model部分负责保存目标数据,View部分主要负责实现数据的界面以及将数据显示出来,二者在Controller的操作下协同工作.在iOS应用中,View的实现主要由UIV ...

  7. iOS之UIview动画

    一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持 执行动画所需要的工作由UIView类自动完成, ...

  8. IOS自定义UIView

    IOS中一般会用到几种方式自定义UIView 1.继承之UIView的存代码的自定义View 2.使用xib和代码一起使用的自定义View 3.存xib的自定义View(不需要业务处理的那种) 本文主 ...

  9. OpenGL ES: iOS 自定义 UIView 响应屏幕旋转

    iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...

随机推荐

  1. 详解利用Dockerfile构建mysql镜像并实现数据的初始化及权限设置

    本文提要 本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动MySQL服务接受外部连接,主要是通过Docker ...

  2. Kafka vs RocketMQ——多Topic对性能稳定性的影响

    引言 上期我们对比了RocketMQ和Kafka在多Topic场景下,收发消息的对比测试,RocketMQ表现稳定,而Kafka的TPS在64个Topic时可以保持13万,到了128个Topic就跌至 ...

  3. Eigen教程(9)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Eigen并没有为matrix提供直接的Reshape和Slicing的API,但是 ...

  4. ActiveMQ入门实例(转)

    转载自:http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html 1.下载ActiveMQ 去官方网站下载:http://act ...

  5. HTTP1.1 协议

    Response Headers 响应头 Expires 设置响应内容的过期时间 过期时间头信息属性值只能是HTTP格式的日期时间,HTTP的日期时间必须是格林威治时 间(GMT),而不是本地时间.举 ...

  6. Android——requestWindowFeature()的应用

    Android开发中经常会在setContentView(R.layout.XXX); 前设置requestWindowFeature(XXXX). 他的意思是需要软件全屏显示.自定义标题(使用按钮等 ...

  7. 1.php代码块

    一.登录 <form action="index.php?m=admin&c=index&a=login&dosubmit=1" method=&qu ...

  8. SpringBoot2 JPA No Identifier specified for entity的解决办法

    No Identifier specified for entity的错误 此类注解都在 import javax.persistence.*;包下     @Id     @GeneratedVal ...

  9. LeetCode practice

    子集和问题:给定一组数和一个值,从这组数中选出若干个数使其和为给定的值.这是个NPC问题. 1.https://leetcode.com/problems/counting-bits/#/soluti ...

  10. Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly

    Android Studio中出现提示: Gradle project sync failed. Basic functionality (eg. editing, debugging) will n ...