阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 3915|回复: 1

[ios源码] iOS Swift3.0实现视频播放横竖屏切换效果

[复制链接]

4208

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
126153
QQ
发表于 2017-3-13 13:23:21 | 显示全部楼层 |阅读模式
iOS Swift3.0实现视频播放横竖屏切换效果

关键代码:添加检测设备方向的通知:
  1. UIDevice.current.beginGeneratingDeviceOrientationNotifications()
  2. // 检测设备方向
  3. NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
复制代码
设备方向改变调用:

  1. /// 设备方向改变
  2. @objc fileprivate func deviceOrientationChange() {
  3.     // 获取当前设备方向
  4.     let orientation = UIDevice.current.orientation
  5.     // 如果手机硬件屏幕朝上或者屏幕朝下或者未知
  6.     if orientation == UIDeviceOrientation.faceUp || orientation == UIDeviceOrientation.faceDown || orientation == UIDeviceOrientation.unknown {
  7.         return
  8.     }
  9.     // UIDeviceOrientation 和 UIInterfaceOrientation 方向转换
  10.     let interfaceOrientation: UIInterfaceOrientation = UIInterfaceOrientation(rawValue: orientation.rawValue)!
  11.     switch interfaceOrientation {
  12.     //屏幕竖直,home键在上面
  13.     case UIInterfaceOrientation.portraitUpsideDown: break
  14.     //屏幕竖直,home键在下面
  15.     case UIInterfaceOrientation.portrait:
  16.         toOrientation(orientation: UIInterfaceOrientation.portrait); break
  17.     //屏幕水平,home键在左
  18.     case UIInterfaceOrientation.landscapeLeft:
  19.         toOrientation(orientation: UIInterfaceOrientation.landscapeLeft); break
  20.     //屏幕水平,home键在右
  21.     case UIInterfaceOrientation.landscapeRight:
  22.         toOrientation(orientation: UIInterfaceOrientation.landscapeRight); break
  23.     default:
  24.         break
  25.     }
  26. }

  27. /// 旋转
  28. private func toOrientation(orientation: UIInterfaceOrientation) {

  29.     //获取当前状态栏的方向
  30.     let currentOrientation = UIApplication.shared.statusBarOrientation
  31.     //如果当前的方向和要旋转的方向一致,直接return
  32.     if currentOrientation == orientation {
  33.         return
  34.     }
  35.     //根据要旋转的方向,重新布局
  36.     if orientation != UIInterfaceOrientation.portrait {
  37.         // 从全屏的一侧直接到全屏的另一侧不修改
  38.         if currentOrientation == UIInterfaceOrientation.portrait {
  39.             label.snp.makeConstraints({ (make) in
  40.                 make.width.equalTo(UIScreen.main.bounds.size.height)
  41.                 make.height.equalTo(UIScreen.main.bounds.size.width)
  42.                 make.center.equalTo(UIApplication.shared.keyWindow!)
  43.             })
  44.         }
  45.     }
  46.     //状态栏旋转
  47.     UIApplication.shared.setStatusBarOrientation(orientation, animated: false)
  48.     UIView.beginAnimations(nil, context: nil)
  49.     UIView.setAnimationDuration(0.35)
  50.     label.transform = CGAffineTransform.identity
  51.     label.transform = getTransformRotationAngle()
  52.     UIView.commitAnimations()
  53. }

  54. /// 获取旋转角度
  55. private func getTransformRotationAngle() -> CGAffineTransform {
  56.     let interfaceOrientation = UIApplication.shared.statusBarOrientation
  57.     if interfaceOrientation == UIInterfaceOrientation.portrait {
  58.         return CGAffineTransform.identity
  59.     } else if interfaceOrientation == UIInterfaceOrientation.landscapeLeft {
  60.         return CGAffineTransform(rotationAngle: (CGFloat)(-M_PI_2))
  61.     } else if (interfaceOrientation == UIInterfaceOrientation.landscapeRight) {
  62.         return CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
  63.     }
  64.     return CGAffineTransform.identity
  65. }
复制代码


  1. 在Info.plist中通添加View controller-based status bar appearance并设置值为NO(why)
复制代码


Demo效果:

游客,如果您要查看本帖隐藏内容请回复




捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

0

主题

12

回帖

84

积分

注册会员

Rank: 2

积分
84
发表于 2018-3-23 09:28:23 | 显示全部楼层
登录可见评论
回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2024-4-19 15:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表