首页 > 其他分享 >ios CCUIView.m

ios CCUIView.m

时间:2024-07-31 19:24:45浏览次数:11  
标签:self view ios frame CCUIView void rect UIView

//
//  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

相关文章

  • ios CCNSURL.m
    ////CCNSURL.h//CCFC////#import<Foundation/Foundation.h>@interfaceNSURL(cc)+(NSString*)telephonePrefix;+(NSString*)smsPrefix;+(NSString*)mailPrefix;+(NSString*)googleMapPrefix;@end////CCNSURL.m//CCFC////......
  • ios CCUIToolBar.m
    ////CCUIToolbar.h//CCFC////#import<Foundation/Foundation.h>#import<UIKit/UIToolbar.h>@interfaceUIToolbar(cc)//createacommontoolbar+(UIToolbar*)createCommonToolbar:(CGRect)rectitems:(NSArray*)buttonItemArr;@end /......
  • 建议所有iPhone升级!苹果iOS 17.6正式版发布:重要错误修复
    苹果发布iOS17.6正式版升级,本次更新距离iOS17.5发布已有两个月时间。升级日志显示,iOS17.6提供重要的错误修复和安全性更新,苹果建议所有用户安装。不出意外的话,iOS17.6将是iOS17最后几次更新之一,后续将仅提供必须的安全更新和Bug修复,苹果的开发重心会转移到iOS18上。根据......
  • vue基础知识总结(2)--- axios的使用
    一.下载Vue3:选择自己想要下载的项目文件夹,cmd回车打开命令栏,执行:cnpminitvue@latest然后等待一会就可以创建一个项目,并更改项目名:√请输入项目名称:...vue-project之后按照提示输入对应的语句:cdvue-projectcnpminstall我们等待几秒Vue3项目就成功创建出来了......
  • 如何让 flet 在 iOS 上打开照片而不是文件?
    我正在尝试使用Flet构建一个Python移动应用程序,我需要它做的事情之一就是接受用户的照片或视频。使用flet文档,我尝试了以下代码:ft.ElevatedButton("Choosefiles...",on_click=lambda_:file_picker.pick_files(allow_multiple=True))在iOS上打开文件选择器......
  • 【iOS】——KVC和KVO
    KVCKVC定义KVC(Key-valuecoding)键值编码,允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值。而不需要调用明确的存取方法。这样就可以在运行时动态地访问和修改对象的属性。而不是在编译时确定。也就是说它提供一种机制来间接访问对象的属性,而不是通过调用Sett......
  • BUG 太多?苹果罕见“重新推送”ios18 beta 4测试版
    在刚刚过去的周末,苹果公司面向开发者,重新发布了iOS/ iPadOS18Beta4更新,内部版本号从22A5316j 变为 22A5316k,目前尚不清楚两个Beta4版本更新之间的区别。此次更新包大小仅为251M左右,是更新。对于已经注册的开发者可以打开“设置”应用程序,进入“软件更新”部......
  • 使用带有 pythonKit XCODE 的嵌入式 Python,在 iOS 应用程序中与 OpenCV-python 签名不
    我根据Beewares使用指南在XCODE中将Python嵌入到我的iOS项目中https://github.com/beeware/Python-Apple-support/blob/main/USAGE.md运行时,我得到pythonKit找不到由ultralytics导入的cv2错误。当我将OpenCV-python添加到我的app_packages文件夹时......
  • Vue axios 请求方式汇总
    1.发送form-data形式letformData=newFormData();formData.append('username',data.username);//添加键值对formData.append('password',data.password);//添加键值对axios.post('http:/xxx/cms-admin/user/login',formData).then(response=&......
  • axios中的那些天才代码!看完我实力大涨!
    ......