[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 ...
随机推荐
- 面试题_93_to_102_编程和代码相关的面试题
93)怎么检查一个字符串只包含数字?(解决方案) 94)Java 中如何利用泛型写一个 LRU 缓存?(答案<) 95)写一段 Java 程序将 byte 转换为 long?(答案) 95)在不 ...
- 使用netcat进行反弹链接的shellcode
from:http://morgawr.github.io/hacking/2014/03/29/shellcode-to-reverse-bind-with-netcat/ 这篇文章主要是谈,在远程 ...
- poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)
题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一 ...
- Codeforces Round #237 (Div. 2) B. Marathon(卡long long)
题目:http://codeforces.com/contest/404/problem/B #include <iostream> #include <cstring> #i ...
- ionic安装拍照选照片插件
1.安装插件,也可以用ionic plugin add .... phonegap local plugin add https://git-wip-us.apache.org/repos/asf/c ...
- Mysql线程池优化笔记
Mysql线程池优化我是总结了一个站长的3篇文章了,这里我整理到一起来本文章就分为三个优化段了,下面一起来看看. Mysql线程池系列一(Thread pool FAQ) 首先介绍什么是mys ...
- [Papers]NSE, $u_3$, Lebesgue space [Cao-Titi, IUMJ, 2008]
$$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{2}{3}+\frac{2}{3q},\quad \fra ...
- 编写一个循环将list容器的元素逆序输出
<c++ primer>P270,习题9.9 实现代码如下: #include<iostream> #include<list> using namespace s ...
- <转>ERP的测试用例模板
1页面部分(1) 页面清单是否完整(是否已经将所需要的页面全部都列出来了)(2) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示)(3) 页面在窗口中的显示是否正确.美观( ...
- ASP.NET CS文件中输出JavaScript脚本的3种方法以及区别
Response.Write 与 Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...