iOS在方向更改时旋转单个视图

时间:2015-06-29 02:18:24

标签: ios objective-c swift

我的应用程序只是纵向模式,但我希望能够在方向更改时旋转单个UIImageView,我可以通过

获得定向事件
    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()
    })

然后在“orientationChanged:”中我有以下代码来旋转图像

previewCardContainer.setTranslatesAutoresizingMaskIntoConstraints(true) //and false doesnt seem to help

previewImage是我想要旋转的,它包含在名为previewImageCard的UIView中。现在它进行旋转,但是我希望它在每次旋转90度时保持容器的形状,因此如果它在横向方向上有效地宽度和高度将交换,并且如果其纵向保持容器正常框架。

我已经尝试过使用autolayout,但我很确定会搞砸一切,所以在我的故事板中我保留了previewImage而没有任何限制(只是占位符内在大小)并且我设置了

{{1}}

previewImage旋转正常,但框架根本没有更新,我不知道为什么,什么可能导致框架在动画中不更新?

ps:我不介意objective-c

中的答案

1 个答案:

答案 0 :(得分:0)

我设法实际使用autolayout来解决这个问题,我设置了宽度和高度约束以及垂直和水平中心约束

然后在我的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()
    })
}

这样可以正确旋转图像,同时保持图像居中,并根据图像的方向调整图像的宽度和高度。看一下故事板中的约束: autolayout constraints