屏幕旋转
-
Orientation :方向
- device Orientation
- interface Orientation
屏幕向右转,设备向左转才能正确显示
image.png
设备旋转的结果,这个时候界面的旋转还没有结束,在这里可以自行控制界面的旋转方向。
开发过程中有可能需要排除这两个方向
在这个通知里,界面界面已经旋转结束了
image.png
第一个shouldAutorotate表示是否可以响应屏幕的自动旋转,第二个是表示用位运算表示的屏幕方向,默认情况下支持所有方向但不支持upsitedown
ios8之前
ios8之后会统一到一个方法里面
这些都是面向方向的,他会已当前设备方向来返回一个尺寸
需求:竖屏的时候默认控件上下结构,横屏的时候是左右结构
思路:发生旋转的时候重新layout,要用到下面的方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
//animateAlongsideTransition第一个block表示正在旋转中,第二个表示旋转结束后
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
{
_button.frame = CGRectMake(100, 100, 150, 50);
_textField.frame = CGRectMake(100, 300, 200, 30);
}
else
{
_button.frame = CGRectMake(100, 100, 150, 50);
_textField.frame = CGRectMake(300, 100, 200, 30);
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
NSLog(@"screen bounds %@", NSStringFromCGRect([UIScreen mainScreen].bounds));
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
强制屏幕旋转
在某些情况下需要做隐藏statusBar的操作
已经设置了四个页面的旋转,有的页面又想让他固定在一个页面上
可以让当前页面一直保持竖屏的状态
自定义转场类型
实现自定义转场最少的需求
image.png
image.png
非交互式转场
image.png
告诉系统用自定义而非默认
第一个方法在present到model的时候用自定义的转场,第二个方法是在dismiss的时候用自定义转场
实现转场过程动画
1.动画时长,2.转场动画在这里实现
转场过程数据
1.containerView表示VC在切换的时候的view容器,2.completeTransition向context汇报切换已经完成了,3.viewControllerForkew,viewForkey获取相关的viewController或者view的方法,4,initialframeForview、finalFrameForview分别表示的是起始位置和终点位置。
1.即将消失的Controller称之为fromviewcontroller,即将出现的controller称为Toviewcontroller
实现
demo
present的自定义转场
@interface TestViewController()<TestSecondViewControllerDelegate, UIViewControllerTransitioningDelegate>
{
PresentAnimation *_presentAnimation;
DismissAnimation *_dismissAnimation;
}
-(void)init
{
_presentAnimation = [[PresentAnimation alloc] init];
_dismissAnimation = [[DismissAnimation alloc] init];
}
//1.添加代理方法
TestSecondViewController *modalVC = [[TestSecondViewController alloc] initWithFromPush:NO];
modalVC.delegate = self;
modalVC.transitioningDelegate = self;
//设置这个present之前的视图不会消失,如果面层forscreen的话会让之前视图消失
modalVC.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:modalVC animated:YES completion:nil];
#pragma mark - transitioning delegate
//实现代理方法
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
{
if ([presented isKindOfClass:[TestSecondViewController class]])
{
//返回自己定义的动画
return _presentAnimation;
}
return nil;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
if ([dismissed isKindOfClass:[TestSecondViewController class]])
{
return _dismissAnimation;
}
return nil;
}
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface PresentAnimation : NSObject<UIViewControllerAnimatedTransitioning>
@end
#import "PresentAnimation.h"
@implementation PresentAnimation
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
return 1;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
//获取推入的VC
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//获取推出的VC
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
//做动画需要获取containview
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toView];
//获取最终推入的VC坐标
CGRect targetFrame = [transitionContext finalFrameForViewController:toVC];
//设置准备推出的view的位置:当前为屏幕的上方
toView.frame = CGRectOffset(targetFrame, 0, - targetFrame.size.height);
//another try
// targetFrame = CGRectOffset(targetFrame, 0, 200);
//获取这个方法的时长
NSTimeInterval duration = [self transitionDuration:transitionContext];
//添加这个动画
[UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
toView.frame = targetFrame;
} completion:^(BOOL finished) {
//完成之后结束动画
[transitionContext completeTransition:YES];
}];
}
@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DismissAnimation : NSObject<UIViewControllerAnimatedTransitioning>
@end
#import "DismissAnimation.h"
@implementation DismissAnimation
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toView];
[containerView sendSubviewToBack:toView];
CGRect initFrame = [transitionContext initialFrameForViewController:fromVC];
CGRect targetFrame = CGRectOffset(initFrame, 0, - initFrame.size.height);
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:duration animations:^{
fromView.frame = targetFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
@end