详解iOS开发之自定义View
iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View。创建一个 Window-based Application程序,在其中添加一个Hypnosister的类,这个类选择继承UIObject。修改这个类,使他继承:UIView
- @interface HypnosisView : UIView
自定义View的关键是定义drawRect: 方法,因为主要是通过重载这个方法,来改变view的外观。例如,可以使用下面代码绘制一个很多环中环的效果的view。
- View Code
- - (void)drawRect:(CGRect)rect
- {
- // What rectangle am I filling? CGRect bounds = [self bounds];
- // Where is its center? CGPoint center;
- center.x = bounds.origin.x + bounds.size.width / 2.0;
- center.y = bounds.origin.y + bounds.size.height / 2.0;
- // From the center how far out to a corner? float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
- // Get the context being drawn upon CGContextRef context = UIGraphicsGetCurrentContext();
- // All lines will be drawn 10 points wide CGContextSetLineWidth(context, 10);
- // Set the stroke color to light gray [[UIColor lightGrayColor] setStroke];
- // Draw concentric circles from the outside in for (float currentRadius = maxRadius; currentRadius > 0;
- currentRadius -= 20) {
- CGContextAddArc(context, center.x, center.y,
- currentRadius, 0.0, M_PI * 2.0, YES);
- CGContextStrokePath(context);
- }
- }
这样view的效果如下图:
我们可以继续绘制一些东西,比如绘制文字,将下面代码添加带这个方法后面。
- // Create a string NSString *text = @"我是朱祁林,不是朱麒麟";
- // Get a font to draw it in UIFont *font = [UIFont boldSystemFontOfSize:28];
- // Where am I going to draw it? CGRect textRect;
- textRect.size = [text sizeWithFont:font];
- textRect.origin.x = center.x - textRect.size.width / 2.0;
- textRect.origin.y = center.y - textRect.size.height / 2.0;
- // Set the fill color of the current context to black [[UIColor blackColor] setFill];
- // Set the shadow to be offset 4 points right, 3 points down,
- // dark gray and with a blur radius of 2 points CGSize offset = CGSizeMake(4, 3);
- CGColorRef color = [[UIColor darkGrayColor] CGColor];
- CGContextSetShadowWithColor(context, offset, 2.0, color);
- // Draw the string [text drawInRect:textRect
- withFont:font];
效果:
如果view过大,我们可以把它放置到一个UIScrollView中间,这样就可以进行拖动了。UIScrollView与View的关系如下图:
使用下面代码创建一个比iPhone屏幕大4倍的View,然后通过UIScrollView来展示,代码如下:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- //创建一个窗体大小的CGRect
- CGRect wholeWindow = [[self window] bounds];
- // 创建一个窗体大小的HypnosisView实例
- view = [[HypnosisView alloc] initWithFrame:wholeWindow];
- UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
- [[self window] addSubview:scrollView];
- // Make your view twice as large as the window CGRect reallyBigRect;
- reallyBigRect.origin = CGPointZero;
- reallyBigRect.size.width = wholeWindow.size.width * 2.0;
- reallyBigRect.size.height = wholeWindow.size.height * 2.0;
- [scrollView setContentSize:reallyBigRect.size];
- CGPoint offset;
- offset.x = wholeWindow.size.width * 0.5;
- offset.y = wholeWindow.size.height * 0.5;
- [scrollView setContentOffset:offset];
- // Create the view view = [[HypnosisView alloc] initWithFrame:reallyBigRect];
- [view setBackgroundColor:[UIColor clearColor]];
- [scrollView addSubview:view];
- [scrollView release];
- [[UIApplication sharedApplication] setStatusBarHidden:YES
- withAnimation:UIStatusBarAnimationFade];
- [[self window] makeKeyAndVisible];
- return YES;
- }
这样我们就可以拖动来展示看不到的view了,如下图:
通过UIScrollView我们还可以设置view的缩放功能,将下面代码添加到中。这样我们就可以使用两根手指缩放view了。
- // Enable zooming
- [scrollView setMinimumZoomScale:0.5];
- [scrollView setMaximumZoomScale:5];
- [scrollView setDelegate:self];
小结:详解iOS开发之自定义View的内容介绍完了,简单的总结了一下自定义view的使用,希望本文对你有所帮助!本文为了方便友们更好的去学IOS开发中的View,提供代码下载,地址为:http://files.cnblogs.com/zhuqil/Hypnosister.zip 。
详解iOS开发之自定义View的更多相关文章
- iOS开发之自定义表情键盘(组件封装与自动布局)
下面的东西是编写自定义的表情键盘,话不多说,开门见山吧!下面主要用到的知识有MVC, iOS开发中的自动布局,自定义组件的封装与使用,Block回调,CoreData的使用.有的小伙伴可能会问写一个自 ...
- 深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS
深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS ios中并不是所有的对象都支持copy,mutableCopy,遵守NSCopying 协议的类可以发送copy消息,遵守NSMutab ...
- 超全详解Java开发环境搭建
摘自:https://www.cnblogs.com/wangjiming/p/11278577.html 超全详解Java开发环境搭建 在项目产品开发中,开发环境搭建是软件开发的首要阶段,也是必 ...
- IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...
- 详解LUA开发工具及其环境配置
LUA开发工具及其环境配置是本文要介绍的内容,主要是来了解并学习lua开发工具的使用和环境的配置,第一次接触LUA的话,就跟本人一起学习吧.看我能不能忽悠到你. LUA是语言,那么一定有编写的工具.第 ...
- [转]javascript console 函数详解 js开发调试的利器
javascript console 函数详解 js开发调试的利器 分步阅读 Console 是用于显示 JS和 DOM 对象信息的单独窗口.并且向 JS 中注入1个 console 对象,使用该 ...
- Java-异常机制详解以及开发时异常设计的原则要求
Java-异常机制详解以及开发时异常设计的原则要求 http://blog.csdn.net/Jack__Frost/article/details/52760930?locationNum=6
- 详解WebService开发中四个常见问题(2)
详解WebService开发中四个常见问题(2) WebService开发中经常会碰到诸如WebService与方法重载.循环引用.数据被穿该等等问题.本文会给大家一些很好的解决方法. AD:WO ...
- 详解WebService开发中四个常见问题(1)
详解WebService开发中四个常见问题(1) WebService开发中经常会碰到诸如WebService与方法重载.循环引用.数据被穿该等等问题.本文会给大家一些很好的解决方法. AD:WO ...
随机推荐
- ROS验证publisher和subscriber
在前面的两篇博客中我们用C++在ROS中创建了一个发布者和接收者,并使用catkin_make构建了新的节点,下面就需要验证一下,我们写的是否正确. 首先运行roscore roscore 在使用ca ...
- poj 3228 Gold Transportation 二分+网络流
题目链接 给出n个城市, 每个城市有一个仓库, 仓库有容量限制, 同时每个城市也有一些货物, 货物必须放到仓库中. 城市之间有路相连, 每条路有长度. 因为有些城市的货物量大于仓库的容量, 所以要运到 ...
- APM代码学习笔记3:执行过程
以Linux平台ArduPlane为例 \ArduPlane\Plane.cpp 定义Plane类 继承自AP_HAL::HAL::Callbacks ,获取hal对象. \ArduPlane\Ard ...
- python基础学习笔记5--对象
对象(object) 1.对象(object): 面向对象程序设计重要术语. 对象的特性:多态性.封装性.继承性 >>def add(x,y): return x+y 对于很多类型的参数都 ...
- 几篇SIEM文章
http://infosecnirvana.com/tag/siem-rule-types/ http://www.tripwire.com/state-of-security/security-da ...
- Android Studio 中快速提取方法
在开发过程中,有时在一个方法内部写了过多的代码,然后想要把一些代码提取出来封装下,分离开放在一个单独的方法里,可能你的做法是直接选中后Ctrl + 叉,或者 Ctrl + C,但在Android St ...
- date命令使用
date命令的帮助信息 [root@localhost source]# date --help用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] ...
- Android企业级程序完全退出的解决方案【转】
http://blog.csdn.net/wangjinyu501/article/details/8763552 问题描述 在平常开发的过程中可以发现,很多开发者对于程序的退出都没有去认真的解决.一 ...
- SqlCacheDependency的使用
最近项目需要几秒就获取一次数据,查数据库服务器压力会很大,因此用缓存技术来缓解服务器压力. 使用SqlCacheDependency采用轮询的方式来获取缓存,SqlDependency查询通知的方式来 ...
- Android应用开发基础篇(9)-----SharedPreferences
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/27/2370319.html 一.概述 对于SharedPreferences,我吧它理解为一种 ...