[英]iOS/iPad orientation change: rotate views manually vs. have 1 view ready for each orientation
[英]iOS rotate single view on orientation change
我的应用程序仅是纵向模式,但是我希望能够在方向更改时旋转单个UIImageView,我可以通过以下方式获得方向事件:
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: nil)
然后在“ orientationChanged:”中,我有以下代码来旋转图像
var o = UIDevice.currentDevice().orientation
var angle:Double = 0;
if (o == UIDeviceOrientation.LandscapeLeft ){angle = M_PI_2}
else if (o == UIDeviceOrientation.LandscapeRight ){angle = -M_PI_2}
else if (o == UIDeviceOrientation.PortraitUpsideDown ){ angle = M_PI}
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.previewImage.transform = CGAffineTransformMakeRotation((CGFloat)(angle))
switch(UIDevice.currentDevice().orientation)
{
case UIDeviceOrientation.LandscapeLeft, UIDeviceOrientation.LandscapeRight:
println("landscape")
self.previewImage.frame = CGRectMake(0, 0, self.previewCardContainer.bounds.size.height, self.previewCardContainer.bounds.size.width)
default:
self.previewImage.frame = self.previewCardContainer.bounds
}
self.previewImage.center = self.previewCardContainer.center
self.previewCardContainer.layoutSubviews()
})
PreviewImage是我要旋转的一个,它包含在一个名为PreviewImageCard的UIView中。 现在它进行旋转,但是我希望它每次旋转90度时都保持容器的形状,因此,如果横向放置,则宽度和高度将有效地交换,如果纵向放置,则容器将保持正常的框架。
我已经尝试过使用autolayout了,但是我很确定这会弄乱一切,所以在我的情节提要中,我没有任何约束(只是占位符固有尺寸)留在了PreviewImage中,
previewCardContainer.setTranslatesAutoresizingMaskIntoConstraints(true) //and false doesnt seem to help
PreviewImage旋转得很好,但是框架根本不更新,我也不知道为什么,是什么导致框架无法在动画中更新?
ps:我不介意在objective-c中回答
我设法通过实际使用自动布局来解决此问题,我设置了宽度和高度约束以及垂直和水平中心约束
然后在我的viewDidLoad我去
previewImageHeightConstraint.constant = previewCardContainer.frame.size.height - 20
previewImageWidthConstraint.constant = previewCardContainer.frame.size.width - 20
在我的orientationChanged:
我去的方法
func orientationChanged(notification:NSNotification){
var o = UIDevice.currentDevice().orientation
var angle:Double = 0;
if (o == UIDeviceOrientation.LandscapeLeft ){angle = M_PI_2}
else if (o == UIDeviceOrientation.LandscapeRight ){angle = -M_PI_2}
else if (o == UIDeviceOrientation.PortraitUpsideDown ){ angle = M_PI}
UIView.animateWithDuration(0.3, animations: { () -> Void in
var rot = CGAffineTransformMakeRotation((CGFloat)(angle))
self.previewImage.transform = rot
switch(UIDevice.currentDevice().orientation)
{
case UIDeviceOrientation.LandscapeLeft, UIDeviceOrientation.LandscapeRight:
self.previewImageWidthConstraint.constant = self.previewCardContainer.frame.size.height - 20
self.previewImageHeightConstraint.constant = self.previewCardContainer.frame.size.width - 20
default:
self.previewImageHeightConstraint.constant = self.previewCardContainer.frame.size.height - 20
self.previewImageWidthConstraint.constant = self.previewCardContainer.frame.size.width - 20
}
self.previewImage.center = self.previewCardContainer.center
self.previewCardContainer.layoutIfNeeded()
})
}
并正确旋转图像,同时保持图像居中,并根据其方向调整宽度和高度。 看看情节提要中的约束:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.