2017-11-15 2 views
0

플랫인지 여부를 감지하는 방법이 있습니까 가로 또는 평평한 세로 방향 장치?평평한 가로/세로 방향 감지

UICollectionView이 내 UIViewController에 있고 인물 모드 모드의 셀이 한 줄에 한 줄로 표시되어 있습니다. 풍경에서 그들은 단지 열에 있습니다 - 그들은 수평에서 수직으로 바꿨습니다. 60 초마다 수집 데이터를 업데이트했습니다. 이 시간이 지나면 데이터를 실현하기 위해 reloadData()으로 전화해야하고 장치 * inFlat 방향을 그대로두면 레이아웃이 깨집니다. sizeForItemAt입니다. 조건 중 어느 것도 완벽하지 않습니다.

let isFlat = UIDevice.currentorientation.isFlat 
if isFlat && isPortrait { 
// 
} 

그것은 동시에 isPortraitisFlat에있을 수 없습니다

let isPortrait = UIDevice.current.orientation.isPortrait 
if !isPortrait { 
    return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height/4) 
} else { 
    return CGSize(width: collectionView.bounds.width/4, height: collectionView.bounds.height) 
} 

은 다음과 같이 배치하는 방법은 없습니다.

이 문제가 있습니까?

+0

코드가 제대로 작동합니다. 여기에 뭐가 잘못 됐니? – trungduc

+0

물론 그렇습니다. 그러나 우리 장치가 평면 세로/평면 가로 위치에있을 때 언급하지 않았으므로 u는'reloadData()'를 호출 할 것입니다. 당신이 볼 수있는 첫 번째'if'는! isPortrait = landscape입니다. 그러나 디바이스를 desc와 landscape에두면 디바이스가 60 초 후에 즉시 플랫으로 업데이트 될 것입니다. 'else' 문을 호출하면 결과가 형편 없습니다. – noname

답변

0

제 생각에는 문제를 해결하려면 항목의 마지막 크기를 저장해야합니다. Google 기기가 평평하게 유지되면 sizeForItemAt은 상품의 마지막 사이즈를 반환합니다. currentSize

var currentSize : CGSize? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // In viewDidLoad, need to set default value for currentSize. Make sure in case 
    // your app start at flat orientation, you collectionView still has a size 
    currentSize = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height/4); 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    if UIDevice.current.orientation.isPortrait { 
    currentSize = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height/4) 
    } else if UIDevice.current.orientation.isPortrait { 
    currentSize = CGSize(width: collectionView.bounds.width/4, height: collectionView.bounds.height) 
    } 
    return currentSize; 
} 

만 갱신 값이 장치는 가로 또는 세로 모드로 유지합니다. 그렇지 않은 경우 항목의 현재 크기를 사용하십시오.

+0

그래, 내가 생각하고 있던 하나의 솔루션이지만, 좀 지저분 해, 나는이 UIViewController에서 그 방향을 사용하지 못하게했다. 시간 내 주셔서 감사합니다. – noname