Although apple in IOS framework provides a lot of UI controls can be used directly, but in the actual development of which we are usually to themselves to customize the appearance and behavior of UIView. So create a UIView subclass is required.
just getting started with IOS development, start with a simple start. Custom UI classes, must inherit or override UIView class and then implement them approach. I am here to put this class is named 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
simply covering UIview two methods: drawRect and initWithFrame
- (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 ;
}
this view on the screen looks like.
points to note:
1, UIView does not automatically redraw themselves, the message you want to send setNeedsDisplay be redrawn.
2, the default is to not be called on firstResponder FirstResponder, to cover it - (BOOL) canBecomeFirstResponder to modify the default properties.
3, right view messages sent becomeFirstResponser allowed to become the current object FirstResponder.
4, one can become FirstResponder after exercise incident response, but some of the ways to achieve them, such as:
1 //手机摇动事件
2 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
3 {
4 if(motion == UIEventSubtypeMotionShake)
5 {
6 [self setCircleColor:[UIColor redColor]];
7 }
8 }
UIScrollView:
UIScrollView is a very useful control to a UIView added to the above settings, you can set certain parameters to achieve drag and paging. And UIScollView above support more than one View displays.
1, the use of paging scrollview: placed in a window at several different pages (display only one), through the scrollview Scroll to implement paging effect. Which has an attribute allows automatic scrollview its frontiers: [scrollView setPagingEnabled: YES];
give an example to explain: side by side at a table on which a lot of cards, there is a window above the table and playing cards the same size . Then one can only display a window playing cards, each pass and move around, you can see the different cards, implements a paging effect. Here is a table and window UIScrollView while poker is the page, the window that the user can actually see the screen.
2, scrollview particular view can zoom in and out operations, but supports only one on which a view of the scaling operation: Zoom Zoom achieve this first set the minimum and maximum values, but also to achieve UIScollViewDelegate protocol specified method.
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];
achieve agreement:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return view ;
}
Finally a way to hide the Status bar:
[[UIApplication sharedApplication] setStatusBarHidden: YES withAnimation: UIStatusBarAnimationFade];
study notes, for reference only, please share.
没有评论:
发表评论