如何对使用了ios autolayout适配的UIView添加动画

问题补充&&
热心网友&5-04 14:03
猜你感兴趣
服务声明: 信息来源于互联网,不保证内容的可靠性、真实性及准确性,仅供参考,版权归原作者所有!Copyright &
Powered byIOS开发 使用XIB自定义一个UIView - 孙启超
- 博客频道 - CSDN.NET
12538人阅读
原文:http://blog.csdn.net/developer_zhang/article/details/8953640
Xcode中集成的BI中的控件十分丰富,但有时候难免不能满足我们的需求,今天我们来学习一下如何使用XIB自定义一个UIView,做到复用的作用。
2 详细流程
目录视图:
2.1 新建一个single view application类型的iOS application工程,名字取为CustomView,如下图,我们不往CustomViewViewController.xib中添加任何控件
2.2 新建一个CustomView.xib,过程如下:
然后往界面上拖一个label和一个button:
2.3修改View视图的属性:
去掉Autolayout:
设置Size为Freeform,设置背景颜色:
2.4 设置ZYViewController.xib中的View的Size属性为None:
ZYViewController.m代码:
-&(void)viewDidLoad&&{&&&&&&[super&viewDidLoad];&&&&&&//获得nib视图数组&&&&&&NSArray&*nib&=&[[NSBundle&mainBundle]loadNibNamed:@&CustomView&&owner:self&options:nil];&&&&&&//得到第一个UIView&&&&&&UIView&*tmpCustomView&=&[nib&objectAtIndex:0];&&&&&&//获得屏幕的Frame&&&&&&CGRect&tmpFrame&=&[[UIScreen&mainScreen]&bounds];&&&&&&//设置自定义视图的中点为屏幕的中点&&&&&&[tmpCustomView&setCenter:CGPointMake(tmpFrame.size.width&/&2,&tmpFrame.size.height&/&2)];&&&&&&//添加视图&&&&&&[self.view&addSubview:tmpCustomView];&&&&&&&&}&&
运行结果:
以上是所有内容,希望对大家有所帮助。
Demo代码下载:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:367090次
积分:5241
积分:5241
排名:第2105名
原创:169篇
转载:31篇
评论:74条
邮箱: 新浪微博:/supersunqichao QQ,微信:
/supersunqichao
(12)(1)(5)(6)(3)(6)(1)(19)(3)(7)(4)(2)(9)(19)(9)(6)(14)(9)(6)(3)(1)(5)(2)(4)(10)(4)(3)(7)(1)(5)(11)(4)接上文:: 在代码中使用Autolayout (1) - 按比例缩放和优先级。
我们继续来看在代码中使用Autolayout的话题。先说intrinsicContentSize,也就是控件的内置大小。比如UILabel,UIButton等控件,都有自己的内置大小。控件的内置大小往往是由控件本身的内容所决定的,比如一个UILabel的文字很长,那么该UILabel的内置大小自然会很长。控件的内置大小可以通过UIView的intrinsicContentSize属性来获取内置大小,也可以通过invalidateIntrinsicContentSize方法来在下次UI规划事件中重新计算intrinsicContentSize。如果直接创建一个原始的UIView对象,显然它的内置大小为0。
继续用代码来写Autolayout,先写一个辅助方法来快速设置UIView的边距:
//设置Autolayout中的边距辅助方法
- (void)setEdge:(UIView*)superview view:(UIView*)view attr:(NSLayoutAttribute)attr constant:(CGFloat)constant
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:attr relatedBy:NSLayoutRelationEqual toItem:superview attribute:attr multiplier:1.0 constant:constant]];
接下来,创建一个UIView,利用上面的辅助方法快速设置其在父控件的左,上,右边距为20单位。如下代码:
UIView *view1 = [UIView new];
view1.backgroundColor = [UIColor yellowColor];
//不允许AutoresizingMask转换成Autolayout
view1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view1];
//设置左,上,右边距为20.
[self setEdge:self.view view:view1 attr:NSLayoutAttributeLeft constant:20];
[self setEdge:self.view view:view1 attr:NSLayoutAttributeTop constant:20];
[self setEdge:self.view view:view1 attr:NSLayoutAttributeRight constant:-20];
但是运行后会,界面上不会显示任何东西。原因就是上面讲的,UIView默认是没有intrinsicContentSize的。我们可以通过创建一个的UIView来改写intrinsicContentSize。
比如,创建一个新的类型:MyView。
然后在.m文件中改写intrinsicContentSize方法,并返回有效值,比如这样:
//改写UIView的intrinsicContentSize
- (CGSize)intrinsicContentSize
    return CGSizeMake(70, 40);
接着修改最上面的代码,把上面view1变量的类型从UIView替换成我们自定义的View:MyView类型:
MyView *view1 = [MyView new];
再次运行代码,View会按照要求显示在屏幕上:
接下来,按照同样的方式,在下方添加另一个MyView,要求其距离父控件边距左,下,右各为20,代码:
MyView *view2 = [MyView new];
view2.backgroundColor = [UIColor yellowColor];
//不允许AutoresizingMask转换成Autolayout
view2.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view2];
//设置左,下,右边距为20.
[self setEdge:self.view view:view2 attr:NSLayoutAttributeLeft constant:20];
[self setEdge:self.view view:view2 attr:NSLayoutAttributeBottom constant:-20];
[self setEdge:self.view view:view2 attr:NSLayoutAttributeRight constant:-20];
运行后是这样:
接下来,通过代码加入Autolayout中的间距。命令view1和view2上下必须间隔20个单位,注意这里要求view2在view1之下的20单位,所以创建NSLayoutConstraint中view2参数在前面。同时注意,view2的attribute参数是NSLayoutAttributeTop,而view1的attribute参数是NSLayoutAttributeBottom:
//设置两个View上下间距为20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20]];
运行结果:
OK,的确,此时view1和view2相互间隔20单位,但是view1被拉伸了。
接下来的任务就是做到如何不让view1拉伸,而让view2拉伸呢?这里就需要使用控件的Content Hugging Priority,这个属性在Xcode中的控件属性中很常见,如下图:
Content Hugging Priority代表控件拒绝拉伸的优先级。优先级越高,控件会越不容易被拉伸。
而下面的Content Compression Resistance Priority代表控件拒绝压缩内置空间的优先级。优先级越高,控件的内置空间会越不容易被压缩。而这里的内置空间,就是上面讲的UIView的intrinsicContentSize。
所以,如果我们把view1(上图中被拉伸的,在上面的View)的Content Hugging Priority设置一个更高的值,那么当Autolayout遇到这种决定谁来拉伸的情况时,view1不会被优先拉伸,而优先级稍低的view2才会被拉伸。
可以直接通过UIView的setContentHuggingPriority:forAxis方法来设置控件的Content Hugging Priority,其中forAxis参数代表横向和纵向,本例中只需要设置纵向,所以传入UILayoutConstraintAxisVertical。整句代码:
//提高view1的Content Hugging Priority
[view1 setContentHuggingPriority:UILayoutPriorityHigh forAxis:UILayoutConstraintAxisVertical];
运行结果:如何对使用了autolayout的UIView添加动画_百度知道
如何对使用了autolayout的UIView添加动画
我有更好的答案
按默认排序
[self.fil setTransform,要是你想添加位置移动
其他颜色之类的动画倒是无所谓假如你非得要你的这个UIView动起来也可以
你只需要对Transformation进行设置动画就行类似于下面这种方式
[UIView animateWithDuration, 20)];其中的self:0:CGAffineTransformMakeTranslation(50autolayout之后
frame是不能够设置的
其他类似问题
uiview的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁如何对使用了autolayout的UIView添加动画_百度知道
如何对使用了autolayout的UIView添加动画
我有更好的答案
按默认排序
其他类似问题
uiview的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 autolayout 的文章

 

随机推荐