//
// CCUIView.h
// CCFC
//
//
#import <Foundation/Foundation.h>
#import "CCUIButton.h"
//创建UI控件的宏
#define UI_ALLOC_CREATE(UIctlName, x, y, width, height) [[UIctlName alloc] initWithFrame:CGRectMake((x), (y), (width), (height))]
#define UI_ALLOC_CREATE_EX(UIctlName, rect) [[UIctlName alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)]
#define UI_RELEASE(UIctlName) [UIctlName release]
//获取View的x、y、宽度、高度
#define GET_VIEW_X(ctlName) ((ctlName).frame.origin.x)
#define GET_VIEW_Y(ctlName) ((ctlName).frame.origin.y)
#define GET_VIEW_WIDTH(ctlName) ((ctlName).frame.size.width)
#define GET_VIEW_HEIGHT(ctlName) ((ctlName).frame.size.height)
//设置View的x、y、宽度、高度
#define SET_VIEW_X(ctlName, newX) \
{ \
CGRect rect = ctlName.frame; \
rect.origin.x = (newX); \
ctlName.frame = rect; \
}
#define SET_VIEW_Y(ctlName, newY) \
{ \
CGRect rect = ctlName.frame; \
rect.origin.y = (newY); \
ctlName.frame = rect; \
}
#define SET_VIEW_WIDTH(ctlName, newWidth) \
{ \
CGRect rect = ctlName.frame; \
rect.size.width = (newWidth); \
ctlName.frame = rect; \
}
#define SET_VIEW_HEIGHT(ctlName, newHeight) \
{ \
CGRect rect = ctlName.frame; \
rect.size.height = (newHeight); \
ctlName.frame = rect; \
}
#define SET_VIEW_FRAME(ctlName, x, y, width, height) \
{ \
CGRect rect = CGRectMake(x, y, width, height); \
ctlName.frame = rect; \
}
//刷新界面
#define UPDATE_VIEW(view) [view setNeedsDisplay]
@interface UIView(cc)
@property(nonatomic, assign) CGFloat x;
@property(nonatomic, assign) CGFloat y;
@property(nonatomic, assign) CGFloat width;
@property(nonatomic, assign) CGFloat height;
// 创建UILabel, 文本居中显示
+ (UILabel *)createLabel:(const CGRect *)rect withTitle:(NSString *)title;
// 创建UIButton, 文本居中显示
+ (UIButton *)createButton:(const CGRect *)rect withTitle:(NSString *)title;
// 移除所有子view
- (void)removeAllSubviews;
// 隐藏所有子view
- (void)hideAllSubViews;
// 创建一个指定区域大小的view
+ (UIView *)createView:(CGRect)rect;
// 创建一个和指定view相同大小的view
+ (UIView *)createViewByView:(UIView *)view;
// 创建一个指定区域大小的透明view
+ (UIView *)createTransparentView:(CGRect)rect;
// 创建一个和指定view相同大小的透明view
+ (UIView *)createTransparentViewByView:(UIView *)view;
// 显示view
- (void)showView;
// 隐藏view
- (void)hideView;
//获取在指定视图中触摸的位置坐标
- (CGPoint)getTouchPoint:(UIEvent *)event;
// 获取视图的子视图(包括子视图的子视图)中属于指定类型或其子类视图的视图指针数组
- (void)getSubViewIsKindOf:(NSString *)viewStr array:(NSMutableArray *)outArray;
- (void)getSubViewIsKindOf:(NSString *)viewStr
array:(NSMutableArray *)outArray
maxSize:(int)maxSize;
// 获取视图的子视图(包括子视图的子视图)中属于指定类型或其子类视图的视图指针数组
- (void)getSubViewIsMemberOf:(NSString *)viewStr array:(NSMutableArray *)outArray;
- (void)getSubViewIsMemberOf:(NSString *)viewStr
array:(NSMutableArray *)outArray
maxSize:(int)maxSize;
// not ok
- (void)commonFlip;
// get the row count of a view's text // not ok
- (int)getTextRows;
// 将view视图保存到照片库中
- (void)saveViewToPhotosAlbum;
// returns the UIImage of the view
- (UIImage *)returnUIImageOfView;
// add tap action response on a view
- (UITapGestureRecognizer *)addTapRecognizer:(id)target action:(SEL)sel;
// set the view to rounded corner
- (void)setRoundedCorner:(CGFloat)radius;
// set the view's borderColor and borderWidth
- (void)setBorderColor:(UIColor *)color withBorderWidth:(CGFloat)width;
+ (void)enableViewBelow:(UIView *)foreView viewBelow:(UIView *)viewBelow;
// set the view to be center of another view
- (void)setCenterOf:(UIView *)anotherView;
// move the view upwards
- (void)moveUpwards:(CGFloat)offset;
// move the view downwards
- (void)moveDownwards:(CGFloat)offset;
// move the view leftwards
- (void)moveLeftwards:(CGFloat)offset;
// move the view rightwards
- (void)moveRightwards:(CGFloat)offset;
// get the UIView that is at the index of subviews
- (UIView *)getSubviewByIndex:(int)index;
// get the first subview
- (UIView *)getFirstSubview;
// get the last subview
- (UIView *)getLastSubview;
// add a layer by rect and color
- (CALayer *)addLayer:(CGRect)rect color:(UIColor *)color;
// get the same level views arr that are behind self
- (NSArray *)getBackwardsViews;
// get the same level views arr that are in front of self
- (NSArray *)getForewardsViews;
// returns whether the touch is inside the view or not
- (BOOL)isTouchInsideView:(UITouch *)touch;
// returns whether the touch is outside the view or not
- (BOOL)isTouchOutsideView:(UITouch *)touch;
// returns whether the touch hit the view
- (BOOL)hitView:(UIView *)view touch:(UITouch *)touch;
// returns whether the touch hit the self
- (BOOL)hitSelf:(UITouch *)touch;
// returns whether the touch hit the view's subview
- (BOOL)hitSubviews:(UITouch *)touch;
// remove subviews that is member of viewStr class
- (void)removeSubViewIsMemberOf:(NSString *)viewStr;
// remove subviews that is kind of viewStr class
- (void)removeSubViewIsKindOf:(NSString *)viewStr;
// recursively remove subviews that is member of viewStr class
- (void)removeRecursiveSubViewIsMemberOf:(NSString *)viewStr;
// recursively remove subviews that is kind of viewStr class
- (void)removeRecursiveSubViewIsKindOf:(NSString *)viewStr;
// set the view that it will show the view outside the frame or not
- (void)showOutsideFrameView;
- (void)hideOutsideFrameView;
#if CC_ENABLE_PRIVATE_API && CC_COMPILE_PRIVATE_CLASS
- (id)scriptingInfoWithChildren; // the struct info of the view's children
- (id)recursiveDescription; // the detail description of the view's recursive views
#endif
@end
//
// CCUIView.m
// CCFC
//
//
#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;
}
}
// 创建UILabel, 文本居中显示
+ (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];
}
// 创建UIButton, 文本居中显示
+ (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];
}
// 移除所有子view
- (void)removeAllSubviews
{
for(UIView *temp in self.subviews)
[temp removeFromSuperview];
}
// 隐藏所有子view
- (void)hideAllSubViews
{
for(UIView *temp in self.subviews)
temp.hidden = TRUE;
}
// 创建一个指定区域大小的view
+ (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];
}
// 创建一个和指定view相同大小的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];
}
// 创建一个指定区域大小的透明view
+ (UIView *)createTransparentView:(CGRect)rect
{
UIView *view = [self createView:rect];
if(!view)
{
return nil;
}
view.backgroundColor = [UIColor clearColor];
return view;
}
// 创建一个和指定view相同大小的透明view
+ (UIView *)createTransparentViewByView:(UIView *)view
{
UIView *temp = [self createViewByView:view];
if(!temp)
{
return nil;
}
temp.backgroundColor = [UIColor clearColor];
return temp;
}
// 显示view
- (void)showView
{
self.hidden = FALSE;
}
// 隐藏view
- (void)hideView
{
self.hidden = TRUE;
}
// 获取在指定视图中触摸的位置坐标
- (CGPoint)getTouchPoint:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
return [touch locationInView:self];
}
// 获取视图的子视图(包括子视图的子视图)中属于指定类型或其子类视图的视图指针数组
- (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];
}
}
// 获取视图的子视图(包括子视图的子视图)中属于指定类型或其子类视图的视图指针数组
- (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);
}
// 将view视图保存到照片库中
- (void)saveViewToPhotosAlbum
{
UIImage *viewImg = [self returnUIImageOfView];
UIImageWriteToSavedPhotosAlbum(viewImg, nil, nil, 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
微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。
我是程序员小迷(致力于C、C++、Java、Kotlin、Android、iOS、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。
欢迎关注。助您在编程路上越走越好!
标签:self,view,ios,frame,CCUIView,void,rect,UIView From: https://blog.csdn.net/cxsjabcabc/article/details/7202580