問題描述
使用 CGAfflineTransformMakeScale/Rotation 只做一個動作 (Using CGAfflineTransformMakeScale/Rotation only does one action)
當用戶將屏幕旋轉為橫向時,我正在嘗試使視頻旋轉並放大。
‑ (void) orientationChanged:(NSNotification *)note
{
bool switchedLeft;
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
self.videoView.transform=CGAffineTransformMakeScale(0.5,0.5);
if (switchedLeft) {
self.videoView.transform=CGAffineTransformMakeRotation(‑M_PI_2);
}else{
self.videoView.transform=CGAffineTransformMakeRotation(M_PI_2);
}
break;
case UIDeviceOrientationLandscapeLeft:
self.videoView.transform=CGAffineTransformMakeRotation(M_PI_2);
self.videoView.transform=CGAffineTransformMakeScale(2.0, 2.0);
switchedLeft=true;
break;
case UIDeviceOrientationLandscapeRight:
self.videoView.transform=CGAffineTransformMakeRotation(‑M_PI_2);
self.videoView.transform=CGAffineTransformMakeScale(2.0, 2.0);
switchedLeft=false;
break;
default:
break;
};
}
存在許多問題。首先,當我最初旋轉到橫向時,它只進行一次轉換,在這種配置中,它只是縮放它。
第二個問題是,當我旋轉到縱向時,它需要旋轉,但它從不旋轉。但是我可以在橫向左側和橫向右側之間來回移動並且它可以正確旋轉。任何幫助將不勝感激
參考解法
方法 1:
You are essentially replacing the rotation transform with scale transform. In order to apply both, you need to use CGAffineTransformConcat()
.
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform scale = CGAffineTransformMakeScale(2.0, 2.0);
self.videoView.transform = CGAffineTransformConcat(rotate, scale);
As for the second part, you don't need to apply another rotation, instead set it to default using CGAffineTransformIdentity
.
case UIDeviceOrientationPortrait:
CGAffineTransform scale = CGAffineTransformMakeScale(0.5,0.5);
self.videoView.transform = CGAffineTransformConcat(CGAffineTransformIdentity, scale);
break;
方法 2:
try this
CGAffineTransform transform = CGAffineTransformRotate(self. videoView.transform, M_PI);
self. videoView.transform = transform;
(by Kyle Griffith、ZeMoon、Moin Shirazi)