Iphone-common-codes-ccteam source code CCUIView. m

Source: Internet
Author: User
The code is as follows: Copy code

 

//
// CCUIView. m
// CCFC
//
// Created by xichen on 11-12-17.
// Copyright 2011 ccteam. All rights reserved.
//

# Import "CCUIView. h"
# Import <QuartzCore/QuartzCore. h>

@ Interface UIView (ccPrivate)

-(Id) text;
-(Id) font;

@ End


@ Implementation UIView (cc)

@ Dynamic x, y, width, height;

-(CGFloat) x
{
Return self. frame. origin. x;
}

-(CGFloat) y
{
Return self. frame. origin. y;
}

-(CGFloat) width
{
Return self. frame. size. width;
}

-(CGFloat) height
{
Return self. frame. size. height;
}

-(Void) setX :( CGFloat) x
{
CGRect rect = self. frame;
If (x! = Rect. origin. x)
{
Rect. origin. x = x;
Self. frame = rect;
}
}

-(Void) setY :( CGFloat) y
{
CGRect rect = self. frame;
If (y! = Rect. origin. y)
{
Rect. origin. y = y;
Self. frame = rect;
}
}

-(Void) setWidth :( CGFloat) width
{
CGRect rect = self. frame;
If (width! = Rect. size. width)
{
Rect. size. width = width;
Self. frame = rect;
}
}

-(Void) setHeight :( CGFloat) height
{
CGRect rect = self. frame;
If (height! = Rect. size. height)
{
Rect. size. height = height;
Self. frame = rect;
}
}



// Create a UILabel and display the text in the center
+ (UILabel *) createLabel :( const CGRect *) rect withTitle :( NSString *) title
{
UILabel * label = [[UILabel alloc] initWithFrame:
CGRectMake (rect-> origin. x, rect-> origin. y, rect-> size. width, rect-> size. height)];

Label. text = title;
Label. textColor = [UIColor blackColor];
Label. backgroundColor = [UIColor whiteColor];
Label. textAlignment = UITextAlignmentCenter;

Return [label autorelease];
}

// Create UIButton and center the text
+ (UIButton *) createButton :( const CGRect *) rect withTitle :( NSString *) title
{
UIButton * btn = [UIButton alloc] initWithFrame:
CGRectMake (rect-> origin. x, rect-> origin. y, rect-> size. width, rect-> size. height)];

[Btn setTitle: title];
[Btn setTitleColor: [UIColor blackColor];
Btn. backgroundColor = [UIColor whiteColor];
Btn. titleLabel. textAlignment = UITextAlignmentCenter;

Return [btn autorelease];
}


// Remove all child views
-(Void) removeAllSubviews
{
For (UIView * temp in self. subviews)
[Temp removeFromSuperview];
}

// Hide all child views
-(Void) hideAllSubViews
{
For (UIView * temp in self. subviews)
Temp. hidden = TRUE;
}

// Create a view with the specified area size
+ (UIView *) createView :( CGRect) rect
{
UIView * view = UI_ALLOC_CREATE (UIView,
Rect. origin. x, rect. origin. y,
Rect. size. width, rect. size. height );
Return [view autorelease];
}

// Create a view of the same size as the specified view
+ (UIView *) createViewByView :( UIView *) view
{
UIView * temp = UI_ALLOC_CREATE (UIView,
View. frame. origin. x, view. frame. origin. y,
View. frame. size. width, view. frame. size. height );
Return [temp autorelease];
}

// Create a transparent view with the specified area size
+ (UIView *) createTransparentView :( CGRect) rect
{
UIView * view = [self createView: rect];
If (! View)
{
Return nil;
}
View. backgroundColor = [UIColor clearColor];

Return view;
}

// Create a transparent view of the same size as the specified view
+ (UIView *) createTransparentViewByView :( UIView *) view
{
UIView * temp = [self createViewByView: view];
If (! Temp)
{
Return nil;
}
Temp. backgroundColor = [UIColor clearColor];

Return temp;
}

// Display view
-(Void) showView
{
Self. hidden = FALSE;
}

// Hide view
-(Void) hideView
{
Self. hidden = TRUE;
}

// Obtain the coordinates of the position touched in the specified view
-(CGPoint) getTouchPoint :( UIEvent *) event
{
UITouch * touch = [[event allTouches] anyObject];
Return [touch locationInView: self];
}

// Obtain The View pointer array of the subview (including the subview) of the view that belongs to the specified type or its subclass view.
-(Void) getSubViewIsKindOf :( NSString *) viewStr array :( NSMutableArray *) outArray
{
If ([self isKindOfClass: NSClassFromString (viewStr)])
[OutArray addObject: self];
For (UIView * view in self. subviews)
{
[View getSubViewIsKindOf: viewStr array: outArray];
}
}

-(Void) getSubViewIsKindOf :( NSString *) viewStr
Array :( NSMutableArray *) outArray
MaxSize: (int) maxSize
{
If ([self isMemberOfClass: NSClassFromString (viewStr)])
[OutArray addObject: self];
If ([outArray count] = maxSize)
Return;
For (UIView * view in self. subviews)
{
[View getSubViewIsKindOf: viewStr array: outArray];
}
}

// Obtain The View pointer array of the subview (including the subview) of the view that belongs to the specified type or its subclass view.
-(Void) getSubViewIsMemberOf :( NSString *) viewStr array :( NSMutableArray *) outArray
{
If ([self isMemberOfClass: NSClassFromString (viewStr)])
[OutArray addObject: self];
For (UIView * view in self. subviews)
{
[View getSubViewIsMemberOf: viewStr array: outArray];
}
}

-(Void) getSubViewIsMemberOf :( NSString *) viewStr
Array :( NSMutableArray *) outArray
MaxSize: (int) maxSize
{
If ([self isMemberOfClass: NSClassFromString (viewStr)])
[OutArray addObject: self];
If ([outArray count] = maxSize)
Return;
For (UIView * view in self. subviews)
{
[View getSubViewIsMemberOf: viewStr array: outArray];
}
}


// Not OK
-(Void) commonFlip
{
[UIView beginAnimations: @ "ccAnimation" context: nil];
[UIView setAnimationDuration: 1.25];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: self cache: YES];
[UIView commitAnimations];
}

// Get the row count of a view's text // not OK
-(Int) getTextRows
{
If (! [Self respondsToSelector: @ selector (text)]
|! [Self respondsToSelector: @ selector (font)])
Return-1;

CGSize size = [[self text] sizeWithFont: [self font]
ConstrainedToSize: CGSizeMake ([self width], UINT_MAX)
LineBreakMode: UILineBreakModeWordWrap];
CGSize tempSize = [[self text] sizeWithFont: [self font];

Return (int) ceil (size. height/tempSize. height );

}

// Save the view to the photo gallery
-(Void) saveViewToPhotosAlbum
{
UIImage * viewImg = [self returnUIImageOfView];
UIImageWriteToSavedPhotosAlbum (viewImg, nil );
}

// Returns the UIImage of the view
-(UIImage *) returnUIImageOfView
{
UIGraphicsBeginImageContext (self. layer. bounds. size );
[Self. layer renderInContext: UIGraphicsGetCurrentContext ()];
UIImage * viewImg = UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
Return viewImg;
}

-(UITapGestureRecognizer *) addTapRecognizer :( id) target action :( SEL) sel
{
Self. userInteractionEnabled = YES;
UITapGestureRecognizer * tapRecog =
[[UITapGestureRecognizer alloc]
InitWithTarget: target action: sel];
If (tapRecog = nil)
Return nil;

[Self addGestureRecognizer: tapRecog];
[TapRecog release];

Return tapRecog;
}

// Set the view to rounded corner
-(Void) setRoundedCorner :( CGFloat) radius
{
Self. layer. cornerRadius = radius;
Self. layer. masksToBounds = YES;
}

// Set the view's borderColor and borderWidth
-(Void) setBorderColor :( UIColor *) color withBorderWidth :( CGFloat) width
{
Self. layer. borderColor = color. CGColor;
Self. layer. borderWidth = width;
}

+ (Void) enableViewBelow :( UIView *) foreView viewBelow :( UIView *) viewBelow
{
ForeView. exclusiveTouch = YES;
ForeView. userInteractionEnabled = NO;
ViewBelow. exclusiveTouch = NO;
ViewBelow. userInteractionEnabled = YES;
}

// Set the view to be center of another view
-(Void) setCenterOf :( UIView *) anotherView
{
Self. center = CGPointMake (anotherView. bounds. size. width/2,
AnotherView. bounds. size. height/2 );
}

// Move the view upwards
-(Void) moveUpwards :( CGFloat) offset
{
CGRect rect = self. frame;
Rect. origin. y-= offset;
Self. frame = rect;
}

// Move the view downwards
-(Void) moveDownwards :( CGFloat) offset
{
CGRect rect = self. frame;
Rect. origin. y + = offset;
Self. frame = rect;
}

// Move the view leftwards
-(Void) moveLeftwards :( CGFloat) offset
{
CGRect rect = self. frame;
Rect. origin. x-= offset;
Self. frame = rect;
}

// Move the view rightwards
-(Void) moveRightwards :( CGFloat) offset
{
CGRect rect = self. frame;
Rect. origin. x + = offset;
Self. frame = rect;
}

// Get the UIView that is at the index of subviews
-(UIView *) getSubviewByIndex :( int) index
{
Return [self. subviews objectAtIndex: index];
}

// Get the first subview
-(UIView *) getFirstSubview
{
If ([self. subviews count])
Return [self. subviews objectAtIndex: 0];
Return nil;
}

// Get the last subview
-(UIView *) getLastSubview
{
If ([self. subviews count])
Return [self. subviews objectAtIndex: [self. subviews count]-1];
Return nil;
}

// Add a layer by rect and color
-(CALayer *) addLayer :( CGRect) rect color :( UIColor *) color
{
CALayer * layer = [CALayer layer];
Layer. backgroundColor = color. CGColor;
Layer. frame = rect;
[Self. layer addSublayer: layer];
Return layer;
}

// Get the same level views arr that are behind self
-(NSArray *) getBackwardsViews
{
UIView * superView = self. superview;
NSArray * arr = superView. subviews;
NSMutableArray * retArr = [NSMutableArray array];

BOOL canAdd = FALSE;
For (UIView * view in arr)
{
If (canAdd)
[RetArr addObject: view];
If (self = view)
CanAdd = TRUE;
}
Return retArr;
}

// Get the same level views arr that are in front of self
-(NSArray *) getForewardsViews
{
UIView * superView = self. superview;
NSArray * arr = superView. subviews;
NSMutableArray * retArr = [NSMutableArray array];

BOOL canAdd = TRUE;
For (UIView * view in arr)
{
If (self = view)
CanAdd = FALSE;
If (canAdd)
[RetArr addObject: view];
}
Return retArr;
}

// Returns whether the touch is inside the view or not
-(BOOL) isTouchInsideView :( UITouch *) touch
{
Return [self pointInside: [touch locationInView: self] withEvent: nil];
}

// Returns whether the touch is outside the view or not
-(BOOL) isTouchOutsideView :( UITouch *) touch
{
Return! [Self pointInside: [touch locationInView: self] withEvent: nil];
}

// Returns whether the touch hit the view
-(BOOL) hitView :( UIView *) view touch :( UITouch *) touch
{
Return ([self hitTest: [touch locationInView: self] withEvent: nil] = view );
}

// Returns whether the touch hit the self
-(BOOL) hitSelf :( UITouch *) touch
{
Return ([self hitTest: [touch locationInView: self] withEvent: nil] = self );
}

// Returns whether the touch hit the view's subview
-(BOOL) hitSubviews :( UITouch *) touch
{
UIView * view = [self hitTest: [touch locationInView: self] withEvent: nil];
Return (view! = Nil & view! = Self );
}

// Remove subviews that is member of viewStr class
-(Void) removeSubViewIsMemberOf :( NSString *) viewStr
{
For (UIView * view in self. subviews)
{
If ([view isMemberOfClass: NSClassFromString (viewStr)])
[View removeFromSuperview];
}
}

// Remove subviews that is kind of viewStr class
-(Void) removeSubViewIsKindOf :( NSString *) viewStr
{
For (UIView * view in self. subviews)
{
If ([view isKindOfClass: NSClassFromString (viewStr)])
[View removeFromSuperview];
}
}

// Recursively remove subviews that is member of viewStr class
-(Void) removeRecursiveSubViewIsMemberOf :( NSString *) viewStr
{
For (UIView * view in self. subviews)
{
If ([view isMemberOfClass: NSClassFromString (viewStr)])
{
[View removeFromSuperview];
Continue;
}
[View removeRecursiveSubViewIsMemberOf: viewStr];
}
}

// Recursively remove subviews that is kind of viewStr class
-(Void) removeRecursiveSubViewIsKindOf :( NSString *) viewStr
{
For (UIView * view in self. subviews)
{
If ([view isKindOfClass: NSClassFromString (viewStr)])
{
[View removeFromSuperview];
Continue;
}
[View removeRecursiveSubViewIsKindOf: viewStr];
}
}

// Set the view that it will show the view outside the frame or not
-(Void) showOutsideFrameView
{
Self. clipsToBounds = NO;
}

-(Void) hideOutsideFrameView
{
Self. clipsToBounds = YES;
}


@ End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.