[iOS UI进阶 - 1] 自定义控件
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] set];
UIRectFill(CGRectMake(, , , ));
}

- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 1.1创建第一个path
CGMutablePathRef linePath = CGPathCreateMutable(); // 1.2.描绘path
CGPathMoveToPoint(linePath, NULL, , );
CGPathAddLineToPoint(linePath, NULL, , ); // 1.3.添加linePath到上下文
CGContextAddPath(ctx, linePath); // 2添加一个圆的path
CGMutablePathRef circlePath = CGPathCreateMutable();
CGPathAddEllipseInRect(circlePath, NULL, CGRectMake(, , , ));
CGContextAddPath(ctx, circlePath); // 渲染上下文
CGContextStrokePath(ctx); // 释放path
CGPathRelease(linePath);
CGPathRelease(circlePath);
}

//
// MyImageView.h
// MyImageView
//
// Created by hellovoidworld on 14/12/31.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h> @interface MyImageView : UIView @property(nonatomic, weak) UIImage *image; @end
//
// MyImageView.m
// MyImageView
//
// Created by hellovoidworld on 14/12/31.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "MyImageView.h" @implementation MyImageView - (void)setImage:(UIImage *)image {
_image = image; // 重新设置了图片的时候,重绘
[self setNeedsDisplay];
} - (void)drawRect:(CGRect)rect {
[self.image drawInRect:rect];
} @end controller:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. MyImageView *imageView = [[MyImageView alloc] init];
imageView.frame = CGRectMake(, , , );
[self.view addSubview:imageView];
imageView.image = [UIImage imageNamed:@"M4"];
}


//
// MyTextView.h
// MyTextField
//
// Created by hellovoidworld on 14/12/31.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h> @interface MyTextView : UITextView @property(nonatomic, copy) NSString *placeHolderText; @end
//
// MyTextView.m
// MyTextField
//
// Created by hellovoidworld on 14/12/31.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "MyTextView.h" @interface MyTextView() <UITextViewDelegate> /** 原来的文本颜色 */
@property(nonatomic, weak) UIColor *originalTextColor; @end @implementation MyTextView // 重写初始化方法,设置代理
- (instancetype)init {
if (self = [super init]) {
self.returnKeyType = UIReturnKeyDone;
self.delegate = self;
} return self;
} // 重新设置了placeHolderText的时候要重绘一下控件
- (void)setPlaceHolderText:(NSString *)placeHolderText {
_placeHolderText = placeHolderText;
[self setNeedsDisplay];
} // 开始编辑,消除placeHolder
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if ([self.text isEqualToString:self.placeHolderText]) {
self.text = nil;
self.textColor = self.originalTextColor;
} return YES;
} // 结束编辑,如果文本为空,设置placeHolder
- (void)textViewDidEndEditing:(UITextView *)textView {
[self setNeedsDisplay];
} - (void)drawRect:(CGRect)rect {
if (self.text.length == ) {
self.text = self.placeHolderText;
self.textColor = [UIColor grayColor];
}
} @end
//
// UIImage+HW.m
// Watermark
//
// Created by hellovoidworld on 14/12/31.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "UIImage+HW.h" @implementation UIImage(HW) + (instancetype) image:(NSString *) image withWatermark:(NSString *) watermark {
// 取得背景图片
UIImage *bgImage = [UIImage imageNamed:image]; // 1.开启一个位图上下文
UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0); // 2.添加背景图片到位图上下文
[bgImage drawInRect:CGRectMake(, , bgImage.size.width, bgImage.size.height)]; // 3.添加水印图片
UIImage *watermarkImage = [UIImage imageNamed:watermark];
CGFloat scale = 0.5;
CGFloat margin = ;
CGFloat watermarkWidth = watermarkImage.size.width * scale;
CGFloat watermarkHeight = watermarkImage.size.width *scale;
CGFloat watermarkX = bgImage.size.width - watermarkWidth - margin;
CGFloat watermarkY = bgImage.size.height - watermarkHeight - margin;
[watermarkImage drawInRect:CGRectMake(watermarkX, watermarkY, watermarkWidth, watermarkHeight)]; // 4.取得合成后的图片
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); // 5.关闭图文上下文
UIGraphicsEndImageContext(); return resultImage;
} @end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIImage *watermarkedImage = [UIImage image:@"M4" withWatermark:@"a9ec8a13632762d0092abc3ca2ec08fa513dc619"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:watermarkedImage];
imageView.frame = CGRectMake(, , , );
[self.view addSubview:imageView];
}
// 压缩图片为PNG格式的二进制数据
NSData *data = UIImagePNGRepresentation(watermarkedImage); // 写入文件
NSString *path =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"watermarkedImage.png"];
NSLog(@"%@", path);
[data writeToFile:path atomically:YES];

/** 创建带有指定宽度和颜色边框的圆形头像图片 */
+ (instancetype) imageOfCircleHeadIcon:(NSString *) image withBorderWidth:(CGFloat) borderWidth borderColor:(UIColor *) borderColor {
// 取得图片
UIImage *headIconImage = [UIImage imageNamed:image]; // 开启图片上下文
CGFloat backImageWidth = headIconImage.size.width + * borderWidth;
CGFloat backImageHeight = headIconImage.size.height + * borderWidth;
CGSize backImageSize = CGSizeMake(backImageWidth, backImageHeight);
UIGraphicsBeginImageContextWithOptions(backImageSize, NO, 0.0); // 描绘背景大圆
[borderColor set]; // 设置圆环颜色
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat backCircleRadius = backImageWidth * 0.5; // 大圆半径
CGFloat backCircleX = backCircleRadius; // 大圆圆心X
CGFloat backCircleY = backCircleRadius; // 大圆圆心Y
CGContextAddArc(ctx, backCircleX, backCircleY, backCircleRadius, , M_PI * , );
CGContextFillPath(ctx); // 描绘用来显示图片的小圆
CGFloat frontCircleRadius = backCircleRadius - borderWidth; // 图片小圆半径
CGFloat frontCircleX = backCircleX;
CGFloat frontCircleY = backCircleY;
CGContextAddArc(ctx, frontCircleX, frontCircleY, frontCircleRadius, , M_PI * , ); // 裁剪(后面描绘的将会受到裁剪)
CGContextClip(ctx); // 添加图片到上下文
[headIconImage drawInRect:CGRectMake(borderWidth, borderWidth, headIconImage.size.width, headIconImage.size.height)]; // 取得合成后的图片
UIImage *headIconResultImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭图片上下文
UIGraphicsEndImageContext(); return headIconResultImage;
}


/** 点击“屏幕截图” */
- (IBAction)screenShotcut {
// 延迟截图, 防止截到的时按钮被按下的状态
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ UIView *view = self.view; // 1.开启位图上下文
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0); // 2.渲染控制器view的layer到上下文
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; // 3.从上下文取得图片
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext(); // 4.结束上下文
UIGraphicsEndImageContext(); // 存储图片
NSData *data = UIImagePNGRepresentation(screenImage);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"screenShot.png"];
NSLog(@"%@", path);
[data writeToFile:path atomically:YES]; }); }

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 开启图片上下文
CGFloat rowW = self.view.frame.size.width;
CGFloat rowH = ;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0); CGContextRef ctx = UIGraphicsGetCurrentContext();
// 画色块背景
[[UIColor redColor] set];
CGContextAddRect(ctx, CGRectMake(, , rowW, rowH));
CGContextFillPath(ctx); // 画线
[[UIColor greenColor] set];
CGFloat lineWidth = ;
CGContextSetLineWidth(ctx, lineWidth);
CGFloat lineX = ;
CGFloat lineY = rowH - lineWidth;
CGContextMoveToPoint(ctx, lineX, lineY);
CGContextAddLineToPoint(ctx, rowW - lineX, lineY);
CGContextStrokePath(ctx); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 使用平铺方式铺满屏幕
[self.view setBackgroundColor:[UIColor colorWithPatternImage:image]]; UIGraphicsEndImageContext();
}

[iOS UI进阶 - 1] 自定义控件的更多相关文章
- [iOS UI进阶 - 0] Quiartz2D
A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...
- iOS UI进阶-1.0 Quartz2D
概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...
- [iOS UI进阶 - 6.1] 核心动画CoreAnimation
A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...
- [iOS UI进阶 - 6.0] CALayer
A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用 2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...
- [iOS UI进阶 - 3.1] 触摸事件的传递
A.事件的产生和传递 发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发 ...
- [iOS UI进阶 - 5.0] 手势解锁Demo
A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件 code source: https://github.com/hellovoidworld/GestureUnlockDemo B ...
- [iOS UI进阶 - 3.0] 触摸事件的基本处理
A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...
- [iOS UI进阶 - 2.3] 彩票Demo v1.3
A.需求 真机调试 "关于”模块 存储开关状态 打电话.发短信 应用评分 打开其他应用 cell 在iOS6 和 iOS7的适配 block的循环引用 屏幕适配 code source: ...
- [iOS UI进阶 - 2.0] 彩票Demo v1.0
A.需求 1.模仿“网易彩票”做出有5个导航页面和相应功能的Demo 2.v1.0 版本搭建基本框架 code source:https://github.com/hellovoidworld/H ...
随机推荐
- Spring学习8-Spring事务管理
http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...
- git文件未改动pull的时候提示冲突
今天在mac下使用git工具,出现一个很奇怪的问题. 先声明当前工作目录是干净的,运行 git status 没有任何文件改动,且没有任何需要push的文件. 我执行 git pull 命令,直接提示 ...
- hdu 3501 Calculation 2 (欧拉函数)
题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质 ...
- Ural1076(km算法)
题目大意 给出n*n表格,第a[i,j]表示i到j的权值,现在我们要将每个a[i,j]=sum[j]-a[i,j], 求出当前二分图a[][]最小匹配 最小匹配只需将权值取负后,求二分图最大匹配,使用 ...
- UVa 10674 (求两圆公切线) Tangents
题意: 给出两个圆的圆心坐标和半径,求这两个圆的公切线切点的坐标及对应线段长度.若两圆重合,有无数条公切线则输出-1. 输出是按照一定顺序输出的. 分析: 首先情况比较多,要一一判断,不要漏掉. 如果 ...
- BestCoder Round #35
A 题意:给出n个黑球,m个白球,每次取1个球,取了n+m次以后,会生成一个随机的01串S, 如果第i次取出的是黑球,则s[i]=1,如果是白色的,那么s[i]=0, 问01串在S中出现的期望次数 大 ...
- WinForm 禁止调整大小、禁止最大化窗口
这个设置代码必须添加到*.designer.cs中,就是自动隐藏的那部分: #region Windows Form Designer generated code 一般窗体设置的代码会生成到最后面, ...
- javascript倒计时代码
其实就是用两个时间戳相减,余数转换为日期,就是所剩的年月日时分秒,不过年份-1970 $scope.timerID = null; $scope.timerRunning = false;$scope ...
- OpenGL 顶点缓存对象
顶点缓存对象(Vertex Buffer Object,简称 VBO),允许开发者根据情况把顶点数据放到显存中. 如果不用 VBO,用 glVertexPointer / glNormalPointe ...
- nginx - ssl 配置 - globelsign ssl
前提: 3个文件 - domain.csr.domain.key.xxx.cer 简述: 1. 本地生成 .key文件 [附件] 2. 再利用key文件,生成csr(certificate Secu ...