View Programming Guide 总结

View Programming Guide 总结

1.所有有关View的transform操作都是相关于view的center,非frame

When modifying the transform property of your view, all transformations are performed relative to the center point of the view.

2.如果view的transform不是identity,那他的frame是不可用的,如果想获取view的size或位置的话,用bounds(转之前的)和center

If a view’s transform property is not the identity transform, the value of that view’s frame property is undefined and must be ignored. When applying transforms to a view, you must use the view’s bounds and center properties to get the size and position of the view. The frame rectangles of any subviews are still valid because they are relative to the view’s bounds.

3.hidden或alpha < 0.01则view不接受事件,hidden属性没有动画,alpha有

To hide a view visually, you can either set its hidden property to YES or change its alpha property to 0.0. A hidden view does not receive touch events from the system(alpha < 0.01 too). However, hidden views do participate in autoresizing and other layout operations associated with the view hierarchy. Thus, hiding a view is often a convenient alternative to removing views from your view hierarchy, especially if you plan to show the views again at some point soon.

If you want to animate a view’s transition from visible to hidden (or the reverse), you must do so using the view’s alpha property. The hidden property is not an animatable property, so any changes you make to it take effect immediately.

4.两种方式获取view
1)用属性property声明,然后获取
2)给view的tag属性赋integer值,然后用view的实例方法viewWithTag:方法获取

There are two ways to locate views in a view hierarchy:

1.Store pointers to any relevant views in an appropriate location, such as in the view controller that owns the views.

2.Assign a unique integer to each view’s tag property and use the viewWithTag: method to locate it.

5.如果view的的transform属性不是CGAffineTransformIdentity,view的frame属性不可用(不准确),但bounds及center属性是可用的(是transform之前的数据)

If a view’s transform property does not contain the identity transform, the frame of that view is undefined and so are the results of its autoresizing behaviors.

6.layer不接收事件

Custom layers do not receive events or participate in the responder chain but do draw themselves and respond to size changes in their parent view or layer according to the Core Animation rules.

7.用程序生成view则用initWithFrame;用xib则调用initWithCoder

//从程序中构建view
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //add your code
    }

    return self;
}

//从nib文件中构建view
- (nullable instancetype)initWithCoder:(NSCoder*)coder
{
    if (self =[superinitWithcoder:coder]) {
        // 初始化代码

    }

    return self;

}

8.尽量不用drawRect,如果可以直接造一个View的话,drawRect太损耗资源

Custom drawing is recommended only as a last resort. In general, if you can use other views to present your content, that is preferred.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值