【问题标题】:iOS rotate single view on orientation changeiOS在方向更改时旋转单个视图
【发布时间】: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


    【解决方案1】:

    我实际上通过使用自动布局来解决这个问题,我设置了宽度和高度约束以及垂直和水平中心约束

    然后在我看来DidLoad 我走了

        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()
        })
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多