iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View。创建一个 Window-based Application程序,在其中添加一个Hypnosister的类,这个类选择继承UIObject。修改这个类,使他继承:UIView

  1. @interface HypnosisView : UIView

自定义View的关键是定义drawRect: 方法,因为主要是通过重载这个方法,来改变view的外观。例如,可以使用下面代码绘制一个很多环中环的效果的view

  1. View Code
  2. - (void)drawRect:(CGRect)rect
  3. {
  4. // What rectangle am I filling?    CGRect bounds = [self bounds];
  5. // Where is its center?    CGPoint center;
  6. center.x = bounds.origin.x + bounds.size.width / 2.0;
  7. center.y = bounds.origin.y + bounds.size.height / 2.0;
  8. // From the center how far out to a corner?    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
  9. // Get the context being drawn upon    CGContextRef context = UIGraphicsGetCurrentContext();
  10. // All lines will be drawn 10 points wide    CGContextSetLineWidth(context, 10);
  11. // Set the stroke color to light gray    [[UIColor lightGrayColor] setStroke];
  12. // Draw concentric circles from the outside in    for (float currentRadius = maxRadius; currentRadius > 0;
  13. currentRadius -= 20)    {
  14. CGContextAddArc(context, center.x, center.y,
  15. currentRadius, 0.0, M_PI * 2.0, YES);
  16. CGContextStrokePath(context);
  17. }
  18. }

这样view的效果如下图:

我们可以继续绘制一些东西,比如绘制文字,将下面代码添加带这个方法后面。

  1. // Create a string    NSString *text = @"我是朱祁林,不是朱麒麟";
  2. // Get a font to draw it in    UIFont *font = [UIFont boldSystemFontOfSize:28];
  3. // Where am I going to draw it?    CGRect textRect;
  4. textRect.size = [text sizeWithFont:font];
  5. textRect.origin.x = center.x - textRect.size.width / 2.0;
  6. textRect.origin.y = center.y - textRect.size.height / 2.0;
  7. // Set the fill color of the current context to black     [[UIColor blackColor] setFill];
  8. // Set the shadow to be offset 4 points right, 3 points down,
  9. // dark gray and with a blur radius of 2 points     CGSize offset = CGSizeMake(4, 3);
  10. CGColorRef color = [[UIColor darkGrayColor] CGColor];
  11. CGContextSetShadowWithColor(context, offset, 2.0, color);
  12. // Draw the string    [text drawInRect:textRect
  13. withFont:font];

效果:

如果view过大,我们可以把它放置到一个UIScrollView中间,这样就可以进行拖动了。UIScrollView与View的关系如下图:

使用下面代码创建一个比iPhone屏幕大4倍的View,然后通过UIScrollView来展示,代码如下:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. //创建一个窗体大小的CGRect
  4. CGRect wholeWindow = [[self window] bounds];
  5. // 创建一个窗体大小的HypnosisView实例
  6. view = [[HypnosisView alloc] initWithFrame:wholeWindow];
  7. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
  8. [[self window] addSubview:scrollView];
  9. // Make your view twice as large as the window    CGRect reallyBigRect;
  10. reallyBigRect.origin = CGPointZero;
  11. reallyBigRect.size.width = wholeWindow.size.width * 2.0;
  12. reallyBigRect.size.height = wholeWindow.size.height * 2.0;
  13. [scrollView setContentSize:reallyBigRect.size];
  14. CGPoint offset;
  15. offset.x = wholeWindow.size.width * 0.5;
  16. offset.y = wholeWindow.size.height * 0.5;
  17. [scrollView setContentOffset:offset];
  18. // Create the view    view = [[HypnosisView alloc] initWithFrame:reallyBigRect];
  19. [view setBackgroundColor:[UIColor clearColor]];
  20. [scrollView addSubview:view];
  21. [scrollView release];
  22. [[UIApplication sharedApplication] setStatusBarHidden:YES
  23. withAnimation:UIStatusBarAnimationFade];
  24. [[self window] makeKeyAndVisible];
  25. return YES;
  26. }

这样我们就可以拖动来展示看不到的view了,如下图:

通过UIScrollView我们还可以设置view的缩放功能,将下面代码添加到中。这样我们就可以使用两根手指缩放view了。

  1. // Enable zooming
  2. [scrollView setMinimumZoomScale:0.5];
  3. [scrollView setMaximumZoomScale:5];
  4. [scrollView setDelegate:self];

小结:详解iOS开发之自定义View的内容介绍完了,简单的总结了一下自定义view的使用,希望本文对你有所帮助!本文为了方便友们更好的去学IOS开发中的View,提供代码下载,地址为:http://files.cnblogs.com/zhuqil/Hypnosister.zip 。

详解iOS开发之自定义View的更多相关文章

  1. iOS开发之自定义表情键盘(组件封装与自动布局)

    下面的东西是编写自定义的表情键盘,话不多说,开门见山吧!下面主要用到的知识有MVC, iOS开发中的自动布局,自定义组件的封装与使用,Block回调,CoreData的使用.有的小伙伴可能会问写一个自 ...

  2. 深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS

    深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS ios中并不是所有的对象都支持copy,mutableCopy,遵守NSCopying 协议的类可以发送copy消息,遵守NSMutab ...

  3. 超全详解Java开发环境搭建

    摘自:https://www.cnblogs.com/wangjiming/p/11278577.html 超全详解Java开发环境搭建   在项目产品开发中,开发环境搭建是软件开发的首要阶段,也是必 ...

  4. IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

    在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...

  5. 详解LUA开发工具及其环境配置

    LUA开发工具及其环境配置是本文要介绍的内容,主要是来了解并学习lua开发工具的使用和环境的配置,第一次接触LUA的话,就跟本人一起学习吧.看我能不能忽悠到你. LUA是语言,那么一定有编写的工具.第 ...

  6. [转]javascript console 函数详解 js开发调试的利器

    javascript console 函数详解 js开发调试的利器   分步阅读 Console 是用于显示 JS和 DOM 对象信息的单独窗口.并且向 JS 中注入1个 console 对象,使用该 ...

  7. Java-异常机制详解以及开发时异常设计的原则要求

    Java-异常机制详解以及开发时异常设计的原则要求 http://blog.csdn.net/Jack__Frost/article/details/52760930?locationNum=6

  8. 详解WebService开发中四个常见问题(2)

    详解WebService开发中四个常见问题(2)   WebService开发中经常会碰到诸如WebService与方法重载.循环引用.数据被穿该等等问题.本文会给大家一些很好的解决方法. AD:WO ...

  9. 详解WebService开发中四个常见问题(1)

    详解WebService开发中四个常见问题(1)   WebService开发中经常会碰到诸如WebService与方法重载.循环引用.数据被穿该等等问题.本文会给大家一些很好的解决方法. AD:WO ...

随机推荐

  1. ROS验证publisher和subscriber

    在前面的两篇博客中我们用C++在ROS中创建了一个发布者和接收者,并使用catkin_make构建了新的节点,下面就需要验证一下,我们写的是否正确. 首先运行roscore roscore 在使用ca ...

  2. poj 3228 Gold Transportation 二分+网络流

    题目链接 给出n个城市, 每个城市有一个仓库, 仓库有容量限制, 同时每个城市也有一些货物, 货物必须放到仓库中. 城市之间有路相连, 每条路有长度. 因为有些城市的货物量大于仓库的容量, 所以要运到 ...

  3. APM代码学习笔记3:执行过程

    以Linux平台ArduPlane为例 \ArduPlane\Plane.cpp 定义Plane类 继承自AP_HAL::HAL::Callbacks ,获取hal对象. \ArduPlane\Ard ...

  4. python基础学习笔记5--对象

    对象(object) 1.对象(object): 面向对象程序设计重要术语. 对象的特性:多态性.封装性.继承性 >>def add(x,y): return x+y 对于很多类型的参数都 ...

  5. 几篇SIEM文章

    http://infosecnirvana.com/tag/siem-rule-types/ http://www.tripwire.com/state-of-security/security-da ...

  6. Android Studio 中快速提取方法

    在开发过程中,有时在一个方法内部写了过多的代码,然后想要把一些代码提取出来封装下,分离开放在一个单独的方法里,可能你的做法是直接选中后Ctrl + 叉,或者 Ctrl + C,但在Android St ...

  7. date命令使用

    date命令的帮助信息 [root@localhost source]# date --help用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] ...

  8. Android企业级程序完全退出的解决方案【转】

    http://blog.csdn.net/wangjinyu501/article/details/8763552 问题描述 在平常开发的过程中可以发现,很多开发者对于程序的退出都没有去认真的解决.一 ...

  9. SqlCacheDependency的使用

    最近项目需要几秒就获取一次数据,查数据库服务器压力会很大,因此用缓存技术来缓解服务器压力. 使用SqlCacheDependency采用轮询的方式来获取缓存,SqlDependency查询通知的方式来 ...

  10. Android应用开发基础篇(9)-----SharedPreferences

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/27/2370319.html 一.概述 对于SharedPreferences,我吧它理解为一种 ...