您的位置:首页 > 产品设计 > UI/UE

【UIKit-124-1】#import <UIKit/UIView.h>

2015-09-21 23:49 399 查看
UIView -基本属性与方法】

@interface UIView :UIResponder <NSCoding,UIAppearance,UIAppearanceContainer,UIDynamicItem,UITraitEnvironment,UICoordinateSpace>

+ (Class)layerClass; // CALayer类,创建试图基本层。
- (instancetype)initWithFrame:(CGRect)frame; //默认的初始化方式

@property(nonatomic,getter=isUserInteractionEnabled)BOOL userInteractionEnabled; //默认开启用户交互,(imageView默认关)
@property(nonatomic) NSInteger tag;
// 标签值,默认0

@property(nonatomic,readonly,retain) CALayer *layer;
// 可以设置圆角,阴影等,详情见CALayer

@end

UIView -几何相关】

@interface UIView(UIViewGeometry)

@property(nonatomic)CGRect frame;//视图位置以及大小(屏幕左上角为【0.0】)(*视图变换之后,frame不会反映视图的真实位置)

@property(nonatomic)CGRect bounds; //视图位置以及大小(自身左上角为【0.0】)

@property(nonatomic)CGPoint center; //视图中心

@property(nonatomic)CGAffineTransform transform; //默认是CGAffineTransformIdentity(位置初始化)

每次变换前都要置位,不然你变换用的坐标系统不是屏幕坐标系统(即绝对坐标系统),而是上一次变换后的坐标系统,具体使用见CGAffineTransform使用
@property(nonatomic)CGFloat contentScaleFactor;//老版适配,然并软(待研究,求留言指出)

@property(nonatomic,getter=isMultipleTouchEnabled)BOOL
multipleTouchEnabled; // 多点触控,默认否
@property(nonatomic,getter=isExclusiveTouch)BOOL
exclusiveTouch; // 响应独占式,默认否。(即控件响应时,排除其他任何响应)可以研究UITouch

【判断触摸点是否在当前视图】

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; //判断触摸点是否在当前视图(从最上方开始沿着响应链逐级调用)。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; //

1首先调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内;

2若返回NO,则hitTest:withEvent:返回nil;

3若返回YES,则向当前视图的所有子视图(subviews)发送hitTest:withEvent:消息,所有子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历,直到有子视图返回非空对象或者全部子视图遍历完毕;

4若第一次有子视图返回非空对象,则hitTest:withEvent:方法返回此对象,处理结束;

5如所有子视图都返回非,则hitTest:withEvent:方法返回自身(self)。

6hitTest:withEvent:方法忽略隐藏(hidden=YES)的视图,禁止用户操作(userInteractionEnabled=YES)的视图,以及alpha级别小于0.01(alpha<0.01)的视图。如果一个子视图的区域超过父视图的bound区域(父视图的clipsToBounds属性为NO,这样超过父视图bound区域的子视图内容也会显示),那么正常情况下对子视图在父视图之外区域的触摸操作不会被识别,因为父视图的pointInside:withEvent:方法会返回NO,这样就不会继续向下遍历子视图了。当然,也可以重写pointInside:withEvent:方法来处理这种情况。

详情请看 =》 /article/5577717.html

【像素点、范围2个视图中切换】

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;

// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;

// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;

// 将rect从view中转换到当前视图中,返回在当前视图中的rect

- (void)viewDidLoad {
[super viewDidLoad];

redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

greenView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];

blueView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
blueView.backgroundColor = [UIColor blueColor];
[redView addSubview:blueView];

}

//  toView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint newPoint = [blueView convertPoint:CGPointMake(10, 10) toView:greenView];
//以greenView的(0.0)为原点,返回blueView 的(10,10)的坐标

NSLog(@"%f,%f",newPoint.x,newPoint.y);

CGRect newRect = [blueView convertRect:CGRectMake(10, 10, 10, 10) toView:greenView];
//以greenView的(0.0)为原点,返回blueView 的(10,10)的坐标,大小一致(10,10)

NSLog(@"%f,%f,%f,%f",newRect.origin.x,newRect.origin.y,newRect.size.width,newRect.size.height);
}

//  fromView
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

CGPoint newPoint = [blueView convertPoint:CGPointMake(10, 10) fromView:greenView];
//以blueView 的(0.0)点为原点,返回greenVView的(10,10)点的坐标

NSLog(@"%f,%f",newPoint.x,newPoint.y);

CGRect newRect = [blueView convertRect:CGRectMake(10, 10, 10, 10) fromView:greenView];
//以blueView 的(0.0)点为原点,返回greenVView的(10,10)点的坐标,大小一致(10,10)

NSLog(@"%f,%f,%f,%f",newRect.origin.x,newRect.origin.y,newRect.size.width,newRect.size.height);

}


【子视图根据父视图的变化自动变化】

@property(nonatomic)BOOL autoresizesSubviews; // 作为父视图,允许子视图自动变化。

@property(nonatomic)UIViewAutoresizing autoresizingMask; // 子视图的自动变化方式(UIViewAutoresizing)

- (void)viewDidLoad {
[super viewDidLoad];

redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

greenView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 40, 40)];
greenView.backgroundColor = [UIColor greenColor];
[redView addSubview:greenView];

redView.autoresizesSubviews = NO;//作为父视图,允许子视图自动变化。
greenView.autoresizingMask = UIViewAutoresizingFlexibleWidth;//可以多选

/*
UIViewAutoresizingNone                 = 0,		// 不随父视图变化,
UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,	// 左侧可变
UIViewAutoresizingFlexibleWidth        = 1 << 1,	// 宽度可变
UIViewAutoresizingFlexibleRightMargin  = 1 << 2,	// 右侧可变
UIViewAutoresizingFlexibleTopMargin    = 1 << 3,	// 上部可变
UIViewAutoresizingFlexibleHeight       = 1 << 4,	// 高度可变
UIViewAutoresizingFlexibleBottomMargin = 1 << 5	// 下部可变
*/
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[redView setFrame:CGRectMake(10,100, 190, 190)];

}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[redView setFrame:CGRectMake(100, 100, 100, 100)];

}


【自动计算控件的best大小 - labelimageView

- (CGSize)sizeThatFits:(CGSize)size; // 获取控件最佳大小

- (void)sizeToFit; // 设置控件最佳大小

UILabel *label = [[UILabel alloc]init];
label.backgroundColor = [UIColor orangeColor];
[self.view addSubview:label];

label.text = @"sizeToFit";                  //首先label 赋值
[label sizeThatFits:CGSizeZero];            //自动计算label。best 大小
[label sizeToFit];                          //自动设置label大小
[label setCenter:CGPointMake(100, 100)];    //设置label位置

UIImageView *img = [UIImageView new];
[self.view addSubview:img];

img.image = [UIImage imageNamed:@"E6222627-6E51-4F54-88C5-723735D226D6"];
[img sizeThatFits:CGSizeZero];
[img sizeToFit];
[img setCenter:CGPointMake(200, 200)];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: