ios旋转屏幕总结 详细教程

第一种:通过人为的办法改变view.transform的属性。

具体办法:

view.transform一般是View的旋转,拉伸移动等属性,类似view.layer.transform,区别在于 View.transform是二维的,也就是使用仿射的办法通常就是带有前缀CGAffineTransform的类(可以到API文档里面搜索这个前 缀的所有类),而view.layer.transform可以在3D模式下面的变化,通常使用的都是前缀为CATransform3D的类。

这里要记住一点,当你改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,记得先把他 们重置可以使用view.transform = CGAffineTransformIdentity,或者view.layer.transform = CATransform3DIdentity,假设你一直不断的改变一个view.transform的属性,而每次改变之前没有重置的话,你会发现后来 的改变和你想要的发生变化了,不是你真正想要的结果。

好了,上面介绍了旋转的属性,接下来就是关键了。官方提供了一个办法就是查看当前电池条的状态UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;通过这个办法,你可以知道当前屏幕的电池条的显示方向,而且你还可以 强制设置他的显示方向,通过设置这个属性就OK了,可以选择是否动画改变电池条方向。有了这两个那我们就可以任意的改变我们想要的显示方式了。

1.获取当前电池条的方向UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation

2.获取当前屏幕的大小CGRect frame = [UIScreen mainScreen].applicationFrame;

3.设置我们的View的中心点
CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));

4.根据当前电池条的方向,获取需要旋转的角度的大小。通常

if (orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformMakeRotation(M_PI*1.5);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
return CGAffineTransformMakeRotation(M_PI/2);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGAffineTransformMakeRotation(-M_PI);
} else {
return CGAffineTransformIdentity;
}

5.可以动画的改变我们view的显示方式了
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];

CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;(获取当前电池条动画改变的时间)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];

//在这里设置view.transform需要匹配的旋转角度的大小就可以了。

[UIView commitAnimations];

第二种:通过setOrientation:的办法强制性的旋转到一个特定的方向。

注意:Apple在3.0以后都不支持这个办法了,这个办法已经成为了私有的了,但是要跳过App Stroe的审核,需要一点巧妙的办法。

不要直接调用[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight]这样的办法来强制性的横屏,这样导致你的程序是很难通过App Store审核的。但是你可以选择使用performSelector的办法来调用它。具体就几行代码如下:

//强制横屏
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}

总结:如果第一种办法可以满足你需要的话,最好使用第一种办法,因为那个上 App Store肯定没问问题,但是第二种的话是需要冒风险的,但是如果你的结构太复杂了,导致使用第一种办法人为很难控制的话,可以尝试简单的使用第二种办 法。我在有米提供的sample里面就看到他使用了第二种简单的办法来显示横屏竖式的广告条。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
iOS 获取屏幕刷新率可以通过Core Animation框架中的CADisplayLink类实现。CADisplayLink是一个定时器类,可以监听屏幕的刷新频率,并在每一帧结束时发送回调。 首先需要创建一个CADisplayLink对象,并添加到主运行循环中: ``` CADisplayLink *displayLink = [UIScreen mainScreen].maximumFramesPerSecond == 0 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 0 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 60 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick60fps:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 120 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick120fps:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 90 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick90fps:)] : nil; [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; ``` 然后实现对应的回调方法,在每帧结束时获取屏幕的刷新率: ``` - (void)displayLinkTick:(CADisplayLink *)link { CGFloat frameInterval = link.duration/60.0; screenRefreshRate = (int)(1.0/frameInterval); } ``` 最后需要注意的是,在iOS 10之后,为了省电和减少GPU负担,屏幕刷新率可能会动态调整,因此获取到的屏幕刷新率并不一定是恒定的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值