如果方向发生变化,有人可以给我提示如何为scrollview设置框架吗?现在我正在做什么来显示图像。任何帮助都会有帮助。
mainScrollView.frame = self.view.frame
for i in 0 ..< images.count {
let imageView = UIImageView()
imageView.image = images[i].image
imageView.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
if let index = images.index(where: { $0.imageName == selectedImageName }){
let position = CGPoint(x: (mainScrollView.frame.width * CGFloat(index)), y: 0)
mainScrollView.setContentOffset(position, animated: true)
}
答案 0 :(得分:1)
在VDL
方法
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
并实现以下方法
func rotated() {
if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation))
{
print("landscape")
}
if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation))
{
print("Portrait")
}
}