Autosize 2 (恒屏和自动旋转)

本文详细介绍了iOS应用中自动旋转的实现方法,包括使用系统自动调整属性进行旋转、在旋转时对视图进行重构以及切换视图以适应不同方向。通过两个函数在旋转动画过程中的应用,实现了界面在不同设备方向上的平滑过渡。

Autosize 2 (恒屏和自动旋转)

自动旋转有如下几种实现方法:
1、使用自动调整属性进行旋转:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

return YES;

}
该方法利用系统自动实现的旋转,view上的元素位置不会改变,可能有的元素会移到屏幕外。
2、在旋转时对视图进行重构(重新设置视图上各个元素的坐标位置)

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientationduration:(NSTimeInterval)duration{    

   UIInterfaceOrientation interfaceOrientation self.interfaceOrientation;

   if (interfaceOrientation == UIInterfaceOrientationPortrait 

      || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)

    {

       button1.frame CGRectMake(2020125125);

       button2.frame CGRectMake(17520125125);

       button3.frame CGRectMake(20168125125);

       button4.frame CGRectMake(175168125125);

       button5.frame CGRectMake(20315125125);

       button6.frame CGRectMake(175315125125);

    }

    else 

    {

       button1.frame CGRectMake(2020125125);

       button2.frame CGRectMake(20155125125);

       button3.frame CGRectMake(17720125125);

       button4.frame CGRectMake(177155125125);

       button5.frame CGRectMake(32820125125);

       button6.frame CGRectMake(328155125125);

    }

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

   return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

 

05 Autosize 2 (恒屏和自动旋转) - 阿帕奇 - XGG XGG05 Autosize 2 (恒屏和自动旋转) - 阿帕奇 - XGG XGG
 

 

有两个函数:

-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration

该方法在发生在旋转动画完全完成之前调用。

-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientationduration:(NSTimeInterval)duration

此方法在旋转开始之后,最后的旋转动画发生之前调用。

3、切换视图

这种方法使用于比较复杂的界面,是需要分别设计横向模式和纵向模式,然后在使用的过程中自动切换。

InterfaceOrientation有四个属性:Portait,LandscapeLeft, LandscapeRight, PortaitUpsideDown.


 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)

interfaceOrientation duration:(NSTimeInterval)duration{

   if (interfaceOrientation == UIInterfaceOrientationPortrait)

    {

       self.view self.portrait;

      self.view.transform CGAffineTransformIdentity;

      self.view.transform CGAffineTransformMakeRotation(degreesToRadian(0));

       self.view.bounds CGRectMake(0.00.0300.0480.0);

    }

   else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)

    {

       self.view self.landscape;

      self.view.transform CGAffineTransformIdentity;

      self.view.transform CGAffineTransformMakeRotation(degreesToRadian(-90));

       self.view.bounds CGRectMake(0.00.0460.0320.0);

    }

   else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)

    {

       self.view self.portrait;

      self.view.transform CGAffineTransformIdentity;

      self.view.transform CGAffineTransformMakeRotation(degreesToRadian(180));

       self.view.bounds CGRectMake(0.00.0300.0480.0);

    }

   else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)

    {

       self.view self.landscape;

      self.view.transform CGAffineTransformIdentity;

       self.view.transform 

      CGAffineTransformMakeRotation(degreesToRadian(90));

       self.view.bounds CGRectMake(0.00.0460.0320.0);

    }

}

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return YES;

 }

因为在上面的代码中使用到了CoreGraphics框架,因此要把该框架连接到该项目中,具体的方法是:在Resources上面点右键Add->ExistingFrameworks。


实现 `<textarea>` 根据内容自动调整高度,并在内容减少时自动收缩,可以通过 JavaScript 动态调整其 `height` 样式属性来实现。以下是一个完整的实现思路代码示例: ### 实现思路 1. 监听 `textarea` 的 `input` 事件,以检测内容的变化。 2. 每次内容变化时,动态调整 `textarea` 的高度,以适应当前内容。 3. 为了确保高度能够随内容减少而收缩,先将 `textarea` 的 `height` 重置为一个默认值,再重新计算其实际高度。 ### 示例代码 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Textarea Autosize</title> <style> textarea { resize: none; /* 禁止手动调整大小 */ overflow: hidden; /* 隐藏滚动条 */ min-height: 50px; /* 设置最小高度 */ max-height: 200px; /* 设置最大高度,防止过高 */ } </style> </head> <body> <textarea id="autosize" placeholder="请输入内容..."></textarea> <script> const textarea = document.getElementById('autosize'); function autosize() { // 重置高度为 auto,以正确计算内容所需高度 textarea.style.height = 'auto'; // 设置新的高度为内容的实际滚动高度 textarea.style.height = Math.min(textarea.scrollHeight, 200) + 'px'; } // 监听 input 事件,实现内容变化时自动调整高度 textarea.addEventListener('input', autosize); // 初始调用一次,确保初始高度合适 autosize(); </script> </body> </html> ``` ### 说明 - `textarea.style.height = 'auto';` 用于重置高度,确保在内容减少时可以正确收缩。 - `textarea.scrollHeight` 返回内容的滚动高度,即内容实际需要的高度。 - `Math.min(textarea.scrollHeight, 200)` 用于限制最大高度,防止 `textarea` 过高。 - `resize: none;` `overflow: hidden;` 用于隐藏浏览器默认的调整手柄滚动条,使外观更整洁。 通过上述方法,可以实现一个能够根据内容动态调整高度,并在内容减少时自动收缩的 `<textarea>` 组件。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值