转自:http://www.cnblogs.com/nightwolf/p/3222597.html

  虽然apple在IOS框架中提供了很多可以直接使用的UI控件,但是在实际开发当中我们通常都是要自己去定制UIView的外观和行为。所以创建UIView的子类是必需的。

  刚开始接触IOS的开发,先从简单的做起。自定义的UI类,都要继承UIView类然后实现或覆盖其中的方法。我这里把这个类命名为HypnosisterView:

  1. 1 #import <Foundation/Foundation.h>
  2. 2
  3. 3 @interface HypnosisterView : UIView
  4. 4
  5. 5 @property (nonatomic,strong) UIColor *circleColor ;
  6. 6
  7. 7 //overide methods
  8. 8 - (void)drawRect:(CGRect)dirtyRect;
  9. 9 - (id)initWithFrame:(CGRect)frame;
  10. 10 @end

  简单地覆盖了UIview的两个方法:drawRect和initWithFrame

  1. - (void)drawRect:(CGRect)dirtyRect
  2. {
  3. CGContextRef ctx = UIGraphicsGetCurrentContext();
  4. CGRect bounds = [self bounds];
  5.  
  6. CGPoint center ;
  7. center.x = bounds.origin.x + bounds.size.width/2.0 ;
  8. center.y = bounds.origin.y + bounds.size.height/2.0 ;
  9.  
  10. float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0 ;
  11.  
  12. CGContextSetLineWidth(ctx,10);
  13.  
  14. [[self circleColor] setStroke] ;
  15.  
  16. for(float currentRadius=maxRadius ; currentRadius >0.0 ; currentRadius -= 20.0)
  17. {
  18. CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI*2.0, YES);
  19. CGContextStrokePath(ctx);
  20. }
  21.  
  22. //draw some text
  23. NSString *text = @"You are getting sleepy." ;
  24.  
  25. UIFont *font = [UIFont boldSystemFontOfSize:28];
  26.  
  27. CGRect textRect ;
  28.  
  29. textRect.size = [text sizeWithFont:font] ;
  30.  
  31. textRect.origin.x = center.x - textRect.size.width / 2.0 ;
  32. textRect.origin.y = center.y - textRect.size.height / 2.0 ;
  33.  
  34. [[UIColor blackColor] setFill];
  35.  
  36. //add show shadow to the text
  37. CGSize offset = CGSizeMake(4, 3);
  38.  
  39. CGColorRef color = [[UIColor darkGrayColor] CGColor];
  40.  
  41. CGContextSetShadowWithColor(ctx, offset, 2.0, color);
  42.  
  43. //draw rect
  44. [text drawInRect:textRect withFont:font];
  45.  
  46. }
  47.  
  48. - (id)initWithFrame:(CGRect)frame
  49. {
  50. self = [super initWithFrame:frame];
  51.  
  52. if(self != nil)
  53. {
  54. [self setBackgroundColor:[UIColor clearColor]] ;
  55. [self setCircleColor:[UIColor lightGrayColor]] ;
  56. }
  57.  
  58. return self ;
  59. }

  在屏幕上这个view的样子。

几点要注意:

1,UIView不会自己自动重绘,要对其发送setNeedsDisplay消息进行重绘。

2,关于firstResponder默认是不能称为FirstResponder的, 要覆盖其- (BOOL)canBecomeFirstResponder来修改这一默认属性。

3,对view发送becomeFirstResponser消息让其成为当前对象的FirstResponder。

4,一个成为FirstResponder以后可以对运动事件响应,不过要实现其中的一些方法,比如:

  1. 1 //手机摇动事件
  2. 2 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
  3. 3 {
  4. 4 if(motion == UIEventSubtypeMotionShake)
  5. 5 {
  6. 6 [self setCircleColor:[UIColor redColor]];
  7. 7 }
  8. 8 }

UIScrollView:

  UIScrollView是一个非常有用的控件,将一个UIView加到上面设置,设置一定的参数就可以实现拖放和分页。而且UIScollView上面支持多于一个View的显示。

1,利用scrollview进行分页:在一个window上面放置几个不同的页面(一次只显示一个),通过的scrollview的滚动来实现分页的效果。其中有一个属性可以使scrollview自动对其边界:[scrollView setPagingEnabled:YES];

举个例子来说明一下:在一个桌子上面并排放着很多扑克牌,桌子上面有一个窗口和扑克牌同样大小。那么窗口一次只能够显示一张扑克牌,每次通过左右移动就可以看到不同的扑克牌,实现了一种分页效果。在这里桌子和窗口就是UIScrollView而扑克牌就是页面,窗口其实也可以认为是用户看到的屏幕。

2,scrollview可以对特定的view进行放大和缩小操作,但是只支持一个在其上面的一个view的缩放操作:实现这个缩放要先设定缩放的最小值和最大值,还要实现UIScollViewDelegate协议中的指定方法。

  1. CGRect windowFrame = [[self window] bounds];
  2. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:windowFrame];
  3. CGRect scrollContentFrame = windowFrame ;
  4. [scrollView setContentSize:scrollContentFrame.size];
  5. [scrollView setMinimumZoomScale:1.0];
  6. [scrollView setMaximumZoomScale:5.0];
  7. [scrollView setDelegate:self];
  8. [[self window] addSubview:scrollView];

实现协议:

  1. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  2. {
  3. return view ;
  4. }

最后一中隐藏Status bar的方法:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

IOS7之后隐藏,参见:

http://www.cnblogs.com/wangpei/p/3550393.html

学习笔记,仅供参考,欢迎交流。

IOS UIView子类UIScrollView的更多相关文章

  1. IOS UIView 04- 自定义控件

    注:本人是翻译过来,并且加上本人的一点见解. 前言 本文将讨论一些自定义视图.控件的诀窍和技巧.我们先概述一下 UIKit 向我们提供的控件,并介绍一些渲染技巧.随后我们会深入到视图和其所有者之间的通 ...

  2. iOS UIView Class Translation

    类 UIView 一个管理屏幕上矩形区域内容的对象.   概述 Views 是你应用的用户界面最基础的组成部分.UIView类定义了对于所有 views 的共有的行为.一个 view 对象在它的边界矩 ...

  3. iOS开发基础-UIScrollView实现图片缩放

    当用户在 UIScrollView 上使用捏合手势时, UIScrollView 会给 UIScrollViewDelegate 协议发送一条消息,并调用代理的 viewForZoomingInScr ...

  4. IOS 怎么用UIScrollView来滚动和缩放他的内容第一篇

    本篇文章来自于互联网资料翻译 UIScrollView是在IOS最有用的控件之一.他是一个来展现超过一个屏幕的内容的很好的方式.下面有很多的技巧来使用他. 这篇文章就是关于UIScrollView的, ...

  5. 【iOS系列】-UIScrollView的介绍及结合UIPageControl实现图片播放的实例

    [iOS系列]-UIScrollView的介绍及结合UIPageControl实现图片播放的实例 第一:UIScrollView的常用属性 //表示UIScrollView内容的尺寸,滚动范围 @pr ...

  6. IOS开发之UIScrollView约束布局

    概要 在iOS开发学习中,UIScrollView是绕不过去的一个重要控件. 但是相对于Android的ScrollView,iOS的这个滚动控件的用法简直是复杂一万倍... 最主要是目前能找到的大部 ...

  7. IOS UIVIEW layer动画 总结(转)

    转发自:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/16306_12.html   IOS UIVIEW layer动画 总结, ...

  8. IOS UIView圆角,阴影,边框,渐增光泽

    圆角 sampleView.layer.cornerRadius = 2.5; // 圓角的弧度sampleView.layer.masksToBounds = YES; 阴影 sampleView. ...

  9. [转]IOS UIView 之属性篇

    [转载自:IOS UIView 之属性篇 From CSDN] UIView 继承于UIResponder             所遵守的协议有 NSCoding .UIAppearance. UI ...

随机推荐

  1. Java实现中文字符串的排序功能

    package test; /** * * @Title 书的信息类 * @author LR * @version 1.0 * @since 2016-04-21 */ public class B ...

  2. cannot restore segment prot after reloc: Permission denied

    编辑/etc/selinux/config,找到这段:# This file controls the state of SELinux on the system. # SELINUX= can t ...

  3. Android问题-DelphiXE5开发Andriod连接Webservice乱码问题

    问题现象:在使用DelphiXE5开发Andriod连接Webservice乱码. 问题原因:数据类型不同. 问题处理:为了不让广大朋友再烦恼,我就把解决办法写出来吧,把数据库中我们要查询的字段类型改 ...

  4. HD1085 Holding Bin-Laden Captive!

    Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...

  5. PCB中层的定义(一)

  6. URAL 2068 Game of Nuts (博弈)

    题意:给定 n 堆石子,每次一个人把它们分成三堆都是奇数的,谁先不能分,谁输. 析:因为每堆都是奇数,那么最后肯定都是要分成1的,那么就把不是1的全加和,然后判断奇偶就OK了. 代码如下: #prag ...

  7. Linux内核完全注释之编程语言和环境(一)

    as86汇编器 1.来源与对于linux的用途 as86来源minix-386开发的intel 8086.80386汇编编译程序和链接程序,他主要为linux创建16位的启动引导扇区程序boot/bo ...

  8. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...

  9. git/github 使用

    原文:http://www.cnblogs.com/fnng/archive/2011/08/25/2153807.html git/github学习笔记 Posted on 2011-08-25 2 ...

  10. Visual Studio 2012 主题下的代码配色方案

    默认的VS2012的深色配色方案个人感觉很丑,不是很好看,于是就自己动手配置了一下,突出语法高亮显示,增加代码语法识别度,个人感觉还是可以的. 原来使用的是VAX,但自从VAX导致的我的VS不能输入中 ...