虽然apple在IOS框架中提供了不少能够直接使用的UI控件,可是在实际开发当中咱们一般都是要本身去定制UIView的外观和行为。因此建立UIView的子类是必需的。app
刚开始接触IOS的开发,先从简单的作起。自定义的UI类,都要继承UIView类而后实现或覆盖其中的方法。我这里把这个类命名为HypnosisterView:框架
1 #import <Foundation/Foundation.h> 2 3 @interface HypnosisterView : UIView 4 5 @property (nonatomic,strong) UIColor *circleColor ; 6 7 //overide methods 8 - (void)drawRect:(CGRect)dirtyRect; 9 - (id)initWithFrame:(CGRect)frame; 10 @end
简单地覆盖了UIview的两个方法:drawRect和initWithFrameide
- (void)drawRect:(CGRect)dirtyRect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect bounds = [self bounds]; CGPoint center ; center.x = bounds.origin.x + bounds.size.width/2.0 ; center.y = bounds.origin.y + bounds.size.height/2.0 ; float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0 ; CGContextSetLineWidth(ctx,10); [[self circleColor] setStroke] ; for(float currentRadius=maxRadius ; currentRadius >0.0 ; currentRadius -= 20.0) { CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI*2.0, YES); CGContextStrokePath(ctx); } //draw some text NSString *text = @"You are getting sleepy." ; UIFont *font = [UIFont boldSystemFontOfSize:28]; 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 ; [[UIColor blackColor] setFill]; //add show shadow to the text CGSize offset = CGSizeMake(4, 3); CGColorRef color = [[UIColor darkGrayColor] CGColor]; CGContextSetShadowWithColor(ctx, offset, 2.0, color); //draw rect [text drawInRect:textRect withFont:font]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self != nil) { [self setBackgroundColor:[UIColor clearColor]] ; [self setCircleColor:[UIColor lightGrayColor]] ; } return self ; }
在屏幕上这个view的样子。学习
几点要注意:atom
1,UIView不会本身自动重绘,要对其发送setNeedsDisplay消息进行重绘。spa
2,关于firstResponder默认是不能称为FirstResponder的, 要覆盖其- (BOOL)canBecomeFirstResponder来修改这一默认属性。code
3,对view发送becomeFirstResponser消息让其成为当前对象的FirstResponder。对象
4,一个成为FirstResponder之后能够对运动事件响应,不过要实现其中的一些方法,好比:blog
1 //手机摇动事件 2 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 3 { 4 if(motion == UIEventSubtypeMotionShake) 5 { 6 [self setCircleColor:[UIColor redColor]]; 7 } 8 }
UIScrollView:继承
UIScrollView是一个很是有用的控件,将一个UIView加到上面设置,设置必定的参数就能够实现拖放和分页。并且UIScollView上面支持多于一个View的显示。
1,利用scrollview进行分页:在一个window上面放置几个不一样的页面(一次只显示一个),经过的scrollview的滚动来实现分页的效果。其中有一个属性可使scrollview自动对其边界:[scrollView setPagingEnabled:YES];
举个例子来讲明一下:在一个桌子上面并排放着不少扑克牌,桌子上面有一个窗口和扑克牌一样大小。那么窗口一次只可以显示一张扑克牌,每次经过左右移动就能够看到不一样的扑克牌,实现了一种分页效果。在这里桌子和窗口就是UIScrollView而扑克牌就是页面,窗口其实也能够认为是用户看到的屏幕。
2,scrollview能够对特定的view进行放大和缩小操做,可是只支持一个在其上面的一个view的缩放操做:实现这个缩放要先设定缩放的最小值和最大值,还要实现UIScollViewDelegate协议中的指定方法。
CGRect windowFrame = [[self window] bounds]; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:windowFrame]; CGRect scrollContentFrame = windowFrame ; [scrollView setContentSize:scrollContentFrame.size]; [scrollView setMinimumZoomScale:1.0]; [scrollView setMaximumZoomScale:5.0]; [scrollView setDelegate:self]; [[self window] addSubview:scrollView];
实现协议:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return view ; }
最后一中隐藏Status bar的方法:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
学习笔记,仅供参考,欢迎交流。