一个很方便控制视图控件Z轴层次的UIView类别??Category)

日期:2016/6/24 16:43:00 来源:本网整理 阅读:0
下面这个类使用UIView类别Category封装了对其子视图层次管理,提供了将控件移到最前面或者最后面,也可以随意调整一个控件的z轴方向的位置??
http://www.ctolive.com/space-1023-do-blog-id-2102.html

[1].[代码] 头文?? 跳至 [1] [2]

#import <Foundation/Foundation.h>

@interface UIView (Hierarchy)

-(int)getSubviewIndex;

-(void)bringToFront;
-(void)sendToBack;

-(void)bringOneLevelUp;
-(void)sendOneLevelDown;

-(BOOL)isInFront;
-(BOOL)isAtBack;

-(void)swapDepthsWithView:(UIView*)swapView;

@end

[2].[代码] 实现文件 跳至 [1] [2]

#import "UIView+Hierarchy.h"

@implementation UIView(Hierarchy)

-(int)getSubviewIndex
{
    return [self.superview.subviews indexOfObject:self];
}

-(void)bringToFront
{
    [self.superview bringSubviewToFront:self];
}

-(void)sendToBack
{
    [self.superview sendSubviewToBack:self];
}

-(void)bringOneLevelUp
{
    int currentIndex = [self getSubviewIndex];
    [self.superview exchangeSubviewAtIndex:currentIndex withSubviewAtIndex:currentIndex+1];
}

-(void)sendOneLevelDown
{
    int currentIndex = [self getSubviewIndex];
    [self.superview exchangeSubviewAtIndex:currentIndex withSubviewAtIndex:currentIndex-1];
}

-(BOOL)isInFront
{
    return ([self.superview.subviews lastObject]==self);
}

-(BOOL)isAtBack
{
    return ([self.superview.subviews objectAtIndex:0]==self);
}

-(void)swapDepthsWithView:(UIView*)swapView
{
    [self.superview exchangeSubviewAtIndex:[self getSubviewIndex] withSubviewAtIndex:[swapView getSubviewIndex]];
}

@end

  • 本文相关:
  • UIView背景的渐??/a>[代码]
    鉴客
  • iOS设备多点触控和屏幕坏点检测程序代??/a>[代码]
    鉴客
  • tableview滚动到最后一行自动加载数??
  • iPhone toolchain 4.0 的两个配套示例代??/a>[代码]
    鉴客
  • IOS 使用 HTTP POST 上传图片和文本信??/a>[代码]
    红薯
  • UITextField限制字数的方??
  • NSMutableURLRequest,在POST方式下传递参??/a>[代码]
    鉴客
  • iPhone开??download 下载文件
  • iPhone开??加载本地pdf到webview
  • iPhone开??文件的增加删除查??/a>[代码]
    鉴客
  • 免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2015 www.zgxue.com All Rights Reserved