iOS压缩动画 CGAffineTransform
生活随笔
收集整理的這篇文章主要介紹了
iOS压缩动画 CGAffineTransform
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
因?yàn)轫?xiàng)目需求,需要如下這樣一個(gè)簡(jiǎn)單的動(dòng)畫效果:
####單個(gè)控件 這個(gè)動(dòng)畫效果,如果對(duì)控件使用UIView的block動(dòng)畫,設(shè)置控件的Frame值,是可以實(shí)現(xiàn)的.
比如下面這個(gè)效果:
我們可以代碼來簡(jiǎn)單實(shí)現(xiàn)上面的效果: -?(void)viewDidLoad?{????[super?viewDidLoad];
????
????
????self.btn?=?[[UIButton?alloc]?initWithFrame:CGRectMake(100,?100,?200,?200)];
????[self.btn?setImage:[UIImage?imageNamed:@"1"]?forState:UIControlStateNormal];
????[self.view?addSubview:self.btn];
????
}
-?(void)touchesBegan:(NSSet<UITouch?*>?*)touches?withEvent:(UIEvent?*)event
{
????[UIView?animateWithDuration:1.0?animations:^{
????????self.btn.frame?=?CGRectMake(100,?300,?200,?0);
????}];
}
復(fù)制代碼
####如果是多個(gè)控件呢? 我們比葫蘆畫瓢,把btn放到一個(gè)View當(dāng)中,我們?cè)賹?duì)View使用一個(gè)UIView的block動(dòng)畫. 首先看下代碼:
-?(void)viewDidLoad?{????[super?viewDidLoad];
????
????self.outView?=?[[UIView?alloc]?initWithFrame:CGRectMake(0,?0,?[UIScreen?mainScreen].bounds.size.width,?[UIScreen?mainScreen].bounds.size.height-44)];
????self.outView.backgroundColor?=?[UIColor?redColor];
????
????self.btn?=?[[UIButton?alloc]?initWithFrame:CGRectMake(100,?100,?200,?200)];
????[self.btn?setImage:[UIImage?imageNamed:@"1"]?forState:UIControlStateNormal];
????
????[self.outView?addSubview:self.btn];
????[self.view?addSubview:self.outView];
????
}
-?(void)touchesBegan:(NSSet<UITouch?*>?*)touches?withEvent:(UIEvent?*)event
{
????[UIView?animateWithDuration:1.0?animations:^{
????????self.outView.frame?=?CGRectMake(0,?[UIScreen?mainScreen].bounds.size.height-44,?[UIScreen?mainScreen].bounds.size.width,?0);
????}];
}
復(fù)制代碼
然后我們看下效果:
紅色的View發(fā)生了形變,但是里面的控件并沒有跟著縮放. 看來這個(gè)方案不行. 我想,應(yīng)該可以用CGAffineTransform來進(jìn)行一個(gè)動(dòng)畫縮放. 然后把上面 self.outView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-44, [UIScreen mainScreen].bounds.size.width, 0); 替換成 self.outView.layer.affineTransform = CGAffineTransformScale(self.outView.layer.affineTransform, 1.0, 0.001);(這里的sy如果為0的話,會(huì)直接消失.)然后動(dòng)畫效果是下面這樣的: 很明顯不符合我們的要求,我想要控件的底部不動(dòng),從上面開始進(jìn)行壓縮下來.####anchorPoint, position, CGAffineTransform 是的,通過這三個(gè)東西,來實(shí)現(xiàn),首先我們上代碼和實(shí)現(xiàn)圖,然后再進(jìn)行分析.
-?(void)viewDidLoad?{????[super?viewDidLoad];
????
????self.outView?=?[[UIView?alloc]?initWithFrame:CGRectMake(0,?0,?[UIScreen?mainScreen].bounds.size.width,?[UIScreen?mainScreen].bounds.size.height-44)];
????self.outView.backgroundColor?=?[UIColor?redColor];
????
????self.btn?=?[[UIButton?alloc]?initWithFrame:CGRectMake(100,?100,?200,?200)];
????[self.btn?setImage:[UIImage?imageNamed:@"1"]?forState:UIControlStateNormal];
????
????[self.outView?addSubview:self.btn];
????[self.view?addSubview:self.outView];
????
}
-?(void)touchesBegan:(NSSet<UITouch?*>?*)touches?withEvent:(UIEvent?*)event
{
????//注意這里不能使用self.outView.frame因?yàn)镕rame會(huì)隨著控件的變化而變化.
????//這樣就導(dǎo)致了layer的position的位置不準(zhǔn)確了.
????CGFloat?positionX?=?0.5*self.outView.bounds.size.width;
????CGFloat?positionY?=?1.0*self.outView.bounds.size.height;
????self.outView.layer.anchorPoint?=?CGPointMake(0.5,?1);
????self.outView.layer.position?=?CGPointMake(positionX,?positionY);
????
????[UIView?animateWithDuration:1.0?animations:^{
????????self.outView.layer.affineTransform?=?CGAffineTransformScale(self.outView.layer.affineTransform,?1,?0.001);
????}];
}
復(fù)制代碼
效果:
這下就完美了,紅色的View和它里面的子控件一起進(jìn)行了縮放. 關(guān)鍵點(diǎn)就那兩行代碼,設(shè)置了 outView 的anchor和position. 關(guān)于anchor和position的文章,網(wǎng)上有很多介紹的.我這里就不再進(jìn)行贅述. position的x = view.bounds.size.width * anchorPoint.x; x = view.bounds.size.height * anchorPoint.y; 當(dāng)你改變anchorPoint的值的時(shí)候,要確保position的值也進(jìn)行了相應(yīng)的改變,否則會(huì)產(chǎn)生View的位置不正確.轉(zhuǎn)載于:https://juejin.im/post/5a376c5451882560b6526ac6
總結(jié)
以上是生活随笔為你收集整理的iOS压缩动画 CGAffineTransform的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 应用内微信 H5 支付
- 下一篇: ASP.NET web.config