#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. Leetcode:Edit Distance 解题报告

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  2. java基础篇---Servlet过滤器

    Servlet过滤器从字面上的字意理解为景观一层次的过滤处理才达到使用的要求,而其实Servlet过滤器就是服务器与客户端请求与响应的中间层组件,在实际项目开发中Servlet过滤器主要用于对浏览器的 ...

  3. OAuth2.0 授权的工作原理

    作者:Barret李靖链接:https://www.zhihu.com/question/19781476/answer/81020455来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  4. web前端开发与iOS终端开发的异同[转]

    * {-webkit-tap-highlight-color: rgba(0,0,0,0);}html {-webkit-text-size-adjust: none;}body {font-fami ...

  5. 微信怎样做SEO

    微信也能做SEO.大家还不知道吧?今天上海SEO优化公司就和大家解说在微信上要怎么做SEO优化. 微信也有SEO?你会不会觉得我是说笑呢.事实上还就是.我也找不到什么好名词来介绍公众号上的排名,就用微 ...

  6. 从商业角度探讨API设计

    为Web设计.实现和维护API不仅仅是一项挑战:对很多公司来说,这是一项势在必行的任务.本系列将带领读者走过一段旅程,从为API确定业务用例到设计方法论,解决实现难题,并从长远的角度看待在Web上维护 ...

  7. es cat http://localhosts:9002/_cat/nodes?v

    http://localhosts:9002/_cat/nodes?v&h=http,version,jdk,disk.total,disk.used,disk.avail,disk.used ...

  8. IDEA 修改文件编码

    Intellij Idea 修改 properties 文件编码 现象:idea 默认的properties文件是GBK,当有中文时,不同的客户端配置的编码不同时,可能产生中文乱码. 解决:修改pro ...

  9. HashSet与TreeSet 区别

    HashSetHashSet有以下特点 不能保证元素的排列顺序,顺序有可能发生变化 不是同步的 集合元素可以是null,但只能放入一个null当向HashSet集合中存入一个元素时,HashSe ...

  10. js学习(一)-动态添加、修改、删除对象的属性和方法

    //-----------------------js代码--------------------------- function class1(){ } //-------------------- ...