当风景时,快速滚动视图全屏

时间:2017-05-21 08:27:58

标签: ios swift scroll landscape-portrait

当设备处于横向但不是纵向时,我试图让滚动视图占据整个屏幕这是我在纵向模式下的视图。我希望这可以像youtube应用程序使用视频一样工作。任何示例代码都非常有用

enter image description here

这就是我设置滚动视图的方式

DispatchQueue.main.async(execute: { () -> Void in
            let imgURL = NSURL(string: self.property[i].image)
            let data = NSData(contentsOf: (imgURL as URL?)!)
            let imageView = UIImageView()
            imageView.image = UIImage(data: data! as Data)
            let xPosition = self.view.frame.width * CGFloat(i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: self.imageScrollView.frame.width, height: self.imageScrollView.frame.height)

            self.imageScrollView.contentSize.width = self.imageScrollView.frame.width * CGFloat(i + 1)
            self.imageScrollView.addSubview(imageView)
        }) //End DispatchQueue

1 个答案:

答案 0 :(得分:1)

您是否在此视图中使用自动布局?如果是这样,您可以在方向更改时更改滚动视图的常数(可以使用UIDeviceOrientationDidChangeNotification),或者如果您只是在代码中设置框架,则相应地进行框架调整

在viewwillappear中注册通知

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)    name:UIDeviceOrientationDidChangeNotification  object:nil];

此方法将通过方向更改进行调用

-

 (void)orientationChanged:(NSNotification *)notification{

switch ([[UIApplication sharedApplication] statusBarOrientation])
    {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
        { 
        //set frame/ constarints for portrait    
        }

            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
        {
        //set frame/ constarints for landscape
        }
            break;
        case UIInterfaceOrientationUnknown:break;
    }

}

别忘了删除观察者

-(void)viewDidDisappear:(BOOL)animated{
   [super viewDidDisappear:animated];
   [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}