【发布时间】:2015-06-30 10:04:18
【问题描述】:
我的应用程序只是纵向模式,但我希望能够在方向改变时旋转单个 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 度时保持容器的形状,因此如果它处于横向,则宽度和高度将有效地交换,如果容器是纵向的,则保持容器的正常框架。
我已经尝试过使用自动布局,但我很确定这会搞砸一切,所以在我的故事板中,我没有任何约束(只是占位符固有大小)离开 previewImage 并设置
previewCardContainer.setTranslatesAutoresizingMaskIntoConstraints(true) //and false doesnt seem to help
previewImage 旋转正常,但帧根本没有更新,我不知道为什么,是什么导致帧在动画中不更新?
ps:我不介意 Objective-c 中的答案
【问题讨论】:
标签: ios objective-c swift