今天同事問了我一個問題,關(guān)于視頻的橫豎屏播放的鞠呈。效果很簡單融师,豎屏的時候,視頻小窗口播放蚁吝,橫屏的時候全屏播放旱爆。因為之前也沒有研究過視頻播放的內(nèi)容,寫了個簡單的demo發(fā)現(xiàn)窘茁,當橫屏?xí)r怀伦,是整個View全部橫過來,如果這時候鋪滿屏幕的話山林,效果和(天天快報房待,今日頭條)實現(xiàn)的不一樣,于是果斷放棄這個思路。
觀察今日頭條的視頻的橫豎屏效果吴攒,當切換橫豎屏是只是視頻小窗口的鋪滿张抄,屏幕并沒有旋轉(zhuǎn),于是解決方案是這樣的洼怔。
關(guān)閉屏幕橫豎屏旋轉(zhuǎn)
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2286896-b933b70b07f58c7f.png?ima
geMogr2/auto-orient/strip%7CimageView2/2/w/1240)
監(jiān)聽橫豎屏切換
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:)
name:UIDeviceOrientationDidChangeNotification object:nil];
實現(xiàn)同時縮放署惯,旋轉(zhuǎn)
由于改變的都是layer的屬性,所以自帶隱式動畫镣隶,如果想取消隱式動畫
[CATransaction begin];
[CATransaction setDisableActions:YES];
#在commit之前修改屬性值
[CATransaction commit];
- 旋轉(zhuǎn)效果
CGAffineTransformMakeRotation()
CGAffineTransformRotate()這兩個方法都可以實現(xiàn)旋轉(zhuǎn)极谊,但是CGAffineTransformMakeRotation()這個方法不能和其他動畫效果同時實現(xiàn)。
self.layer.affineTransform = CGAffineTransformRotate(self.layer.affineTransform, [self leftDirectionRotationAngle]);
- 縮放效果
self.layer.bounds = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
- 調(diào)用這兩個方法后可實現(xiàn)同時縮放旋轉(zhuǎn)安岂,但是由于layer的錨點為(0.5,0.5),所以旋轉(zhuǎn)縮放都是以layer的中心點實現(xiàn)的效果轻猖,所以要改變layer的position
self.layer.position = CGPointMake(self.layer.position.x, self.view.frame.size.height/2);
動態(tài)計算橫豎屏轉(zhuǎn)換時layer旋轉(zhuǎn)的角度
定義一個當前的屏幕方向
@property (nonatomic, assign) UIDeviceOrientation currentDirection;
#根據(jù)即將要旋轉(zhuǎn)的方向來計算旋轉(zhuǎn)的角度
#即將向左橫屏
- (CGFloat)leftDirectionRotationAngle{
CGFloat angle = 0;
switch (self.currentDirection) {
case UIDeviceOrientationPortrait:
angle = M_PI_2;
break;
case UIDeviceOrientationLandscapeRight:
angle = M_PI;
default:
break;
}
return angle;
}
#即將向右橫屏
- (CGFloat)rightDirectionRotationAngle{
CGFloat angle = 0;
switch (self.currentDirection) {
case UIDeviceOrientationPortrait:
angle = -M_PI_2;
break;
case UIDeviceOrientationLandscapeLeft:
angle = M_PI;
default:
break;
}
return angle;
}
#即將豎屏
- (CGFloat)potraitDirectionRotationAngle{
CGFloat angle = 0;
switch (self.currentDirection) {
case UIDeviceOrientationLandscapeLeft:
angle = -M_PI_2;
break;
case UIDeviceOrientationLandscapeRight:
angle = M_PI_2;
default:
break;
}
return angle;
}
實現(xiàn)效果
flow.2.gif